Fix twisted websocket close after a client-initiated close - #48
Merged
Merged
Conversation
Since the client's close event is queued before the reactor echoes it and finishes the request, the application can close the websocket while wsproto is already CLOSED but the request is not finished yet, which raised a LocalProtocolError. wsSend is now a no-op once a close was sent. 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
Since #46,
WebSocketChannel.dataReceivedqueues the client'sCloseConnectionevent first, then completes the closing handshake on the reactor thread: it echoes the close frame (wsproto becomesCLOSED) and then finishes the request. The application thread can read the queued event in between, and when it closes the websocket (explicitly, or by leavingwith request.accept() as ws:),wsClosesends aCloseConnectionwhile the request is not finished yet.wsSendonly returns early onrequest.finished, so wsproto raises:Before #46 the event was queued after the request was finished, which hid the race. It is timing-dependent: in LocalStack it showed up in CI (Docker) after bumping to rolo 0.8.5, where the endpoint failed right after a client close, the
$disconnectroute was skipped and the request was logged asWEBSOCKET /dev => 500.Changes
WebSocketChannel.wsSendis a no-op once wsproto isLOCAL_CLOSINGorCLOSED, so closing (or sending) after the closing handshake has started does not raise. The surface the client's websocket close code and reason #46 ordering is unchanged: the client's close event, with its code and reason, still comes first.app_sendonce the stream is closed and swallows wsproto'sLocalProtocolError.Testing
tests/serving/test_twisted.pydriveWebSocketChannelwith a request stand-in that stays unfinished, which freezes the channel in the race window:test_websocket_close_after_client_close_before_request_finished: the client's close frame is received and echoed, the app getsWebSocketDisconnectedError(4001, "bye"), thenclose()twice must not raise nor write anything else.test_websocket_close_twice_before_request_finished: a secondclose()(and asend()) after a server-initiated close is a no-op.test_server_close_after_client_close(twisted): the reactor'sclose()is held until the app has closed the websocket explicitly and via thewithexit, and the app must complete without error.LocalProtocolError: Event CloseConnection(code=1000, reason=None) cannot be sent in state ConnectionState.CLOSED., resp.LOCAL_CLOSING) and pass with it.🤖 Generated with Claude Code