Repository navigation
Fix key and value refleak when a table resize fails on insert - #1507
Merged
Merged
Conversation
_md_add_with_hash() and _md_add_for_upd() take new references and hand them to the *_steal_refs() variants, which return -1 without consuming them when growing the table fails. Release them on that path, so a MemoryError from add(), setdefault(), item assignment, update() or merge() no longer leaks one reference each to the identity, key and value.
asvetlov
marked this pull request as ready for review
September 21, 2026 17:39
Contributor
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1507 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 37 37
Lines 6128 6160 +32
Branches 552 554 +2
=========================================
+ Hits 6128 6160 +32
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Sep 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
_md_add_with_hash()and_md_add_for_upd()inhashtable.hincref the identity, key and value and pass them to the*_steal_refs()variants, which return -1 without consuming them when growing the table fails. AMemoryErrorat that point leaked one reference to each. Both wrappers now release them on failure. A plainPy_DECREFis safe even inside an update batch that otherwise defers decrefs, since the caller still holds its own references and none of the counts can reach zero.The direct
*_steal_refs()callers inmd_update_from_dict(),md_update_from_kwnames()andmd_update_from_seq()already clean up on failure, so they are unchanged. The pure-Python backend has no manual refcounting and needs no change.The new test fails the n-th allocation with
_testcapi.set_nomemory()for each n until the call succeeds, feeding items one at a time so the table grows mid-insert, and checks the key and value refcounts after every attempt. Without the fix it fails foradd,setdefault,__setitem__,updateandmerge. It is markedc_extensionand does not run on the pure-Python leg, because failing allocations inside interpreted code hits CPython's own unraisable-error paths and leaves refs held by a stray traceback.Are there changes in behavior for the user?
No, apart from the leak being gone.
Is it a substantial burden for the maintainers to support this?
No.
Related issue number
N/A. The same fix is bundled in #1505, which will drop its copy once this lands.
Checklist
CONTRIBUTORS.txt(N/A)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Opus 5); to be reviewed by @asvetlov.
Agent run details (optional, for reviewers)
pytest tests2088 passed, 19 skipped.MULTIDICT_NO_EXTENSIONS=1,--no-c-extensions): 1043 passed, 15 skipped, 1048 deselected.MULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1with libasan preloaded andPYTHONMALLOC=malloc,-k "not test_leak": 2077 passed, 19 skipped.extendpasses, it already cleaned up); with the change: 12 passed.tests/test_mutable_multidict.pyon the C run: 100% lines and branches. The injection arms, calls and disarms on one line so the coverage tracer never sees the failed allocation.getrefcountis not reliable.pre-commithooks andmake doc-spellingpass.