Skip to content

Fix items() set algebra corrupting state on reentrant equality - #1453

Merged
asvetlov merged 4 commits into
aio-libs:masterfrom
asvetlov:fix-itemsview-reentrant-equality
Sep 14, 2026
Merged

asvetlov merged 4 commits into
aio-libs:masterfrom
asvetlov:fix-itemsview-reentrant-equality

Conversation

@asvetlov

Copy link
Copy Markdown
Member

What do these changes do?

items() set algebra (&, |, -, in, isdisjoint()) walks a key's
hash chain while temporarily marking visited entries, then compares stored
values against a caller-supplied one before unmarking. That comparison can
run arbitrary user code through a custom __eq__, and code that calls back
into the same MultiDict from there (getall(), for example) while the
chain is still marked observes some matching entries as missing. Depending
on the operation this returns wrong data, raises a bogus KeyError, or, in
the C extension, hangs the process outright (see
#1317 (review)
for the report and a reproduction).

The fix materializes the matching (key, value) pairs into a plain list
after the hash chain has been fully walked and restored, and only then runs
the value comparisons against that snapshot. This covers the C extension's
and1/and2/or1/sub2/contains/isdisjoint paths via a new
md_finder_collect() helper, and the pure-Python __and__(), the only
pure-Python method using the same mark-then-compare pattern (the other
pure-Python methods don't mark and were never affected).

Are there changes in behavior for the user?

No, other than the crash/corruption going away. Ordinary comparisons (an
__eq__ that doesn't touch the multidict) behave exactly as before.

Is it a substantial burden for the maintainers to support this?

No. The fix is a mechanical materialize-then-compare change with no new
public API, plus regression tests.

Related issue number

Follows up on the review comment at
#1317 (review).
No separate issue is filed.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes -- N/A
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • 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
Agent run details (optional, for reviewers)

Tests: pytest -q passed (1927 passed, 12 skipped) under both the C
extension and MULTIDICT_NO_EXTENSIONS=1 pure-Python builds, plus a debug
build (MULTIDICT_DEBUG_BUILD=1, asserts on, -O0).
Lint: pre-commit run (ruff, ruff-format, clang-format, mypy for 3.11 and
3.13) passed on all changed files.
Docs: make doc-spelling passed with no wordlist additions needed.
Confirmed pre-fix: the new tests hang (C extension) or fail with wrong
results/KeyError (pure Python) against the unmodified code.

Drafted with Claude Code (Sonnet 5); reviewed by asvetlov.

items() & other, other & items(), items() | other, other - items(),
in, and isdisjoint() walk a key's hash chain while temporarily
marking visited entries, then compare stored values against a
caller-supplied one with PyObject_RichCompareBool() (C extension) or
== (pure Python) before unmarking. That comparison can run arbitrary
user code through a custom __eq__, and if that code calls back into
the same MultiDict (getall(), for example) while the chain is still
marked, it observes some matching entries as missing, which can
return wrong data, raise a bogus KeyError, or in the C extension
hang the process.

Materialize the matching (key, value) pairs into a plain list first,
after the chain has been fully walked and restored, and only then
run the value comparisons against that snapshot. Applies to the C
extension's and1/and2/or1/sub2/contains/isdisjoint paths via a new
md_finder_collect() helper, and to the pure-Python __and__(), the
only pure-Python method that used the same mark-then-compare pattern.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 14, 2026
@codspeed

codspeed Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 19%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 12 (👁 12) regressed benchmarks
✅ 236 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
👁 test_items_view_equals[cs-c] 183.7 µs 237.6 µs -22.66%
👁 test_items_view_more[cs-c] 91.5 µs 119.2 µs -23.2%
👁 test_items_view_and[cs-c] 209.3 µs 264.6 µs -20.91%
👁 test_items_view_more_or_equal[cs-c] 149.4 µs 202.7 µs -26.29%
👁 test_items_view_or[cs-c] 303.6 µs 347.5 µs -12.63%
👁 test_items_view_is_disjoint[cs-c] 130.6 µs 155.1 µs -15.77%
👁 test_items_view_more[ci-c] 126.5 µs 154.5 µs -18.13%
👁 test_items_view_equals[ci-c] 193.5 µs 246.6 µs -21.53%
👁 test_items_view_more_or_equal[ci-c] 216.6 µs 272.6 µs -20.54%
👁 test_items_view_and[ci-c] 245.4 µs 302.7 µs -18.94%
👁 test_items_view_or[ci-c] 334.2 µs 375.9 µs -11.11%
👁 test_items_view_is_disjoint[ci-c] 136.1 µs 160 µs -14.94%

Comparing asvetlov:fix-itemsview-reentrant-equality (aee9d6e) with master (c288eb5)

Open in CodSpeed

@codecov

codecov Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c288eb5) to head (aee9d6e).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1453   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           32        32           
  Lines         4909      4956   +47     
  Branches       406       408    +2     
=========================================
+ Hits          4909      4956   +47     
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.

@asvetlov
asvetlov marked this pull request as ready for review September 14, 2026 10:50
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 14, 2026 10:50
@asvetlov

Copy link
Copy Markdown
Member Author

Well, slight performance degradation is an acceptable price for correctness.
The set algebra is not the hot path anyway; it exists for the sake of ABC commitment.

@greptile-apps

greptile-apps Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5

Safe to merge.

No outstanding issues remain. asvetlov manually resolved the earlier thread without explanation.

Reviews (3): Last reviewed commit: "Merge branch 'master' into fix-itemsview..." | Re-trigger Greptile

Comment thread CHANGES/1453.bugfix.rst Resolved
@asvetlov
asvetlov merged commit 144171b into aio-libs:master Sep 14, 2026
69 of 75 checks passed
@asvetlov
asvetlov deleted the fix-itemsview-reentrant-equality branch September 14, 2026 15:32
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