Repository navigation
Fix a data race on entry->hash in to_dict()'s mark cleanup - #1450
Conversation
_md_restore_all_hashes() cleared to_dict()'s temporary "seen" mark on every entry with a plain, non-atomic store, while a lock-free __contains__/get() on another thread loads the same field with an atomic operation and no lock at all under the free-threaded build. This routes the clear through the existing _md_entry_store_hash() helper, matching the pattern already used for the sibling mark/unmark sites in md_find_next()/md_finder_cleanup()/md_post_update() (aio-libs#1448). C-extension only; the pure-Python to_dict() has no equivalent hash-marking scheme.
Merging this PR will improve performance by 11.29%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_multidict_getall_str_hit[cs-c] |
38 ms | 33.6 ms | +13.13% |
| ⚡ | test_multidict_getall_str_hit[ci-c] |
38.8 ms | 34.7 ms | +11.75% |
| ⚡ | test_cimultidict_getall_istr_hit[c] |
36.8 ms | 33.1 ms | +11.12% |
| ⚡ | test_cimultidict_getall_istr_hit_nonascii[c] |
28 ms | 25.7 ms | +9.2% |
Tip
Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.
Comparing asvetlov:fix-restore-all-hashes-race (5a0beb2) with master (80632eb)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1450 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 31 31
Lines 4471 4489 +18
Branches 341 344 +3
=========================================
+ Hits 4471 4489 +18
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:
|
Confidence Score: 5/5No actionable issue was established, and nothing found in this change blocks merging. No review comments or severity-bearing execution findings remain. The focused native regression passed, though the free-threaded runtime path is unavailable in this environment. Files Needing Attention: None.
What T-Rex did
Reviews (1): Last reviewed commit: "Fix a data race on entry->hash in to_dic..." | Re-trigger Greptile |
What do these changes do?
to_dict()(viamd_to_dict()) marks every entry's hash while it walksthe table to collect keys, then clears all the marks in one pass via
_md_restore_all_hashes(), while holdingself's critical section. Thatexcludes other mutators but not a lock-free reader:
__contains__andget()/getone()/__getitem__load the same field with an atomic opand no lock at all.
_md_restore_all_hashes()'s clear was a plain,non-atomic store, racing that atomic load under the free-threaded
build. This routes it through the existing
_md_entry_store_hash()helper, matching the pattern already used for the sibling mark/unmark
sites in
md_find_next()/md_finder_cleanup()/md_post_update()fixed by #1448.
This is a C-extension-only fix: the pure-Python
to_dict()has noequivalent hash-marking scheme, so there is nothing to change there.
Are there changes in behavior for the user?
No. This only affects the free-threaded build's internal memory-model
correctness; there is no change to observable behavior.
Is it a substantial burden for the maintainers to support this?
No, it's a small, self-contained fix mirroring the existing atomic-op
pattern already used elsewhere in
hashtable.h.Related issue number
Found via a Greptile review comment on #1447; not otherwise linked to
an issue.
Checklist
CONTRIBUTORS.txt(N/A: noCONTRIBUTORS.txtin this repo)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtAgent run details (optional, for reviewers)
Race confirmed with ThreadSanitizer (clang 21,
-fsanitize=thread,LD_PRELOADed into a CPython 3.14.7 free-threaded build): on thepre-fix code, TSan reports a data race between the plain store in
_md_restore_all_hashes()(hashtable.h:1748) and the atomic load in_md_entry_load_hash()reached from bothget()(
_md_get_one_lockfree) and__contains__(_md_contains_lockfree).After the fix, that report is gone; only an unrelated, pre-existing
CPython-internal biased-refcounting race remains, present identically
before and after.
New test:
tests/test_multidict.py::test_to_dict_vs_lock_free_reads_thread_safety,marked
c_extension, racesto_dict()against lock-freecontains()/get()on never-deleted keys.Tests:
pytest -qagainst the default C-extension build (1897passed), a free-threaded debug build (
MULTIDICT_DEBUG_BUILD=1onCPython 3.14.7 free-threaded,
--c-extensions, 1892 passed, 5skipped), and the pure-Python leg (
MULTIDICT_NO_EXTENSIONS=1, 1897passed).
Lint:
pre-commit run(ruff, clang-format, mypy) passed on the changedfiles.
Docs:
make doc-spellingpassed with no wordlist additions needed.Drafted with Claude Code (Sonnet 5); reviewed by asvetlov.