fix: close websocket cleanly on session id mismatch#1148
Open
gretta-bot wants to merge 1 commit intowidgetti:masterfrom
Open
fix: close websocket cleanly on session id mismatch#1148gretta-bot wants to merge 1 commit intowidgetti:masterfrom
gretta-bot wants to merge 1 commit intowidgetti:masterfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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():However, the JupyterLab kernel protocol expects all WebSocket messages to be JSON. The frontend's
@jupyterlab/servicespackage tries to parse this message as JSON inserialize.js:This results in the error:
Solution
Replace
send_text()withclose()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.