Skip to content

Commit 270cec6

Browse files
committed
more cleanup
1 parent 0a2c228 commit 270cec6

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

jupyter_client/multikernelmanager.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,7 @@ def _starting_kernels(self):
109109
def _context_default(self) -> zmq.Context:
110110
context = zmq.Context()
111111
# Use a finalizer to destroy the context.
112-
# self._finalizer = weakref.finalize(self, context.destroy)
113-
114-
raise ValueError('Notes here')
115-
"""
116-
The finalizer hangs because the context can't be destroyed.
117-
There are two other things that are probably contributing:
118-
- There is an open kernel subprocess at shutdown that is raising a warning on __del__
119-
- We are not properly canceling the _add_kernel_when_ready task
120-
"""
112+
self._finalizer = weakref.finalize(self, context.destroy)
121113
return context
122114

123115
connection_dir = Unicode("")
@@ -248,12 +240,14 @@ async def _async_shutdown_kernel(
248240
await task
249241
km = self.get_kernel(kernel_id)
250242
await t.cast(asyncio.Future, km.ready)
243+
except asyncio.exceptions.CancelledError:
244+
pass
251245
except Exception:
252246
self.remove_kernel(kernel_id)
253247
return
254248
km = self.get_kernel(kernel_id)
255249
# If a pending kernel raised an exception, remove it.
256-
if km.ready.exception():
250+
if not km.ready.cancelled() and km.ready.exception():
257251
self.remove_kernel(kernel_id)
258252
return
259253
stopper = ensure_async(km.shutdown_kernel(now, restart))
@@ -310,7 +304,7 @@ async def _async_shutdown_all(self, now: bool = False) -> None:
310304
await km.ready
311305
except asyncio.exceptions.CancelledError:
312306
self._pending_kernels[km.kernel_id].cancel()
313-
except Exception as e:
307+
except Exception:
314308
# Will have been logged in _add_kernel_when_ready
315309
pass
316310

jupyter_client/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ def wrapped(*args, **kwargs):
1414
loop = asyncio.get_running_loop()
1515
except RuntimeError:
1616
# Workaround for bugs.python.org/issue39529.
17-
loop = asyncio.get_event_loop_policy().get_event_loop()
17+
try:
18+
loop = asyncio.get_event_loop_policy().get_event_loop()
19+
except RuntimeError:
20+
loop = asyncio.new_event_loop()
21+
asyncio.set_event_loop(loop)
1822
import nest_asyncio # type: ignore
1923

2024
nest_asyncio.apply(loop)

0 commit comments

Comments
 (0)