Repository navigation
Keep the module alive while a multidict can still read its state - #1563
Merged
asvetlov merged 3 commits intoSep 24, 2026
Merged
Conversation
A multidict caches the module state as a bare pointer, and the only thing keeping the module alive was the heap type's own reference. type_clear() drops that reference, so a collection that clears a multidict type before the multidicts themselves lets module_dealloc() free the state while md_clear() is still about to read it; that showed up as an access violation at interpreter exit on Windows. Each multidict now holds a strong reference to its module, released after md_clear(). module_clear() also closes the pools, so no shell is parked once the types it would need to free it are gone.
Keeping a module reference on every multidict costs an allocation-bound operation a refcount pair: ``cls()`` moves by about 3% and the 20-item ``cls(items)`` and ``d.copy()`` rows by about 1%, on both builds. The rest of the table is re-measured rather than changed; rows the commit cannot touch, including the ``dict`` column, differ by under half a percent because the interpreter builds behind the previous numbers were not byte-identical to these.
asvetlov
marked this pull request as ready for review
September 24, 2026 10:59
Contributor
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1563 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 41 41
Lines 7074 7084 +10
Branches 615 615
=========================================
+ Hits 7074 7084 +10
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:
|
4 of 6 tasks
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?
A multidict caches the module state as a bare
mod_state*, and the onlything keeping the module alive was the heap type's own reference.
CPython's
type_clear()doesPy_CLEAR(ht_module), so a collection thatclears a multidict type before the multidicts themselves lets
module_dealloc()free the state whilemd_clear()is still about toread it. Each multidict now holds a strong reference to its module,
released after
md_clear(), andmodule_clear()closes the pools so noshell is parked once the types that a drain would need are gone.
C extension only; the pure-Python backend has no module state to outlive.
Found while looking at an intermittent Windows wheel-test crash: the
suite passes in full, then the process exits with
3221225477(
0xC0000005), for example inthis cp310-win_amd64 run.
Reproduced under AddressSanitizer on CPython 3.10 as a heap-use-after-free
read in
next_version(), on memory freed bymodule_dealloc().Are there changes in behavior for the user?
No, other than the crash no longer happening.
Is it a substantial burden for the maintainers to support this?
No. It is one field and a reference, on paths that already existed.
Related issue number
None; found from a CI failure.
Checklist
CONTRIBUTORS.txtN/ACHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Opus 5 (Claude Code); reviewed by @asvetlov.
Agent run details (optional, for reviewers)
Reproducer, against the unfixed extension built with
MULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1:test_multidict_torn_down_with_its_moduleis that reproducer; it abortsunder ASan without the fix and passes with it.
test_multidict_refers_to_its_modulefails on any build without it.Tests, all on this branch:
MULTIDICT_NO_FREELIST=1pre-commit run,make doc-spellingNot run: the hypothesis suite and TSan.
Benchmarks, callgrind,
PYTHONHASHSEED=0, CPython 3.14.7 GIL andfree-threaded, against
7b84e1b:cls()/ MultiDictcls()/ CIMultiDictd.copy(), 20 itemsWriting
Py_NewRef(mod)inline in the constructors instead cost +420 Irper 200-item build on the free-threaded leg (+4.3%
clear, +1.4%copy), entirely through inlining churn rather than the refcount: abuild with the struct field and no refcounting cost +7 Ir. Hence the
NOINLINE md_set_module().