From 77f67b4492df5770057c620a29085e5984814f44 Mon Sep 17 00:00:00 2001 From: Boyuan Deng <1998dbyctw@gmail.com> Date: Thu, 15 May 2025 09:22:15 -0700 Subject: [PATCH 1/5] Working --- ipykernel/kernelbase.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index 01539fd22..d682525b7 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -384,6 +384,7 @@ async def dispatch_shell(self, msg): self.log.debug(" Content: %s\n --->\n ", msg["content"]) if not self.should_handle(self.shell_stream, msg, idents): + self._publish_status("idle", "shell") return handler = self.shell_handlers.get(msg_type, None) From 077d7f05426fe46ad15fac63b1879c2cfd11a3c8 Mon Sep 17 00:00:00 2001 From: Boyuan Deng <1998dbyctw@gmail.com> Date: Thu, 15 May 2025 10:31:00 -0700 Subject: [PATCH 2/5] Addressing Jason's comments. --- ipykernel/kernelbase.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index d682525b7..37ba742f2 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -292,6 +292,13 @@ def __init__(self, **kwargs): self._do_exec_accepted_params = _accepts_parameters( self.do_execute, ["cell_meta", "cell_id"] ) + + def _publish_status_and_flush(self, status, channel, stream): + self._publish_status(status, channel) + # flush to ensure reply is sent before + # handling the next request + if stream: + stream.flush(zmq.POLLOUT) async def dispatch_control(self, msg): # Ensure only one control message is processed at a time @@ -331,10 +338,7 @@ async def process_control(self, msg): sys.stdout.flush() sys.stderr.flush() - self._publish_status("idle", "control") - # flush to ensure reply is sent - if self.control_stream: - self.control_stream.flush(zmq.POLLOUT) + self._publish_status_and_flush("idle", "control", self.control_stream) def should_handle(self, stream, msg, idents): """Check whether a shell-channel message should be handled @@ -370,11 +374,7 @@ async def dispatch_shell(self, msg): # Only abort execute requests if self._aborting and msg_type == "execute_request": self._send_abort_reply(self.shell_stream, msg, idents) - self._publish_status("idle", "shell") - # flush to ensure reply is sent before - # handling the next request - if self.shell_stream: - self.shell_stream.flush(zmq.POLLOUT) + self._publish_status_and_flush("idle", "shell", self.shell_stream) return # Print some info about this message and leave a '--->' marker, so it's @@ -384,7 +384,7 @@ async def dispatch_shell(self, msg): self.log.debug(" Content: %s\n --->\n ", msg["content"]) if not self.should_handle(self.shell_stream, msg, idents): - self._publish_status("idle", "shell") + self._publish_status_and_flush("idle", "shell", self.shell_stream) return handler = self.shell_handlers.get(msg_type, None) @@ -413,11 +413,7 @@ async def dispatch_shell(self, msg): sys.stdout.flush() sys.stderr.flush() - self._publish_status("idle", "shell") - # flush to ensure reply is sent before - # handling the next request - if self.shell_stream: - self.shell_stream.flush(zmq.POLLOUT) + self._publish_status_and_flush("idle", "shell", self.shell_stream) def pre_handler_hook(self): """Hook to execute before calling message handler""" From 9515620fb7aa1e6aab4c3fbe9a6de4476cc88a05 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 15 May 2025 17:31:20 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ipykernel/kernelbase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index 37ba742f2..651dc79fc 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -292,7 +292,7 @@ def __init__(self, **kwargs): self._do_exec_accepted_params = _accepts_parameters( self.do_execute, ["cell_meta", "cell_id"] ) - + def _publish_status_and_flush(self, status, channel, stream): self._publish_status(status, channel) # flush to ensure reply is sent before From be199a4bd1e351542df68cb43c348b8580e52a80 Mon Sep 17 00:00:00 2001 From: Boyuan Deng <1998dbyctw@gmail.com> Date: Thu, 15 May 2025 10:51:20 -0700 Subject: [PATCH 4/5] Adding Docstring to the function. --- ipykernel/kernelbase.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index 37ba742f2..c3660d93b 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -292,13 +292,6 @@ def __init__(self, **kwargs): self._do_exec_accepted_params = _accepts_parameters( self.do_execute, ["cell_meta", "cell_id"] ) - - def _publish_status_and_flush(self, status, channel, stream): - self._publish_status(status, channel) - # flush to ensure reply is sent before - # handling the next request - if stream: - stream.flush(zmq.POLLOUT) async def dispatch_control(self, msg): # Ensure only one control message is processed at a time @@ -599,6 +592,12 @@ def _publish_status(self, status, channel, parent=None): parent=parent or self.get_parent(channel), ident=self._topic("status"), ) + + def _publish_status_and_flush(self, status, channel, stream, parent=None): + """send status on IOPub and flush specified stream to ensure reply is sent before handling the next reply""" + self._publish_status(status, channel, parent) + if stream: + stream.flush(zmq.POLLOUT) def _publish_debug_event(self, event): if not self.session: From 64e1c2a35a2403bb6f378405a65b8e053333864f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 15 May 2025 17:53:14 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ipykernel/kernelbase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index c3660d93b..cb440199e 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -592,7 +592,7 @@ def _publish_status(self, status, channel, parent=None): parent=parent or self.get_parent(channel), ident=self._topic("status"), ) - + def _publish_status_and_flush(self, status, channel, stream, parent=None): """send status on IOPub and flush specified stream to ensure reply is sent before handling the next reply""" self._publish_status(status, channel, parent)