@@ -383,15 +383,16 @@ export abstract class ManagerBase implements IWidgetManager {
383
383
// Try fetching all widget states through the control comm
384
384
let data : any ;
385
385
let buffers : any ;
386
+ let timeoutID : number | undefined ;
386
387
try {
387
388
const initComm = await this . _create_comm (
388
389
CONTROL_COMM_TARGET ,
389
390
uuid ( ) ,
390
391
{ } ,
391
392
{ version : CONTROL_COMM_PROTOCOL_VERSION }
392
393
) ;
393
-
394
394
await new Promise ( ( resolve , reject ) => {
395
+ let succeeded = false ;
395
396
initComm . on_msg ( ( msg : any ) => {
396
397
data = msg [ 'content' ] [ 'data' ] ;
397
398
@@ -409,28 +410,31 @@ export abstract class ManagerBase implements IWidgetManager {
409
410
return new DataView ( b instanceof ArrayBuffer ? b : b . buffer ) ;
410
411
}
411
412
} ) ;
412
-
413
+ succeeded = true ;
414
+ clearTimeout ( timeoutID ) ;
413
415
resolve ( null ) ;
414
416
} ) ;
415
417
416
- initComm . on_close ( ( ) => reject ( 'Control comm was closed too early' ) ) ;
418
+ initComm . on_close ( ( ) => {
419
+ if ( ! succeeded ) reject ( 'Control comm was closed too early' ) ;
420
+ } ) ;
417
421
418
422
// Send a states request msg
419
423
initComm . send ( { method : 'request_states' } , { } ) ;
420
424
421
425
// Reject if we didn't get a response in time
422
- setTimeout (
426
+ timeoutID = window . setTimeout (
423
427
( ) => reject ( 'Control comm did not respond in time' ) ,
424
428
CONTROL_COMM_TIMEOUT
425
429
) ;
426
430
} ) ;
427
-
428
431
initComm . close ( ) ;
429
432
} catch ( error ) {
430
433
console . warn (
431
434
'Failed to fetch ipywidgets through the "jupyter.widget.control" comm channel, fallback to fetching individual model state. Reason:' ,
432
435
error
433
436
) ;
437
+ clearTimeout ( timeoutID ) ;
434
438
// Fall back to the old implementation for old ipywidgets backend versions (ipywidgets<=7.6)
435
439
return this . _loadFromKernelModels ( ) ;
436
440
}
0 commit comments