◐ Shell
clean mode source ↗

gh-120321: Make gi_frame_state transitions atomic in FT build by colesbury · Pull Request #142599 · python/cpython

This makes generator frame state transitions atomic in the free
threading build, which avoids segfaults when trying to execute
a generator from multiple threads concurrently.

There are still a few operations that aren't thread-safe and may crash
if performed concurrently on the same generator/coroutine:

 * Accessing gi_yieldfrom/cr_await/ag_await
 * Accessing gi_frame/cr_frame/ag_frame
 * Async generator operations

@colesbury

mpage

kumaraditya303

kumaraditya303

kumaraditya303

kumaraditya303

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>

@colesbury

kumaraditya303

kumaraditya303

markshannon

Also use it in both the default and FT builds.
Generator calls may be nested so it's not sufficient to set the field
before the call to PyEval_Frame.

kumaraditya303

kumaraditya303

Co-authored-by: Kumar Aditya <kumaraditya@python.org>

cdce8p

cocolato pushed a commit to cocolato/cpython that referenced this pull request

Dec 22, 2025
…ythongh-142599)

This makes generator frame state transitions atomic in the free
threading build, which avoids segfaults when trying to execute
a generator from multiple threads concurrently.

There are still a few operations that aren't thread-safe and may crash
if performed concurrently on the same generator/coroutine:

 * Accessing gi_yieldfrom/cr_await/ag_await
 * Accessing gi_frame/cr_frame/ag_frame
 * Async generator operations