Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-120321: Make gen.gi_frame.clear() thread-safe#143112

Merged
colesbury merged 1 commit intopython:mainpython/cpython:mainfrom
colesbury:gh-120321-gen-frame-clearcolesbury/cpython:gh-120321-gen-frame-clearCopy head branch name to clipboard
Jan 8, 2026
Merged

gh-120321: Make gen.gi_frame.clear() thread-safe#143112
colesbury merged 1 commit intopython:mainpython/cpython:mainfrom
colesbury:gh-120321-gen-frame-clearcolesbury/cpython:gh-120321-gen-frame-clearCopy head branch name to clipboard

Conversation

@colesbury
Copy link
Contributor

@colesbury colesbury commented Dec 23, 2025

This splits up the clearing of a generator frame from the tp_finalize implementation, which makes it easier to do the frame transition atomically.

Note that since gh-111792, it's only legal to call generator.gi_frame.clear() on a generator in the FRAME_CREATED state or finished state, not any of the suspended states.

@colesbury
Copy link
Contributor Author

I've seen this reported in TSan, although I'm not sure where the frame.clear() call is happening in Python:

test_imap_handle_iterable_exception (test.test_multiprocessing_forkserver.test_threads.WithThreadsTestPool.test_imap_handle_iterable_exception) ... ==================
WARNING: ThreadSanitizer: data race (pid=952942)
  Atomic write of size 1 at 0x7fffb56c24a3 by thread T79:
    #0 _Py_atomic_compare_exchange_int8 /home/sgross/cpython/./Include/cpython/pyatomic_gcc.h:75:10 (python+0x2ba69c) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
    #1 gen_send_ex /home/sgross/cpython/Objects/genobject.c:327:15 (python+0x2ba69c)
    #2 gen_iternext /home/sgross/cpython/Objects/genobject.c:721:9 (python+0x2b6ec2) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
    #3 _PyForIter_VirtualIteratorNext /home/sgross/cpython/Python/ceval.c:4115:24 (python+0x527188) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
    #4 _PyEval_EvalFrameDefault /home/sgross/cpython/Python/generated_cases.c.h:5375:36 (python+0x50aa13) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
    #5 _PyEval_EvalFrame /home/sgross/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x4ec83d) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
...

  Previous read of size 1 at 0x7fffb56c24a3 by main thread:
    #0 frame_clear_impl /home/sgross/cpython/Objects/frameobject.c:2019:18 (python+0x2d06f2) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
    #1 frame_clear /home/sgross/cpython/Objects/clinic/frameobject.c.h:407:20 (python+0x2d06f2)
    #2 _PyEval_EvalFrameDefault /home/sgross/cpython/Python/generated_cases.c.h:3556:35 (python+0x50b406) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
    #3 _PyEval_EvalFrame /home/sgross/cpython/./Include/internal/pycore_ceval.h:119:16 (python+0x4ec83d) (BuildId: 59de1379ec77c44e6bf58e50950f5e82982a1cca)
...

Objects/genobject.c Show resolved Hide resolved
@colesbury colesbury merged commit e2f15ae into python:main Jan 8, 2026
54 checks passed
@colesbury colesbury deleted the gh-120321-gen-frame-clear branch January 8, 2026 19:45
reidenong pushed a commit to reidenong/cpython that referenced this pull request Jan 12, 2026
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.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.