Skip to content

Commit c286b69

Browse files
committed
for VSCode, ensure stdout is flushed before we send execution_state=idle
1 parent 666e3fc commit c286b69

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

custom_components/pyscript/jupyter_kernel.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,14 @@ async def shell_handler(self, shell_socket, wire_msg):
338338
await self.send(self.iopub_socket, "execute_input", content, parent_header=msg["header"])
339339

340340
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+
341349
self.ast_ctx.parse(code)
342350
exc = self.ast_ctx.get_exception_obj()
343351
if exc is None:
@@ -415,6 +423,15 @@ async def shell_handler(self, shell_socket, wire_msg):
415423
if msg["content"].get("store_history", True):
416424
self.execution_count += 1
417425

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+
418435
elif msg["header"]["msg_type"] == "kernel_info_request":
419436
content = {
420437
"protocol_version": "5.3",
@@ -566,7 +583,7 @@ async def control_listen(self, reader, writer):
566583
await self.housekeep_q.put(["shutdown"])
567584
except asyncio.CancelledError:
568585
raise
569-
except EOFError:
586+
except (EOFError, ConnectionResetError):
570587
_LOGGER.debug("control_listen got eof")
571588
await self.housekeep_q.put(["unregister", "control", asyncio.current_task()])
572589
control_socket.close()
@@ -586,7 +603,7 @@ async def stdin_listen(self, reader, writer):
586603
# _LOGGER.debug("stdin_listen received %s", _)
587604
except asyncio.CancelledError:
588605
raise
589-
except EOFError:
606+
except (EOFError, ConnectionResetError):
590607
_LOGGER.debug("stdin_listen got eof")
591608
await self.housekeep_q.put(["unregister", "stdin", asyncio.current_task()])
592609
stdin_socket.close()
@@ -607,7 +624,7 @@ async def shell_listen(self, reader, writer):
607624
except asyncio.CancelledError:
608625
shell_socket.close()
609626
raise
610-
except EOFError:
627+
except (EOFError, ConnectionResetError):
611628
_LOGGER.debug("shell_listen got eof")
612629
await self.housekeep_q.put(["unregister", "shell", asyncio.current_task()])
613630
shell_socket.close()
@@ -628,7 +645,7 @@ async def heartbeat_listen(self, reader, writer):
628645
await heartbeat_socket.send(msg)
629646
except asyncio.CancelledError:
630647
raise
631-
except EOFError:
648+
except (EOFError, ConnectionResetError):
632649
_LOGGER.debug("heartbeat_listen got eof")
633650
await self.housekeep_q.put(["unregister", "heartbeat", asyncio.current_task()])
634651
heartbeat_socket.close()
@@ -649,7 +666,7 @@ async def iopub_listen(self, reader, writer):
649666
# _LOGGER.debug("iopub received %s", _)
650667
except asyncio.CancelledError:
651668
raise
652-
except EOFError:
669+
except (EOFError, ConnectionResetError):
653670
await self.housekeep_q.put(["unregister", "iopub", asyncio.current_task()])
654671
iopub_socket.close()
655672
self.iopub_socket.discard(iopub_socket)
@@ -667,8 +684,14 @@ async def housekeep_run(self):
667684
content = {"name": "stdout", "text": msg[1] + "\n"}
668685
if self.iopub_socket:
669686
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"],
671692
)
693+
elif msg[0] == "handshake":
694+
await msg[1].put(msg[2])
672695
elif msg[0] == "register":
673696
if msg[1] not in self.tasks:
674697
self.tasks[msg[1]] = set()

0 commit comments

Comments
 (0)