Skip to content

Commit

Permalink
Fall back to legacy protocol if selected_subprotocol raises exception (
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart authored Mar 11, 2022
1 parent 05baa15 commit fb25932
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions jupyter_server/services/kernels/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"])


Expand Down

0 comments on commit fb25932

Please sign in to comment.