Skip to content

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

Merged
kumaraditya303 merged 3 commits into
python:mainfrom
mpage:gh-154130-dictiter-double-free
Nov 20, 2026
Merged

kumaraditya303 merged 3 commits 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

@DinoV DinoV left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@nascheme nascheme left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Nit: the PR description says exhaustion is marked with -1 but that's not how it's done.

Comment thread Objects/dictobject.c Resolved
@kumaraditya303
kumaraditya303 enabled auto-merge (squash) November 20, 2026 12:11
@kumaraditya303
kumaraditya303 merged commit 89d78fc into python:main Nov 20, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

6 participants