Skip to content

fix: close websocket cleanly on session id mismatch#1148

Open
gretta-bot wants to merge 1 commit intowidgetti:masterfrom
gretta-bot:fix/session-id-mismatch-websocket-close
Open

fix: close websocket cleanly on session id mismatch#1148
gretta-bot wants to merge 1 commit intowidgetti:masterfrom
gretta-bot:fix/session-id-mismatch-websocket-close

Conversation

@gretta-bot
Copy link

Problem

When a session ID mismatch occurs during kernel reuse (e.g., browser reconnects with stale credentials), the code sends a plain text error message via websocket.send_text():

websocket.send_text("Session id mismatch when reusing kernel (hack attempt?)")

However, the JupyterLab kernel protocol expects all WebSocket messages to be JSON. The frontend's @jupyterlab/services package tries to parse this message as JSON in serialize.js:

value = JSON.parse(data);  // Fails: "Session id mismatch..." is not valid JSON

This results in the error:

SyntaxError: Kernel message validation error: Unexpected token 'S', "Session id"... is not valid JSON

Solution

Replace send_text() with close() to cleanly close the WebSocket connection without sending a malformed message. The error is already logged server-side, and the client handles WebSocket closes gracefully.

Testing

This was discovered via Sentry errors in production. The fix ensures the client no longer receives protocol-violating messages.

When a session ID mismatch occurs during kernel reuse, the code was sending
a plain text error message via websocket.send_text(). However, the JupyterLab
kernel protocol expects all WebSocket messages to be JSON. This caused a
SyntaxError in the frontend when JSON.parse() tried to parse the plain text.

The fix replaces send_text() with close() to cleanly close the WebSocket
connection without sending a malformed message. The error is already logged
on the server side, and the client will handle the WebSocket close gracefully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant