Skip to content

Commit 74642c5

Browse files
committed
Shutdown process before feeder thread
1 parent d8b9cfb commit 74642c5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/xopen/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def _open_process(self):
267267
# data continuously to the process stdin on another thread.
268268
self.in_thread = threading.Thread(target=self._feed_pipe)
269269
self.in_thread.start()
270+
self._process_explicitly_terminated = False
270271
self._file: BinaryIO = self.process.stdout # type: ignore
271272
self._wait_for_output_or_process_exit()
272273
self._raise_if_error()
@@ -290,7 +291,11 @@ def _feed_pipe(self):
290291
if chunk == b"":
291292
self.in_pipe.close()
292293
return
293-
self.in_pipe.write(chunk)
294+
try:
295+
self.in_pipe.write(chunk)
296+
except BrokenPipeError:
297+
if not self._process_explicitly_terminated:
298+
raise
294299
finally:
295300
self.in_pipe.close()
296301

@@ -329,14 +334,15 @@ def close(self) -> None:
329334
return
330335
check_allowed_code_and_message = False
331336
if "r" in self._mode:
332-
self._feeding = False
333-
self._file.read()
334337
retcode = self.process.poll()
335338
if retcode is None:
336339
# still running
340+
self._process_explicitly_terminated = True
337341
self.process.terminate()
338342
check_allowed_code_and_message = True
339343
self.process.wait()
344+
self._feeding = False
345+
self._file.read()
340346
if self.in_thread:
341347
self.in_thread.join()
342348
self._file.close()

0 commit comments

Comments
 (0)