refactor: connect-first stream lifecycle for sse and streamable_http#2292
Open
refactor: connect-first stream lifecycle for sse and streamable_http#2292
Conversation
Apply the websocket_client pattern from #2266 to the other two transports: establish the network connection first, create memory streams only after it succeeds, then own all four stream ends plus the task group in a single merged async with as the innermost scope. This eliminates the try/finally + four explicit aclose() calls. If the connection fails, no streams were ever created — nothing to clean up. The multi-CM async with unwinds in reverse order on exit, so tg.__aexit__ waits for cancelled tasks to finish before any stream end closes. streamable_http has one outer async with (the AsyncExitStack for the conditional httpx client), which is clean on all Python versions. sse has two unavoidable outer layers (httpx_client_factory feeds into aconnect_sse — data dependency, can't merge). On 3.14, coverage.py's static analysis sees a phantom branch on the innermost multi-CM line: each __aexit__ gets a POP_JUMP_IF_TRUE for 'did it suppress the exception?', which memory streams never do. One targeted pragma on the line we own, documented inline. Behavior change: sse_client's ConnectError is no longer wrapped in an ExceptionGroup, since the task group is never entered when the connection fails. Updated the regression test to match.
Kludex
reviewed
Mar 13, 2026
Comment on lines
+64
to
+68
| async with aconnect_sse( | ||
| client, | ||
| "GET", | ||
| url, | ||
| ) as event_source: |
Member
There was a problem hiding this comment.
Suggested change
| async with aconnect_sse( | |
| client, | |
| "GET", | |
| url, | |
| ) as event_source: | |
| async with aconnect_sse(client, "GET", url) as event_source: |
Kludex
reviewed
Mar 13, 2026
Comment on lines
+125
to
+126
| finally: | ||
| await read_stream_writer.aclose() |
Kludex
approved these changes
Mar 13, 2026
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.
Follow-up to #2266 — applies the
websocket_clientpattern to the other two client transports.What changed
Instead of creating memory streams up front and cleaning them up in a
try/finally, the transports now:httpx_client_factory/aconnect_sse/AsyncExitStackbecome the outer scopesaconnect_sse()raisesConnectError, no streams were ever created, nothing to clean upasync withas the innermost scope — closes everything in reverse order on exit, task group waits for cancelled tasks before any stream end closesDrops the
try/finallywrapper and four explicitaclose()calls from each file. Net −6 lines.Why the previous attempt was reverted
Commit history in #2266:
1e83583tried replacingtry:withasync with streams:at the same nesting position, leavinghttpx_client_factoryandaconnect_ssenested inside it. On Python 3.14, that triggered phantom branch arcs on the innerasync withlines — each context manager's__aexit__compiles to aPOP_JUMP_IF_TRUE("did it suppress the exception?") that gets misattributed through 3.14's exception table.d297fcfreverted totry/finally.This PR inverts the nesting instead. Verified empirically across 3.11/3.13/3.14:
async with+ multi-CM innermost (websocket, streamable_http)async with+ multi-CM innermost (sse)# pragma: no branchon that linestreamable_httphas only one outer layer (AsyncExitStack) → clean on all versions, zero pragmas.ssehas two unavoidable outer layers (httpx_client_factoryfeedsclientintoaconnect_sse— data dependency, can't merge into one line) → one documented pragma on theasync with (streams, tg)line, which is the line we own.Behavior change
sse_client'sConnectErroris no longer wrapped in anExceptionGroup— the task group is never entered when the connection fails, so there's nothing to group. Updatedtest_sse_client_closes_all_streams_on_connection_errorto expect a barehttpx.ConnectError.AI Disclaimer