Skip to content

Commit ce24932

Browse files
committed
Use transient shell channel, and do not nudge busy kernels
1 parent f716328 commit ce24932

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

notebook/services/kernels/handlers.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ def create_stream(self):
129129
stream.channel = channel
130130

131131
def nudge(self):
132-
shell_channel = self.channels['shell']
132+
# Use a transient shell channel to prevent leaking
133+
# shell responses to the front-end.
134+
shell_channel = kernel.connect_shell()
135+
136+
# The IOPub used by the client.
133137
iopub_channel = self.channels['iopub']
134138

135139
future = Future()
@@ -141,18 +145,20 @@ def finish():
141145
loop.remove_timeout(timeout)
142146
loop.remove_timeout(nudge_handle)
143147
iopub_channel.stop_on_recv()
144-
shell_channel.stop_on_recv()
148+
if not shell_channel.closed():
149+
shell_channel.close()
145150

146151
def on_shell_reply(msg):
147152
if not info_future.done():
148153
self.log.debug("Nudge: shell info reply received: %s", self.kernel_id)
149-
shell_channel.stop_on_recv()
154+
if not shell_channel.closed():
155+
shell_channel.close()
150156
self.log.debug("Nudge: resolving shell future")
151-
info_future.set_result(msg)
157+
info_future.set_result(None)
152158
if iopub_future.done():
153159
finish()
154160
self.log.debug("Nudge: resolving main future in shell handler")
155-
future.set_result(info_future.result())
161+
future.set_result(None)
156162

157163
def on_iopub(msg):
158164
if not iopub_future.done():
@@ -163,7 +169,7 @@ def on_iopub(msg):
163169
if info_future.done():
164170
finish()
165171
self.log.debug("Nudge: resolving main future in iopub handler")
166-
future.set_result(info_future.result())
172+
future.set_result(None)
167173

168174
def on_timeout():
169175
self.log.warning("Nudge: Timeout waiting for kernel_info_reply: %s", self.kernel_id)
@@ -177,16 +183,18 @@ def on_timeout():
177183

178184
# Nudge the kernel with kernel info requests until we get an IOPub message
179185
def nudge(count):
180-
count += 1
181-
nonlocal nudge_handle
186+
# Do not nudge busy kernels as kernel info requests sent to shell are
187+
# queued behind execution requests.
188+
if self.kernel_manager.get_kernel(self.kernel_id).execution_state == 'busy':
189+
future.set_result(None)
182190
if not future.done():
183191
log = self.log.warning if count % 10 == 0 else self.log.debug
184192
log("Nudging attempt %s on kernel %s" % (count, self.kernel_id))
185193
self.session.send(shell_channel, "kernel_info_request")
194+
nonlocal nudge_handle
186195
nudge_handle = loop.call_later(0.5, nudge, count)
187196

188197
nudge_handle = loop.call_later(0, nudge, count=0)
189-
190198
timeout = loop.add_timeout(loop.time() + self.kernel_info_timeout, on_timeout)
191199
return future
192200

0 commit comments

Comments
 (0)