Repository navigation
Don't double-iterate during MultiDict initialization - #3
Merged
Merged
Conversation
Previously, the MultiDict constructor iterated twice over its argument: once as a sanity check on its contents, and once again to add those contents to the dictionary. If a generator was passed as an argument, the first iteration would drain it, and the resulting dictionary would be empty. To fix this, save the contents of the generator in another list. This fixes aio-libs#2.
Member
|
thanks |
bdraco
added a commit
to bdraco/multidict
that referenced
this pull request
May 17, 2026
- Adds "Push from a fork, not to upstream" as PR rule aio-libs#3, with the commands and a note on the `GH013` error you get for trying to push a feature branch to `aio-libs/multidict`. - Folds in the docs spell check section from aio-libs#1344 as PR rule aio-libs#7, so `make doc-spelling` is documented as the pre-push gate; adds `codecov` to `docs/spelling_wordlist.txt` for the new fragment. - Renumbers PR rules 3-6 to 4-8, and extends the news fragments to cover all three additions.
3 tasks done
This was referenced Sep 5, 2026
5 of 6 tasks
asvetlov
added a commit
that referenced
this pull request
Sep 24, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? A multidict caches the module state as a bare `mod_state*`, and the only thing keeping the module alive was the heap type's own reference. CPython's `type_clear()` does `Py_CLEAR(ht_module)`, 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. Each multidict now holds a strong reference to its module, released after `md_clear()`, and `module_clear()` closes the pools so no shell 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 in [this cp310-win_amd64 run](https://github.com/aio-libs/multidict/actions/runs/35979984043/job/107569423607). Reproduced under AddressSanitizer on CPython 3.10 as a heap-use-after-free read in `next_version()`, on memory freed by `module_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 - [x] I think the code is well written - [x] Unit tests for the changes exist - [x] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` N/A - [x] Add a new news fragment into the `CHANGES/` folder - [x] `make doc-spelling` passes and any new technical words are added to `docs/spelling_wordlist.txt` Drafted with Claude Opus 5 (Claude Code); reviewed by @asvetlov. <details> <summary>Agent run details (optional, for reviewers)</summary> Reproducer, against the unfixed extension built with `MULTIDICT_DEBUG_BUILD=1 MULTIDICT_ASAN_BUILD=1`: ``` ERROR: AddressSanitizer: heap-use-after-free READ of size 8 at 0x7ad819e26108 thread T0 #0 next_version multidict/_multilib/freethreading.h:205 #1 md_clear multidict/_multilib/hashtable.h:2000 #2 multidict_tp_clear multidict/_multidict.c:675 #3 delete_garbage Modules/gcmodule.c:1017 freed by thread T0 here: #1 module_dealloc Objects/moduleobject.c:689 ``` `test_multidict_torn_down_with_its_module` is that reproducer; it aborts under ASan without the fix and passes with it. `test_multidict_refers_to_its_module` fails on any build without it. Tests, all on this branch: | Leg | Result | | --- | --- | | 3.10, C extension | 2627 passed | | 3.14.7t, free-threaded | 2615 passed | | pure Python, isolated tree and venv | 1258 passed | | 3.10 ASan and UBSan, pools on | 2615 passed, no reports | | 3.10 ASan and UBSan, `MULTIDICT_NO_FREELIST=1` | 2615 passed, no reports | | `pre-commit run`, `make doc-spelling` | pass | Not run: the hypothesis suite and TSan. Benchmarks, callgrind, `PYTHONHASHSEED=0`, CPython 3.14.7 GIL and free-threaded, against `7b84e1b`: | Cell | GIL | Free-threaded | | --- | --- | --- | | `cls()` / MultiDict | +3.2% (590 to 609) | +3.7% (822 to 852) | | `cls()` / CIMultiDict | +2.6% (730 to 749) | +3.3% (911 to 941) | | `d.copy()`, 20 items | +1.0% | +1.1% | | everything else | under 0.5% | under 0.5% | Writing `Py_NewRef(mod)` inline in the constructors instead cost +420 Ir per 200-item build on the free-threaded leg (+4.3% `clear`, +1.4% `copy`), entirely through inlining churn rather than the refcount: a build with the struct field and no refcounting cost +7 Ir. Hence the `NOINLINE md_set_module()`. </details>
This was referenced Sep 24, 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.
Previously, the MultiDict constructor iterated twice over its argument:
once as a sanity check on its contents, and once again to add those
contents to the dictionary. If a generator was passed as an argument,
the first iteration would drain it, and the resulting dictionary would
be empty.
To fix this, save the contents of the generator in another list. This
fixes #2.