Skip to content

Close the async streaming response on the event loop thread - #941

Merged
joe-clickhouse merged 1 commit into
mainfrom
joe/fix-async-stream-close-race
Aug 6, 2026
Merged

Close the async streaming response on the event loop thread#941
joe-clickhouse merged 1 commit into
mainfrom
joe/fix-async-stream-close-race

Conversation

@joe-clickhouse

@joe-clickhouse joe-clickhouse commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Fixes the intermittent cloud failure in test_mid_stream_exception_async.

StreamingResponseSource.close() runs on the parser executor thread. It cancelled the producer task and closed the aiohttp response directly, racing the event loop handling the server side connection abort. On TLS connections that could raise an AttributeError from asyncio's SSL shutdown instead of the real StreamFailureError. Cloud is the only TLS run, which is why it never reproduced locally.

The cleanup is now scheduled onto the event loop with call_soon_threadsafe, falling back to a direct call if the loop is gone. Adds a unit test that checks the response is closed on the loop thread.

StreamingResponseSource.close() ran the producer task cancel and
response.close() from the parser executor thread. Under TLS that raced
the event loop handling a server connection abort and could raise
AttributeError from asyncio's SSL shutdown instead of the real
StreamFailureError. Schedule the cleanup with call_soon_threadsafe.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@joe-clickhouse
joe-clickhouse merged commit 0f05a3c into main Aug 6, 2026
36 checks passed
@joe-clickhouse
joe-clickhouse deleted the joe/fix-async-stream-close-race branch August 6, 2026 00:48
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.

1 participant