Skip to content

Commit f623822

Browse files
committed
stop control thread before closing sockets on it
closing sockets in-use by another thread is a recipe for SIGABRT skip `close(all_fds=True)` in favor of explicit cleanup of sockets
1 parent c772130 commit f623822

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

ipykernel/control.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ def __init__(self, **kwargs):
1515
self.pydev_do_not_trace = True
1616
self.is_pydev_daemon_thread = True
1717

18-
def run(self):
18+
def run(self):
1919
self.io_loop.make_current()
20-
self.io_loop.start()
21-
self.io_loop.close(all_fds=True)
20+
try:
21+
self.io_loop.start()
22+
finally:
23+
self.io_loop.close()
24+
25+
def stop(self):
26+
"""Stop the thread.
27+
28+
This method is threadsafe.
29+
"""
30+
self.io_loop.add_callback(self.io_loop.stop)

ipykernel/kernelapp.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ def close(self):
346346
self.log.debug("Closing iopub channel")
347347
self.iopub_thread.stop()
348348
self.iopub_thread.close()
349+
if self.control_thread and self.control_thread.is_alive():
350+
self.log.debug("Closing control thread")
351+
self.control_thread.stop()
352+
self.control_thread.join()
349353

350354
if self.debugpy_socket and not self.debugpy_socket.closed:
351355
self.debugpy_socket.close()
@@ -487,7 +491,7 @@ def init_kernel(self):
487491
control_stream=control_stream,
488492
debugpy_stream=debugpy_stream,
489493
debug_shell_socket=self.debug_shell_socket,
490-
shell_stream=shell_stream,
494+
shell_stream=shell_stream,
491495
control_thread=self.control_thread,
492496
iopub_thread=self.iopub_thread,
493497
iopub_socket=self.iopub_socket,

0 commit comments

Comments
 (0)