Skip to content

Commit 64ff5d4

Browse files
committed
avoid shadowing context
instead, keep same context but use `socket_class` kwarg to specify socket classes shadow context prevents cleanup of untracked sockets via ctx.destroy because it disconnects socket bookkeeping
1 parent 3b1188a commit 64ff5d4

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

ipykernel/iostream.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ def __init__(self, socket, pipe=False):
9292
# ensure all of our sockets as sync zmq.Sockets
9393
# don't create async wrappers until we are within the appropriate coroutines
9494
self.socket: zmq.Socket[bytes] | None = zmq.Socket(socket)
95-
self._sync_context: zmq.Context[zmq.Socket[bytes]] = zmq.Context(socket.context)
95+
if self.socket.context is None:
96+
# bug in pyzmq, shadow socket doesn't always inherit context attribute
97+
self.socket.context = socket.context
98+
self._context = socket.context
9699

97100
self.background_socket = BackgroundSocket(self)
98101
self._main_pid = os.getpid()
@@ -112,8 +115,7 @@ def __init__(self, socket, pipe=False):
112115

113116
def _setup_event_pipe(self):
114117
"""Create the PULL socket listening for events that should fire in this thread."""
115-
ctx = self._sync_context
116-
self._pipe_in0 = ctx.socket(zmq.PULL)
118+
self._pipe_in0 = self._context.socket(zmq.PULL, socket_class=zmq.Socket)
117119
self._pipe_in0.linger = 0
118120

119121
_uuid = b2a_hex(os.urandom(16)).decode("ascii")
@@ -147,7 +149,8 @@ def _event_pipe(self):
147149
event_pipe = self._local.event_pipe
148150
except AttributeError:
149151
# new thread, new event pipe
150-
event_pipe = self._sync_context.socket(zmq.PUSH)
152+
# create sync base socket
153+
event_pipe = self._context.socket(zmq.PUSH, socket_class=zmq.Socket)
151154
event_pipe.linger = 0
152155
event_pipe.connect(self._event_interface)
153156
self._local.event_pipe = event_pipe
@@ -184,12 +187,12 @@ async def _handle_event(self):
184187

185188
def _setup_pipe_in(self):
186189
"""setup listening pipe for IOPub from forked subprocesses"""
187-
ctx = self._sync_context
190+
ctx = self._context
188191

189192
# use UUID to authenticate pipe messages
190193
self._pipe_uuid = os.urandom(16)
191194

192-
self._pipe_in1 = ctx.socket(zmq.PULL)
195+
self._pipe_in1 = ctx.socket(zmq.PULL, socket_class=zmq.Socket)
193196
self._pipe_in1.linger = 0
194197

195198
try:

0 commit comments

Comments
 (0)