Skip to content

Add transport.is_closing() guard on websocket.send in sansio implementation#2863

Closed
hrv-dys wants to merge 2 commits intoKludex:mainfrom
hrv-dys:fix/sansio-transport-closing-check-on-send
Closed

Add transport.is_closing() guard on websocket.send in sansio implementation#2863
hrv-dys wants to merge 2 commits intoKludex:mainfrom
hrv-dys:fix/sansio-transport-closing-check-on-send

Conversation

@hrv-dys
Copy link
Copy Markdown
Contributor

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

Summary

In websockets_sansio_impl.py, the websocket.send handler writes to the
transport without checking transport.is_closing(), while the websocket.close
handler in the same method (10 lines later) does perform this check. The
wsproto_impl.py implementation consistently checks at both points.

This inconsistency means a websocket.send message arriving while the
transport is closing could result in writing to a half-closed transport.

This fix adds the same is_closing() guard that's already used for
websocket.close in this file and for both message types in wsproto.

Belief that guided this fix: don't assume consistent behavior between
the wsproto and websockets implementations — they handle protocol states
differently. Cross-referencing all three implementations side by side
revealed this inconsistency.

Test plan

  • tests/protocols/test_websocket.py passes

…tation

The websocket.send handler was writing to the transport without checking
transport.is_closing(), while the websocket.close handler 10 lines below
already performed this check. The wsproto implementation consistently
checks at both points. This adds the same guard to prevent writing to a
half-closed transport.
@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/sansio-transport-closing-check-on-send (97856c6) 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