fix: prevent SIGSEGV when closing AsyncSession during active stream (#675) - #751
Open
minhduytran wants to merge 1 commit into
Open
fix: prevent SIGSEGV when closing AsyncSession during active stream (#675)#751minhduytran wants to merge 1 commit into
minhduytran wants to merge 1 commit into
Conversation
minhduytran
force-pushed
the
fix/async-session-close-crash
branch
from
April 30, 2026 07:40
dc99b07 to
d0f4e28
Compare
When closing an async streaming response (SSE / long-lived chunked stream) before the server ends it, aclose() must set quit_now before awaiting the perform task. Without this: 1. The perform coroutine never receives the abort signal so aclose() hangs forever on long-lived connections. 2. The curl easy handle may be in a half-initialized streaming state when cleanup runs, causing curl_easy_reset() / curl_easy_cleanup() to free already-freed memory -> SIGSEGV / SIGABRT. Also reorder AsyncSession.close() to set _closed = True before await self.acurl.close(). release_curl() is called as a done-callback on the stream task and checks self._closed to decide whether to call acurl.remove_handle() or curl.close(). If _closed is set after the multi-handle is destroyed, remove_handle() is called on a dead pointer. Fixes lexiforest#675
minhduytran
force-pushed
the
fix/async-session-close-crash
branch
from
April 30, 2026 07:47
d0f4e28 to
2568a09
Compare
Owner
|
The original issue is about sync session, but this PR is about async session. Are they really related? |
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.
Problem
Two race conditions cause segfaults/hangs when AsyncSession.close() or Response.aclose() is called during an active streaming request.
1. AsyncSession.close() ordering race (SIGSEGV / SIGABRT)
_closed is set AFTER await self.acurl.close(). Between these two lines, the release_curl callback fires, sees _closed == False, and calls acurl.remove_handle() on the already-destroyed multi-handle.
2. Response.aclose() missing abort signal (hang + crash)
aclose() never sets quit_now before awaiting astream_task. For SSE streams, await self.astream_task hangs forever, and the curl handle is left in a partially-initialized state that crashes on cleanup.
Fix
Related
Fixes #675
The Curl.reset = lambda self: None monkey-patch workaround documented in the issue confirms this root cause.
Checklist