Skip to content

Keep the instance's case mode when re-initializing - #1622

Merged
asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:init-keeps-case-mode
Sep 28, 2026
Merged

asvetlov merged 2 commits into
aio-libs:masterfrom
asvetlov:init-keeps-case-mode

Conversation

@asvetlov

@asvetlov asvetlov commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

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. So MultiDict.__init__(ci_md, pairs) quietly turned a CIMultiDict case-sensitive (type(d) still CIMultiDict, but "key" in d became false for "Key"), and CIMultiDict.__init__(md) raised TypeError. The pure-Python implementation reads self._ci and has neither problem.

_multidict_tp_init() now reads self->is_ci. Without the parameter the MultiDict and CIMultiDict tp_init slots were identical, so CIMultiDict inherits MultiDict's, which matches pure Python, where CIMultiDict.__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_ci could 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, and CIMultiDict.__init__() accepts a MultiDict, 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_ci here races the unconditional store to is_ci that #1621 removes, and CI's TSan job reports it from test_reinit_thread_safety.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes (N/A, the documented behaviour did not change)
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt (N/A)
  • Add a new news fragment into the CHANGES/ folder
  • make doc-spelling passes and any new technical words are added to docs/spelling_wordlist.txt

Drafted with Claude Code (Claude Opus 5.5); reviewed by .

Agent run details (optional, for reviewers)
  • New test test_base_init_keeps_case_mode on master's C extension: both C cases fail (the AssertionError above, and TypeError: descriptor '__init__' requires a 'multidict._multidict.CIMultiDict' object). The pure-Python cases pass before and after.
  • Full suite: FT 3.14.7t release 2869 passed; FT MULTIDICT_DEBUG_BUILD=1 2869 passed; GIL 3.10.17 2881 passed; pure Python (MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions) 1357 passed.
  • callgrind on GIL 3.10.17, where construction goes through tp_init, PYTHONHASHSEED=0: no ctor_* row moved; update -10 Ir (-0.03%).
  • make doc-spelling, pre-commit: pass.

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__().
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 28, 2026
@codspeed

codspeed Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 706 untouched benchmarks


Comparing asvetlov:init-keeps-case-mode (6dd8274) with master (e04369b)

Open in CodSpeed

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
asvetlov force-pushed the init-keeps-case-mode branch from da0eb7c to 6dd8274 Compare September 28, 2026 08:55
@asvetlov
asvetlov marked this pull request as ready for review September 28, 2026 09:13
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 28, 2026 09:13
@codecov

codecov Bot commented Sep 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (43978bd) to head (6dd8274).
⚠️ Report is 3 commits behind head on master.

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     
Flag Coverage Δ
CI-GHA 100.00% <100.00%> (ø)
pytest 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Fixes case-mode handling during multidict re-initialization.

No blocking issue was found; the change is safe to merge based on the checked behavior.

What we checked:

  • Built the parent revision’s C extension and ran the same constructor checks against it and the PR revision. T-Rex
  • In both revisions, MultiDict.new(CIMultiDict) raised TypeError, and normal construction, explicit CIMultiDict.new followed by initialization, and reinitialization all supported lowercase lookup for a mixed-case key. T-Rex
  • Executed the case-mode-constructor-repro.py script and observed constructor rejection and 'key' membership behavior on both parent and PR outputs. T-Rex
  • Compared the internal C-paths and initializations between parent and PR to explain how is_ci is set, and confirmed no case-sensitive membership consequence. T-Rex

Reviews (1) · Last reviewed commit: "Keep the instance's case mode when re-in..."

@asvetlov
asvetlov merged commit 9802f1b into aio-libs:master Sep 28, 2026
65 of 66 checks passed
@asvetlov
asvetlov deleted the init-keeps-case-mode branch September 28, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant