Skip to content

gh-154130: Fix double decref of the dict in shared dict iterators - #160312

Open
mpage wants to merge 1 commit into
python:mainfrom
mpage:gh-154130-dictiter-double-free
Open

mpage wants to merge 1 commit into
python:mainfrom
mpage:gh-154130-dictiter-double-free

Conversation

@mpage

@mpage mpage commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

In the free-threaded build, threads advancing the same dict iterator each read di_dict as a borrowed reference, so concurrently exhausting it could run di->di_dict = NULL; Py_DECREF(d) more than once and free the dict while other threads were still using it. Reversed iterators were also affected: they dropped the reference while holding the dict's critical section.

In the free-threaded build, keep the iterator's reference to the dict until the iterator is deallocated (as list and tuple iterators do) and mark exhaustion by setting di_pos to -1.

In the free-threaded build, threads advancing the same dict iterator each
read `di_dict` as a borrowed reference, so concurrently exhausting it could run
`di->di_dict = NULL; Py_DECREF(d)` more than once and free the dict while
other threads were still using it.  Reversed iterators were also affected:
they dropped the reference while holding the dict's critical section.

In the free-threaded build, keep the iterator's reference to the dict until
the iterator is deallocated (as list and tuple iterators do) and mark
exhaustion by setting `di_pos` to -1.
@mpage
mpage force-pushed the gh-154130-dictiter-double-free branch from f29f11e to 364cc82 Compare October 22, 2026 20:33
@mpage
mpage requested review from DinoV and nascheme October 22, 2026 22:25
@mpage
mpage marked this pull request as ready for review October 22, 2026 22:25
@mpage mpage added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Oct 22, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @mpage for commit 364cc82 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F160312%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Oct 22, 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