Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid starting IOPub background thread after it's been stopped #1137

Merged
merged 4 commits into from
Sep 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions ipykernel/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, socket, pipe=False):
piped from subprocesses.
"""
self.socket = socket
self._stopped = False
self.background_socket = BackgroundSocket(self)
self._master_pid = os.getpid()
self._pipe_flag = pipe
Expand All @@ -83,7 +84,12 @@ def _start_event_gc():
self._event_pipe_gc_task = asyncio.ensure_future(self._run_event_pipe_gc())

self.io_loop.run_sync(_start_event_gc)
self.io_loop.start()

if not self._stopped:
# avoid race if stop called before start thread gets here
# probably only comes up in tests
self.io_loop.start()

if self._event_pipe_gc_task is not None:
# cancel gc task to avoid pending task warnings
async def _cancel():
Expand Down Expand Up @@ -219,10 +225,16 @@ def start(self):

def stop(self):
"""Stop the IOPub thread"""
self._stopped = True
if not self.thread.is_alive():
return
self.io_loop.add_callback(self.io_loop.stop)
self.thread.join()

self.thread.join(timeout=30)
if self.thread.is_alive():
# avoid infinite hang if stop fails
msg = "IOPub thread did not terminate in 30 seconds"
raise TimeoutError(msg)
# close *all* event pipes, created in any thread
# event pipes can only be used from other threads while self.thread.is_alive()
# so after thread.join, this should be safe
Expand Down