Skip to content

Commit

Permalink
Fix Exception in OutStream.close() (#1076)
Browse files Browse the repository at this point in the history
This bug fixes an Exception which was thrown
in OutStream.close() whenever the OutStream was
constructed with watchfd=False.

A regression test was also added to test_io.py.

Co-authored-by: Ilya Sherstyuk <isherstyuk@nvidia.com>
  • Loading branch information
ilyasher and ilyasher authored Jan 13, 2023
1 parent b4ea598 commit 203ee2b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ipykernel/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def __init__(
self._buffer = StringIO()
self.echo = None
self._isatty = bool(isatty)
self._should_watch = False

if (
watchfd
Expand Down Expand Up @@ -449,7 +450,7 @@ def set_parent(self, parent):

def close(self):
"""Close the stream."""
if sys.platform.startswith("linux") or sys.platform.startswith("darwin"):
if self._should_watch:
self._should_watch = False
self.watch_fd_thread.join()
if self._exc:
Expand Down
3 changes: 3 additions & 0 deletions ipykernel/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def test_outstream():
stream = OutStream(session, pub, "stdout")
stream = OutStream(session, thread, "stdout", pipe=object())

stream = OutStream(session, thread, "stdout", watchfd=False)
stream.close()

stream = OutStream(session, thread, "stdout", isatty=True, echo=io.StringIO())
with pytest.raises(io.UnsupportedOperation):
stream.fileno()
Expand Down

0 comments on commit 203ee2b

Please sign in to comment.