Skip to content

Commit

Permalink
Shutdown process before feeder thread
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelm committed Jun 4, 2024
1 parent d8b9cfb commit 74642c5
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/xopen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def _open_process(self):
# data continuously to the process stdin on another thread.
self.in_thread = threading.Thread(target=self._feed_pipe)
self.in_thread.start()
self._process_explicitly_terminated = False
self._file: BinaryIO = self.process.stdout # type: ignore
self._wait_for_output_or_process_exit()
self._raise_if_error()
Expand All @@ -290,7 +291,11 @@ def _feed_pipe(self):
if chunk == b"":
self.in_pipe.close()
return
self.in_pipe.write(chunk)
try:
self.in_pipe.write(chunk)
except BrokenPipeError:
if not self._process_explicitly_terminated:
raise
finally:
self.in_pipe.close()

Expand Down Expand Up @@ -329,14 +334,15 @@ def close(self) -> None:
return
check_allowed_code_and_message = False
if "r" in self._mode:
self._feeding = False
self._file.read()
retcode = self.process.poll()
if retcode is None:
# still running
self._process_explicitly_terminated = True
self.process.terminate()
check_allowed_code_and_message = True
self.process.wait()
self._feeding = False
self._file.read()
if self.in_thread:
self.in_thread.join()
self._file.close()
Expand Down

0 comments on commit 74642c5

Please sign in to comment.