@@ -171,17 +171,19 @@ async def _add_kernel_when_ready(
171
171
try :
172
172
await kernel_awaitable
173
173
self ._kernels [kernel_id ] = km
174
- finally :
175
174
self ._pending_kernels .pop (kernel_id , None )
175
+ except Exception as e :
176
+ self .log .exception (e )
176
177
177
178
async def _remove_kernel_when_ready (
178
179
self , kernel_id : str , kernel_awaitable : t .Awaitable
179
180
) -> None :
180
181
try :
181
182
await kernel_awaitable
182
183
self .remove_kernel (kernel_id )
183
- finally :
184
184
self ._pending_kernels .pop (kernel_id , None )
185
+ except Exception as e :
186
+ self .log .exception (e )
185
187
186
188
def _using_pending_kernels (self ):
187
189
"""Returns a boolean; a clearer method for determining if
@@ -224,22 +226,6 @@ async def _async_start_kernel(self, kernel_name: t.Optional[str] = None, **kwarg
224
226
225
227
start_kernel = run_sync (_async_start_kernel )
226
228
227
- async def _shutdown_kernel_when_ready (
228
- self ,
229
- kernel_id : str ,
230
- now : t .Optional [bool ] = False ,
231
- restart : t .Optional [bool ] = False ,
232
- ) -> None :
233
- """Wait for a pending kernel to be ready
234
- before shutting the kernel down.
235
- """
236
- # Only do this if using pending kernels
237
- if self ._using_pending_kernels ():
238
- kernel = self ._kernels [kernel_id ]
239
- await kernel .ready
240
- # Once out of a pending state, we can call shutdown.
241
- await ensure_async (self .shutdown_kernel (kernel_id , now = now , restart = restart ))
242
-
243
229
async def _async_shutdown_kernel (
244
230
self ,
245
231
kernel_id : str ,
@@ -258,11 +244,8 @@ async def _async_shutdown_kernel(
258
244
Will the kernel be restarted?
259
245
"""
260
246
self .log .info ("Kernel shutdown: %s" % kernel_id )
261
- # If we're using pending kernels, block shutdown when a kernel is pending.
262
- if self ._using_pending_kernels () and kernel_id in self ._pending_kernels :
263
- raise RuntimeError ("Kernel is in a pending state. Cannot shutdown." )
264
247
# If the kernel is still starting, wait for it to be ready.
265
- elif kernel_id in self ._pending_kernels :
248
+ if kernel_id in self ._pending_kernels :
266
249
kernel = self ._pending_kernels [kernel_id ]
267
250
try :
268
251
await kernel
@@ -320,13 +303,17 @@ async def _async_shutdown_all(self, now: bool = False) -> None:
320
303
"""Shutdown all kernels."""
321
304
kids = self .list_kernel_ids ()
322
305
kids += list (self ._pending_kernels )
323
- futs = [ensure_async (self ._shutdown_kernel_when_ready (kid , now = now )) for kid in set (kids )]
306
+ kms = list (self ._kernels .values ())
307
+ futs = [ensure_async (self .shutdown_kernel (kid , now = now )) for kid in set (kids )]
324
308
await asyncio .gather (* futs )
325
- # When using "shutdown all", all pending kernels
326
- # should be awaited before exiting this method.
309
+ # If using pending kernels, the kernels will not have been fully shut down.
327
310
if self ._using_pending_kernels ():
328
- for km in self ._kernels .values ():
329
- await km .ready
311
+ for km in kms :
312
+ try :
313
+ await km .ready
314
+ except Exception as e :
315
+ # Will have been logged in _add_kernel_when_ready
316
+ pass
330
317
331
318
shutdown_all = run_sync (_async_shutdown_all )
332
319
0 commit comments