Fix bugs with input pipe reading (messages beginning with b'\n' or ending with whitespace)
This commit is contained in:
parent
22d22bc5e3
commit
a2d6077619
@ -8,7 +8,7 @@ from .terminate_process_on_failure import terminate_process_on_failure
|
|||||||
|
|
||||||
class PipeReaderThread(Thread):
|
class PipeReaderThread(Thread):
|
||||||
eof = b''
|
eof = b''
|
||||||
stop_sequence = b'stop_reading'
|
stop_sequence = b'stop_reading\n'
|
||||||
|
|
||||||
def __init__(self, pipe_path, stop_event, message_handler):
|
def __init__(self, pipe_path, stop_event, message_handler):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -20,11 +20,11 @@ class PipeReaderThread(Thread):
|
|||||||
def run(self):
|
def run(self):
|
||||||
with open(self._pipe_path, 'rb') as pipe:
|
with open(self._pipe_path, 'rb') as pipe:
|
||||||
while True:
|
while True:
|
||||||
message = pipe.readline().rstrip()
|
message = pipe.readline()
|
||||||
if message in (self.eof, self.stop_sequence):
|
if message in (self.eof, self.stop_sequence):
|
||||||
self._stop_event.set()
|
self._stop_event.set()
|
||||||
break
|
break
|
||||||
self._message_handler(message)
|
self._message_handler(message[:-1])
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.unblock()
|
self.unblock()
|
||||||
@ -33,5 +33,5 @@ class PipeReaderThread(Thread):
|
|||||||
def unblock(self):
|
def unblock(self):
|
||||||
with suppress(OSError):
|
with suppress(OSError):
|
||||||
fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK)
|
fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK)
|
||||||
write(fd, self.stop_sequence + b'\n')
|
write(fd, self.stop_sequence)
|
||||||
close(fd)
|
close(fd)
|
||||||
|
Loading…
Reference in New Issue
Block a user