Conversation
The twisted WebSocketChannel queued the client's CloseConnection event only after close() had queued its internal poison pill, so consumers always saw code 1005. Queue the event first, and carry the close reason in WebSocketDisconnectedError (twisted and ASGI adapters). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bentsku
deleted the
aws-1924-rolo-surface-the-clients-websocket-close-code-and-reason
branch
September 30, 2026 13:19
This was referenced Sep 30, 2026
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
When a client closes a WebSocket with a code and reason (e.g.
4001/"test reason"), the application served through the twisted serving layer never sees them.ws.receive()raisesWebSocketDisconnectedErrorwith the default code1005and no reason:WebSocketChannel.dataReceivedcallsself.close()on the client'sCloseConnectionevent before queueing it.close()queues its internal poison pillCloseConnection(None), so the consumer reads the pill first (→1005) and the client's real close event is never read.WebSocketDisconnectedErroronly carries the code, so thereasonis dropped even when the event is read.In LocalStack, API Gateway WebSocket APIs pass the close code and reason to the
$disconnectroute (requestContext.disconnectStatusCode/disconnectReason), which we currently can't populate.Changes
WebSocketChannel.dataReceivedqueues the event before completing the closing handshake, so the client'sCloseConnectioncomes ahead of the poison pill.WebSocketDisconnectedErroraccepts an optionalreason(exposed as.reason, defaults toNone, so this is backwards compatible).event.reason. The ASGI adapter passesevent.get("reason")(the optionalreasonkey onwebsocket.disconnectin the ASGI spec).Testing
test_close_code_and_reason_client_initiated: the client closes with4001/"test reason", and the server-side exception must carry both. It fails without the fix (assert 1005 == 4001).1000/1006without a reason.🤖 Generated with Claude Code