Repository navigation
Fix items() set algebra corrupting state on reentrant equality - #1453
Conversation
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.
Merging this PR will degrade performance by 19%
|
| 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)
Codecov Report✅ All modified and coverable lines are covered by tests. 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
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:
|
|
Well, slight performance degradation is an acceptable price for correctness. |
Confidence Score: 5/5Safe 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 |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
What do these changes do?
items()set algebra (&,|,-,in,isdisjoint()) walks a key'shash 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 backinto the same
MultiDictfrom there (getall(), for example) while thechain is still marked observes some matching entries as missing. Depending
on the operation this returns wrong data, raises a bogus
KeyError, or, inthe 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 listafter 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/isdisjointpaths via a newmd_finder_collect()helper, and the pure-Python__and__(), the onlypure-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
CONTRIBUTORS.txtCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtAgent run details (optional, for reviewers)
Tests:
pytest -qpassed (1927 passed, 12 skipped) under both the Cextension and
MULTIDICT_NO_EXTENSIONS=1pure-Python builds, plus a debugbuild (
MULTIDICT_DEBUG_BUILD=1, asserts on,-O0).Lint:
pre-commit run(ruff, ruff-format, clang-format, mypy for 3.11 and3.13) passed on all changed files.
Docs:
make doc-spellingpassed 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.