Uh oh!
There was an error while loading. Please reload this page.
gh-146613: Fix re-entrant use-after-free in itertools._grouper - #147962
Conversation
The same pattern was fixed in groupby.__next__ (pythongh-143543 / a91b5c3), but _grouper_next (the inner group iterator returned by groupby) was missed. A user-defined __eq__ can re-enter the grouper during PyObject_RichCompareBool, causing Py_XSETREF to free currkey while it is still being used. Fix by taking local snapshots of tgtkey/currkey + INCREF/DECREF protection, exactly as done in groupby_next. Added regression test in test_itertools.py.
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
TheSkyC
commented
Apr 1, 2026
Hi @vstinner, |
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
I confirm that the test does crash without the fix, and does pass successfully with the fix.
Uh oh!
There was an error while loading. Please reload this page.
…zjUFK.rst Co-authored-by: Victor Stinner <vstinner@python.org>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Head branch was pushed to by a user without write access
…zjUFK.rst Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
vstinner
commented
Apr 1, 2026
Lint CI job failed with: It's unrelated to this change. I reported the issue to: #145632 (comment). |
TheSkyC
commented
Apr 1, 2026
Thanks for reporting the issue, @vstinner! Should I update the branch from main to fix it? |
vstinner
commented
Apr 1, 2026
The issue is not fixed yet, so there is no need to update your branch. |
vstinner
commented
Apr 1, 2026
#147968 will fix the Lint CI. |
Uh oh!
There was an error while loading. Please reload this page.
Thanks @TheSkyC for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14. |
pythonGH-147962) (cherry picked from commit fc7a188) Co-authored-by: Ma Yukun <68433685+TheSkyC@users.noreply.github.com>
GH-148010 is a backport of this pull request to the 3.14 branch. |
pythonGH-147962) (cherry picked from commit fc7a188) Co-authored-by: Ma Yukun <68433685+TheSkyC@users.noreply.github.com>
GH-148011 is a backport of this pull request to the 3.13 branch. |
Closesgh-146613
The same pattern was fixed in
groupby.__next__(gh-143543 / a91b5c3), but_grouper_next(the inner group iterator returned bygroupby) was missed.A user-defined
__eq__can re-enter the grouper duringPyObject_RichCompareBool, causingPy_XSETREFto freecurrkeywhile it is still being used.Fixed by taking strong references (
Py_INCREF/Py_DECREF) totgtkeyandcurrkeybefore the comparison, exactly as done ingroupby_next.Added regression test
test_grouper_reentrant_eq_does_not_crash.