From dbe5b10482781da1e7697a677775d23bacc07f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 3 May 2019 17:31:57 +0200 Subject: [PATCH] Fix unblock race condition randomly leading to deadlocks --- pipe_io_server/pipe_reader_thread.py | 14 ++++++++------ pipe_io_server/pipe_writer_thread.py | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pipe_io_server/pipe_reader_thread.py b/pipe_io_server/pipe_reader_thread.py index 85e9a2e..7e16b32 100644 --- a/pipe_io_server/pipe_reader_thread.py +++ b/pipe_io_server/pipe_reader_thread.py @@ -34,11 +34,13 @@ class PipeReaderThread(Thread): return open(self._pipe_path, 'rb') def stop(self): - self.unblock() + self._unblock() self.join() - def unblock(self): - with suppress(OSError): - fd = osopen(self._pipe_path, O_WRONLY | O_NONBLOCK) - write(fd, self.stop_sequence) - close(fd) + 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 diff --git a/pipe_io_server/pipe_writer_thread.py b/pipe_io_server/pipe_writer_thread.py index 3be90f9..054496d 100644 --- a/pipe_io_server/pipe_writer_thread.py +++ b/pipe_io_server/pipe_writer_thread.py @@ -39,11 +39,13 @@ class PipeWriterThread(Thread): return open(self._pipe_path, 'wb') def stop(self): - self.unblock() + self._unblock() self.join() - def unblock(self): - self._write_queue.push_front(None) - with suppress(OSError): - fd = osopen(self._pipe_path, O_RDONLY | O_NONBLOCK) - close(fd) + 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