Repository navigation
Keep the instance's case mode when re-initializing - #1622
Merged
Merged
Conversation
Add a threaded test that re-initializes a shared multidict, through both md_init() and md_clone_from_ht(), whose old values have a __del__ that reads and adds to it, while other threads run lock-free lookups and iterate it. Under TSan it found two data races, fixed here. md_clone_from_ht() memcpy()'d the whole source table, including the num_readers field that lock-free readers of the source update atomically; the copy now skips the reader fields. _md_install_keys() stored md->is_ci on every re-init while lock-free lookups read it; the store now happens only when the value changes, which a re-init of a live multidict never does through its own type's __init__().
The C extension's __init__() took is_ci from the class whose slot was called, not from the instance: MultiDict.__init__(ci_md, ...) turned a CIMultiDict case-sensitive, and CIMultiDict.__init__(md) raised a TypeError, where the pure-Python implementation keeps the instance's own mode and accepts both. Read self->is_ci instead; with the parameter gone the two tp_init slots were identical, so CIMultiDict inherits MultiDict's.
asvetlov
force-pushed
the
init-keeps-case-mode
branch
from
September 28, 2026 08:55
da0eb7c to
6dd8274
Compare
asvetlov
marked this pull request as ready for review
September 28, 2026 09:13
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1622 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 44 44
Lines 8268 8381 +113
Branches 703 706 +3
==========================================
+ Hits 8268 8381 +113
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:
|
Contributor
|
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?
In the C extension,
__init__()took the case mode from the class whose__init__slot was called, not from the instance. SoMultiDict.__init__(ci_md, pairs)quietly turned aCIMultiDictcase-sensitive (type(d)stillCIMultiDict, but"key" in dbecame false for"Key"), andCIMultiDict.__init__(md)raisedTypeError. The pure-Python implementation readsself._ciand has neither problem._multidict_tp_init()now readsself->is_ci. Without the parameter theMultiDictandCIMultiDicttp_initslots were identical, soCIMultiDictinheritsMultiDict's, which matches pure Python, whereCIMultiDict.__init__ is MultiDict.__init__.This came up while investigating the TSan report fixed in #1621. It is also the only way a live multidict's
is_cicould change, which #1621's guarded store in_md_install_keys()otherwise relies on never happening.Are there changes in behavior for the user?
Yes. Calling one class's
__init__()on an instance of the other now keeps the instance's own case mode, andCIMultiDict.__init__()accepts aMultiDict, as the pure-Python implementation already did.Is it a substantial burden for the maintainers to support this?
No. It removes a function.
Related issue number
Found while working on #1621, and stacked on it: this branch includes #1621's commit until that one merges. Without #1621, reading
self->is_cihere races the unconditional store tois_cithat #1621 removes, and CI's TSan job reports it fromtest_reinit_thread_safety.Checklist
CONTRIBUTORS.txt(N/A)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5.5); reviewed by .
Agent run details (optional, for reviewers)
test_base_init_keeps_case_modeon master's C extension: both C cases fail (theAssertionErrorabove, andTypeError: descriptor '__init__' requires a 'multidict._multidict.CIMultiDict' object). The pure-Python cases pass before and after.MULTIDICT_DEBUG_BUILD=12869 passed; GIL 3.10.17 2881 passed; pure Python (MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions) 1357 passed.tp_init,PYTHONHASHSEED=0: noctor_*row moved;update-10 Ir (-0.03%).make doc-spelling, pre-commit: pass.