From aa7eea6242f5565554347e9e7841036641c2f301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 3 May 2019 22:00:23 +0200 Subject: [PATCH] Really fix deadlock causing race condition --- pipe_io_server/pipe_reader_thread.py | 13 ++++++------- pipe_io_server/pipe_writer_thread.py | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/pipe_io_server/pipe_reader_thread.py b/pipe_io_server/pipe_reader_thread.py index 7e16b32..84d9288 100644 --- a/pipe_io_server/pipe_reader_thread.py +++ b/pipe_io_server/pipe_reader_thread.py @@ -34,13 +34,12 @@ class PipeReaderThread(Thread): return open(self._pipe_path, 'rb') def stop(self): - self._unblock() + while self.is_alive(): + self._unblock() self.join() def _unblock(self): - while True: - with suppress(OSError): - fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK) - write(fd, self.stop_sequence) - close(fd) - break + with suppress(OSError): + fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK) + write(fd, self.stop_sequence) + close(fd) diff --git a/pipe_io_server/pipe_writer_thread.py b/pipe_io_server/pipe_writer_thread.py index 054496d..47305e4 100644 --- a/pipe_io_server/pipe_writer_thread.py +++ b/pipe_io_server/pipe_writer_thread.py @@ -39,13 +39,12 @@ class PipeWriterThread(Thread): return open(self._pipe_path, 'wb') def stop(self): - self._unblock() + while self.is_alive(): + self._unblock() self.join() def _unblock(self): - while True: - with suppress(OSError): - fd = osopen(self._pipe_path, O_RDONLY | O_NONBLOCK) - self._write_queue.push_front(None) - close(fd) - break + with suppress(OSError): + fd = osopen(self._pipe_path, O_RDONLY | O_NONBLOCK) + self._write_queue.push_front(None) + close(fd)