Add transport.is_closing() guard before send in websockets implementation#2861
Closed
hrv-dys wants to merge 5 commits intoKludex:mainfrom
Closed
Add transport.is_closing() guard before send in websockets implementation#2861hrv-dys wants to merge 5 commits intoKludex:mainfrom
hrv-dys wants to merge 5 commits intoKludex:mainfrom
Conversation
…tion The websockets_impl.py asgi_send method was missing a check for whether the transport is closing before writing data. Both wsproto_impl.py and websockets_sansio_impl.py already have this guard. Without it, a write to a closing transport can raise an unexpected exception or silently fail. This adds the same is_closing() check, raising ClientDisconnected to give the ASGI app a clean signal that the connection is gone.
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.
Summary
The
websockets_impl.pyasgi_sendmethod writes data viaawait self.send()without first checking whether the transport is closing. The other two
WebSocket implementations —
wsproto_impl.pyandwebsockets_sansio_impl.py— both guard their writes with
self.transport.is_closing()checks.Without this guard, a write to a closing transport can raise an unexpected
exception inside the websockets library, or silently fail. This fix adds the
same check, raising
ClientDisconnectedto give the ASGI app a clean signalthat the connection is gone.
Belief that guided this fix: the three WebSocket implementations
(websockets, wsproto, websockets-sansio) handle protocol states differently,
so you can't assume they'll all behave the same without explicit guards.
Finding this inconsistency between implementations was only possible by
cross-referencing all three side by side.
Test plan
tests/protocols/test_websocket.pypasses