From fb25932ada7df6e8c78e70a1696831ac8cbb3057 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Fri, 11 Mar 2022 18:47:13 +0100 Subject: [PATCH] Fall back to legacy protocol if selected_subprotocol raises exception (#706) --- jupyter_server/services/kernels/handlers.py | 22 ++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/jupyter_server/services/kernels/handlers.py b/jupyter_server/services/kernels/handlers.py index 84d14fd26e..d1eaf5054e 100644 --- a/jupyter_server/services/kernels/handlers.py +++ b/jupyter_server/services/kernels/handlers.py @@ -140,6 +140,14 @@ def iopub_data_rate_limit(self): def rate_limit_window(self): return self.settings.get("rate_limit_window", 1.0) + @property + def subprotocol(self): + try: + protocol = self.selected_subprotocol + except Exception: + protocol = None + return protocol + def __repr__(self): return "%s(%s)" % ( self.__class__.__name__, @@ -484,7 +492,7 @@ def on_message(self, ws_msg): self.log.debug("Received message on closed websocket %r", ws_msg) return - if self.selected_subprotocol == "v1.kernel.websocket.jupyter.org": + if self.subprotocol == "v1.kernel.websocket.jupyter.org": channel, msg_list = deserialize_msg_from_ws_v1(ws_msg) msg = { "header": None, @@ -515,7 +523,7 @@ def on_message(self, ws_msg): ignore_msg = True if not ignore_msg: stream = self.channels[channel] - if self.selected_subprotocol == "v1.kernel.websocket.jupyter.org": + if self.subprotocol == "v1.kernel.websocket.jupyter.org": self.session.send_raw(stream, msg_list) else: self.session.send(stream, msg) @@ -533,7 +541,7 @@ def get_part(self, field, value, msg_list): def _on_zmq_reply(self, stream, msg_list): idents, fed_msg_list = self.session.feed_identities(msg_list) - if self.selected_subprotocol == "v1.kernel.websocket.jupyter.org": + if self.subprotocol == "v1.kernel.websocket.jupyter.org": msg = {"header": None, "parent_header": None, "content": None} else: msg = self.session.deserialize(fed_msg_list) @@ -546,7 +554,7 @@ def _on_zmq_reply(self, stream, msg_list): if self._limit_rate(channel, msg, parts): return - if self.selected_subprotocol == "v1.kernel.websocket.jupyter.org": + if self.subprotocol == "v1.kernel.websocket.jupyter.org": super(ZMQChannelsHandler, self)._on_zmq_reply(stream, parts) else: super(ZMQChannelsHandler, self)._on_zmq_reply(stream, msg) @@ -558,7 +566,7 @@ def write_stderr(self, error_message, parent_header): content={"text": error_message + "\n", "name": "stderr"}, parent=parent_header, ) - if self.selected_subprotocol == "v1.kernel.websocket.jupyter.org": + if self.subprotocol == "v1.kernel.websocket.jupyter.org": bin_msg = serialize_msg_to_ws_v1(err_msg, "iopub", self.session.pack) self.write_message(bin_msg, binary=True) else: @@ -736,7 +744,7 @@ def _send_status_message(self, status): # that all messages from the stopped kernel have been delivered iopub.flush() msg = self.session.msg("status", {"execution_state": status}) - if self.selected_subprotocol == "v1.kernel.websocket.jupyter.org": + if self.subprotocol == "v1.kernel.websocket.jupyter.org": bin_msg = serialize_msg_to_ws_v1(msg, "iopub", self.session.pack) self.write_message(bin_msg, binary=True) else: @@ -762,7 +770,7 @@ def _on_error(self, channel, msg, msg_list): msg["content"]["ename"] = "ExecutionError" msg["content"]["evalue"] = "Execution error" msg["content"]["traceback"] = [self.kernel_manager.traceback_replacement_message] - if self.selected_subprotocol == "v1.kernel.websocket.jupyter.org": + if self.subprotocol == "v1.kernel.websocket.jupyter.org": msg_list[3] = self.session.pack(msg["content"])