Skip to content

gh-160347: Fix concurrent driving race on async generator awaitables - #161995

Open
losewayy wants to merge 1 commit into
python:mainfrom
losewayy:gh-160347
Open

losewayy wants to merge 1 commit into
python:mainfrom
losewayy:gh-160347

Conversation

@losewayy

@losewayy losewayy commented Nov 14, 2026 •

Copy link
Copy Markdown

Closes #160347

Summary

Async generator awaitables previously used AWAITABLE_STATE_ITER both while a call was executing and while it was suspended. Another call on the same awaitable could therefore take the resume path before the initial exception was delivered, or clear ag_running_async while the original call was still active. This could cause a shared aclose() awaitable to resume the generator instead of closing it. Same-thread reentry can also corrupt the awaitable's state in GIL-enabled builds.

Add AWAITABLE_STATE_EXECUTING to give each call exclusive access to the awaitable until the step yields or finishes. Overlapping calls raise the existing ValueError without changing the active operation's state. ag_running_async continues to exclude other awaitables while the owning operation is suspended.

The tests cover reentry, concurrent calls on shared awaitables, ownership across suspension, and existing argument handling for finished generators.

Testing

Tested on Windows x64 Debug builds:

  • GIL: test_asyncgen, test_generators, test_coroutines (262 tests passed).
  • Free-threaded: the above modules plus test_free_threading.test_async_generators (273 tests passed).
  • Free-threaded -R 3:3: test_asyncgen and test_free_threading.test_async_generators (114 tests passed, with no reference leaks reported).

…ables

Add AWAITABLE_STATE_EXECUTING to guard each step of PyAsyncGenASend and
PyAsyncGenAThrow while a call is running, instead of publishing ITER
before the initial send or throw executes. Concurrent or reentrant calls
on the same awaitable now raise ValueError without corrupting
ag_running_async or closing an active operation.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T12:00:33.103742Z 3b523a8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Free-threading: concurrent send() on a shared aclose() awaitable can resume the async generator instead of closing it

1 participant