keep the websocket close code and reason on WebSocket - #47
Merged
Merged
Conversation
WebSocket.receive now records the code and reason of the first disconnect in close_code / close_reason, so they are still available after the iterator ended silently on disconnect. Also removes the redundant twisted/wsproto import test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Motivation
Follow-up to #46.
WebSocket.__iter__ends silently when the client disconnects, which is the intended behavior forfor msg in wsloops, but it swallows theWebSocketDisconnectedErroralong with the close code and reason. Consumers that need them (e.g. LocalStack's API Gateway$disconnectroute, which reportsdisconnectStatusCode/disconnectReason) have to drop the iterator and handlereceive()+ the exception manually.Changes
WebSocketgetsclose_codeandclose_reasonattributes (similar to thewebsocketslibrary),Noneuntil a disconnect is seen.WebSocket.receiverecords the code and reason of the firstWebSocketDisconnectedErrorbefore re-raising it. Later calls only see the server's internal close event (code1005, no reason), so they don't overwrite it.test_twisted_gateway_import_and_wsproto_dependency(added in Add missing wsproto dependency and twisted extra #44): every twisted test already importsrolo.serving.twisted, which importswsproto, so it didn't check anything extra.Testing
test_close_code_and_reason_after_iter: the client sends a message and closes with4001/"test reason". After the server-sideforloop ends,ws.close_code/ws.close_reasonmust hold them. It fails without the change. Like the test from surface the client's websocket close code and reason #46, it runs against twisted only, because hypercorn doesn't forward the client's close code or reason.🤖 Generated with Claude Code