@@ -338,6 +338,14 @@ async def shell_handler(self, shell_socket, wire_msg):
338
338
await self .send (self .iopub_socket , "execute_input" , content , parent_header = msg ["header" ])
339
339
340
340
code = msg ["content" ]["code" ]
341
+ #
342
+ # replace VSCode initialization code, which depend on iPython % extensions
343
+ #
344
+ if code .startswith ("%config " ):
345
+ code = "None"
346
+ if code .startswith ("_rwho_ls = %who_ls" ):
347
+ code = "print([])"
348
+
341
349
self .ast_ctx .parse (code )
342
350
exc = self .ast_ctx .get_exception_obj ()
343
351
if exc is None :
@@ -415,6 +423,15 @@ async def shell_handler(self, shell_socket, wire_msg):
415
423
if msg ["content" ].get ("store_history" , True ):
416
424
self .execution_count += 1
417
425
426
+ #
427
+ # Make sure stdout gets sent before set report execution_state idle on iopub,
428
+ # otherwise VSCode doesn't display stdout. We do a handshake with the
429
+ # housekeep task to ensure any queued messages get processed.
430
+ #
431
+ handshake_q = asyncio .Queue (0 )
432
+ await self .housekeep_q .put (["handshake" , handshake_q , 0 ])
433
+ await handshake_q .get ()
434
+
418
435
elif msg ["header" ]["msg_type" ] == "kernel_info_request" :
419
436
content = {
420
437
"protocol_version" : "5.3" ,
@@ -566,7 +583,7 @@ async def control_listen(self, reader, writer):
566
583
await self .housekeep_q .put (["shutdown" ])
567
584
except asyncio .CancelledError :
568
585
raise
569
- except EOFError :
586
+ except ( EOFError , ConnectionResetError ) :
570
587
_LOGGER .debug ("control_listen got eof" )
571
588
await self .housekeep_q .put (["unregister" , "control" , asyncio .current_task ()])
572
589
control_socket .close ()
@@ -586,7 +603,7 @@ async def stdin_listen(self, reader, writer):
586
603
# _LOGGER.debug("stdin_listen received %s", _)
587
604
except asyncio .CancelledError :
588
605
raise
589
- except EOFError :
606
+ except ( EOFError , ConnectionResetError ) :
590
607
_LOGGER .debug ("stdin_listen got eof" )
591
608
await self .housekeep_q .put (["unregister" , "stdin" , asyncio .current_task ()])
592
609
stdin_socket .close ()
@@ -607,7 +624,7 @@ async def shell_listen(self, reader, writer):
607
624
except asyncio .CancelledError :
608
625
shell_socket .close ()
609
626
raise
610
- except EOFError :
627
+ except ( EOFError , ConnectionResetError ) :
611
628
_LOGGER .debug ("shell_listen got eof" )
612
629
await self .housekeep_q .put (["unregister" , "shell" , asyncio .current_task ()])
613
630
shell_socket .close ()
@@ -628,7 +645,7 @@ async def heartbeat_listen(self, reader, writer):
628
645
await heartbeat_socket .send (msg )
629
646
except asyncio .CancelledError :
630
647
raise
631
- except EOFError :
648
+ except ( EOFError , ConnectionResetError ) :
632
649
_LOGGER .debug ("heartbeat_listen got eof" )
633
650
await self .housekeep_q .put (["unregister" , "heartbeat" , asyncio .current_task ()])
634
651
heartbeat_socket .close ()
@@ -649,7 +666,7 @@ async def iopub_listen(self, reader, writer):
649
666
# _LOGGER.debug("iopub received %s", _)
650
667
except asyncio .CancelledError :
651
668
raise
652
- except EOFError :
669
+ except ( EOFError , ConnectionResetError ) :
653
670
await self .housekeep_q .put (["unregister" , "iopub" , asyncio .current_task ()])
654
671
iopub_socket .close ()
655
672
self .iopub_socket .discard (iopub_socket )
@@ -667,8 +684,14 @@ async def housekeep_run(self):
667
684
content = {"name" : "stdout" , "text" : msg [1 ] + "\n " }
668
685
if self .iopub_socket :
669
686
await self .send (
670
- self .iopub_socket , "stream" , content , parent_header = self .parent_header ,
687
+ self .iopub_socket ,
688
+ "stream" ,
689
+ content ,
690
+ parent_header = self .parent_header ,
691
+ identities = [b"stream.stdout" ],
671
692
)
693
+ elif msg [0 ] == "handshake" :
694
+ await msg [1 ].put (msg [2 ])
672
695
elif msg [0 ] == "register" :
673
696
if msg [1 ] not in self .tasks :
674
697
self .tasks [msg [1 ]] = set ()
0 commit comments