Handle reads during ASGI WebSocket shutdown - #1233
Merged
Merged
Conversation
Closing an ASGI WebSocket session while its context remains open can wake a pending read with anyio.EndOfStream. The exception escapes the session task group because its EOF handling expects an empty byte string from the network stream. Translate the ASGI queue's EndOfStream to b"" at the stream boundary, matching httpcore2's AnyIO adapter. Cover the pending-read contract and cross-task session close on asyncio and Trio. This addresses the vendored equivalent of httpx-ws issue pydantic#146 [1], with the same EOF translation proposed upstream [2]. [1]: frankie567/httpx-ws#146 [2]: frankie567/httpx-ws#147
Contributor
Author
|
@Viicos could you please TAL? I suspect your bot might come and close this since there's no ticket :( |
Merging this PR will not alter performance
Comparing Footnotes
|
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
An ASGI network-stream read begun after queue closure raises AnyIO's ClosedResourceError. The WebSocket session only handles network-layer read errors and EOF, leaving this backend-specific failure unhandled. Translate local queue closure to httpcore2.ReadError, as the normal AnyIO network adapter does. Keep EndOfStream mapped to b"" and extend the stream regression to exercise both pending and post-close reads. Addresses the missing closure case identified in review [1]. [1]: pydantic#1233 (comment)
Member
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
Closing an ASGI WebSocket stream can expose AnyIO's queue exceptions through the network-stream interface. A pending receive raises
EndOfStream, and a read begun after local queue closure raisesClosedResourceError. The WebSocket session handles network-layer errors and EOF, so these backend exceptions can escape its task group.Translate
EndOfStreamtob""andClosedResourceErrortohttpcore2.ReadError, matching httpcore2's AnyIO adapter. The regressions cover pending and post-close reads on asyncio and Trio, plus explicit session close while another task owns the context.This addresses the vendored equivalent of the httpx-ws issue 1 using the EOF translation proposed upstream 2.
Prepared with assistance from Codex.
Checklist