Skip to content

Commit 690d2f4

Browse files
authored
Wrap the concurrent futures in an asyncio future (#1000)
1 parent e61ba81 commit 690d2f4

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.github/workflows/python-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ jobs:
118118
pip check
119119
- name: Run the tests
120120
run: |
121+
pip install jupyter_client@https://github.com/blink1073/jupyter_client/archive/refs/heads/synchronous_managers.zip
121122
pytest -vv || pytest -vv --lf
122123
123124
make_sdist:

jupyter_server/services/kernels/handlers.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ async def post(self, kernel_id, action):
108108
self.finish()
109109

110110

111+
def _ensure_future(f):
112+
"""Wrap a concurrent future as an asyncio future if there is a running loop."""
113+
try:
114+
asyncio.get_running_loop()
115+
return asyncio.wrap_future(f)
116+
except RuntimeError:
117+
return f
118+
119+
111120
class ZMQChannelsHandler(AuthenticatedZMQStreamHandler):
112121
"""There is one ZMQChannelsHandler per running kernel and it oversees all
113122
the sessions.
@@ -186,7 +195,7 @@ def nudge(self):
186195
self.log.debug("Nudge: not nudging busy kernel %s", self.kernel_id)
187196
f: Future = Future()
188197
f.set_result(None)
189-
return f
198+
return _ensure_future(f)
190199
# Use a transient shell channel to prevent leaking
191200
# shell responses to the front-end.
192201
shell_channel = kernel.connect_shell()
@@ -287,7 +296,7 @@ def nudge(count):
287296
future = gen.with_timeout(loop.time() + self.kernel_info_timeout, both_done)
288297
# ensure we have no dangling resources or unresolved Futures in case of timeout
289298
future.add_done_callback(finish)
290-
return future
299+
return _ensure_future(future)
291300

292301
def request_kernel_info(self):
293302
"""send a request for kernel_info"""
@@ -311,7 +320,7 @@ def request_kernel_info(self):
311320
if not future.done():
312321
self.log.debug("Waiting for pending kernel_info request")
313322
future.add_done_callback(lambda f: self._finish_kernel_info(f.result()))
314-
return self._kernel_info_future
323+
return _ensure_future(self._kernel_info_future)
315324

316325
def _handle_kernel_info_reply(self, msg):
317326
"""process the kernel_info_reply
@@ -704,7 +713,7 @@ def _limit_rate(self, channel, msg, msg_list):
704713

705714
def close(self):
706715
super().close()
707-
return self._close_future
716+
return _ensure_future(self._close_future)
708717

709718
def on_close(self):
710719
self.log.debug("Websocket closed %s", self.session_key)

0 commit comments

Comments
 (0)