@@ -383,15 +383,16 @@ export abstract class ManagerBase implements IWidgetManager {
383383 // Try fetching all widget states through the control comm
384384 let data : any ;
385385 let buffers : any ;
386+ let timeoutID : number | undefined ;
386387 try {
387388 const initComm = await this . _create_comm (
388389 CONTROL_COMM_TARGET ,
389390 uuid ( ) ,
390391 { } ,
391392 { version : CONTROL_COMM_PROTOCOL_VERSION }
392393 ) ;
393-
394394 await new Promise ( ( resolve , reject ) => {
395+ let succeeded = false ;
395396 initComm . on_msg ( ( msg : any ) => {
396397 data = msg [ 'content' ] [ 'data' ] ;
397398
@@ -409,28 +410,31 @@ export abstract class ManagerBase implements IWidgetManager {
409410 return new DataView ( b instanceof ArrayBuffer ? b : b . buffer ) ;
410411 }
411412 } ) ;
412-
413+ succeeded = true ;
414+ clearTimeout ( timeoutID ) ;
413415 resolve ( null ) ;
414416 } ) ;
415417
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+ } ) ;
417421
418422 // Send a states request msg
419423 initComm . send ( { method : 'request_states' } , { } ) ;
420424
421425 // Reject if we didn't get a response in time
422- setTimeout (
426+ timeoutID = window . setTimeout (
423427 ( ) => reject ( 'Control comm did not respond in time' ) ,
424428 CONTROL_COMM_TIMEOUT
425429 ) ;
426430 } ) ;
427-
428431 initComm . close ( ) ;
429432 } catch ( error ) {
430433 console . warn (
431434 'Failed to fetch ipywidgets through the "jupyter.widget.control" comm channel, fallback to fetching individual model state. Reason:' ,
432435 error
433436 ) ;
437+ clearTimeout ( timeoutID ) ;
434438 // Fall back to the old implementation for old ipywidgets backend versions (ipywidgets<=7.6)
435439 return this . _loadFromKernelModels ( ) ;
436440 }
0 commit comments