Fix bug where select() wouldn't block on linux (EOF)

This commit is contained in:
Kristóf Tóth 2019-08-06 16:56:14 +02:00
parent d4a6fe8f49
commit 1bd64c368c

View File

@ -1,6 +1,6 @@
# pylint: disable=redefined-builtin,too-many-instance-attributes # pylint: disable=redefined-builtin,too-many-instance-attributes
from contextlib import contextmanager from contextlib import contextmanager
from os import O_NONBLOCK, O_RDONLY, open, fdopen, close, write, pipe from os import O_NONBLOCK, O_RDONLY, O_WRONLY, open, fdopen, close, write, pipe
from threading import Thread from threading import Thread
from select import select from select import select
@ -12,7 +12,7 @@ class PipeReaderThread(Thread):
super().__init__(daemon=True) super().__init__(daemon=True)
self._message_handler = message_handler self._message_handler = message_handler
self._pipe_path = pipe_path self._pipe_path = pipe_path
self._read_fd, self._read_fp = None, None self._read_fd, self._read_fp, self._prevent_eof_fd = None, None, None
self._stop_signal_rfd, self._stop_signal_wfd = pipe() self._stop_signal_rfd, self._stop_signal_wfd = pipe()
self._msg_buf = b'' self._msg_buf = b''
self._stop_event = stop_event self._stop_event = stop_event
@ -35,8 +35,10 @@ class PipeReaderThread(Thread):
def _open(self): def _open(self):
self._read_fd = open(self._pipe_path, O_RDONLY | O_NONBLOCK) self._read_fd = open(self._pipe_path, O_RDONLY | O_NONBLOCK)
self._read_fp = fdopen(self._read_fd, 'rb') self._read_fp = fdopen(self._read_fd, 'rb')
self._prevent_eof_fd = open(self._pipe_path, O_WRONLY)
yield yield
self._read_fp.close() self._read_fp.close()
close(self._prevent_eof_fd)
close(self._stop_signal_rfd) close(self._stop_signal_rfd)
close(self._stop_signal_wfd) close(self._stop_signal_wfd)