Skip to content

Add transport.is_closing() guard before send in websockets implementation#2861

Closed
hrv-dys wants to merge 5 commits intoKludex:mainfrom
hrv-dys:fix/websockets-impl-transport-closing-check
Closed

Add transport.is_closing() guard before send in websockets implementation#2861
hrv-dys wants to merge 5 commits intoKludex:mainfrom
hrv-dys:fix/websockets-impl-transport-closing-check

Conversation

@hrv-dys
Copy link
Copy Markdown
Contributor

@hrv-dys hrv-dys commented Mar 18, 2026

Summary

The websockets_impl.py asgi_send method writes data via await self.send()
without first checking whether the transport is closing. The other two
WebSocket implementations — wsproto_impl.py and websockets_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 ClientDisconnected to give the ASGI app a clean signal
that 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.py passes

…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.
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 18, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing hrv-dys:fix/websockets-impl-transport-closing-check (45718d1) with main (02bed6f)

Open in CodSpeed

@Kludex Kludex closed this Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants