Repository navigation
Fix a data race on entry->hash between getall()/update() and lock-free reads - #1448
Merged
Merged
Conversation
…e reads getall()/popall() (via md_find_next()/md_finder_cleanup()) and update()/extend()/merge() (via md_post_update()) mark and unmark a matching entry's hash while holding self's critical section. That excludes other mutators but not a lock-free reader: __contains__ and get()/getone()/__getitem__ load the same field with an atomic op and no lock at all. The mark/unmark writes were plain, non-atomic stores, racing that atomic load under the free-threaded build. Route them through the existing _md_entry_store_hash() helper, matching the pattern _md_replace()/_md_update() already use for the same mark bit.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1448 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 31 31
Lines 4449 4471 +22
Branches 337 341 +4
=========================================
+ Hits 4449 4471 +22
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:
|
asvetlov
marked this pull request as ready for review
September 13, 2026 20:47
asvetlov
enabled auto-merge (squash)
September 13, 2026 20:47
asvetlov
disabled auto-merge
September 13, 2026 20:51
asvetlov
enabled auto-merge (squash)
September 13, 2026 20:51
Contributor
Confidence Score: 5/5Safe to merge based on the available build and concurrent regression evidence. No actionable findings were established. Files Needing Attention: None.
What T-Rex did
Reviews (1): Last reviewed commit: "Merge branch 'master' into fix-entry-has..." | Re-trigger Greptile |
This was referenced Sep 13, 2026
asvetlov
added a commit
that referenced
this pull request
Sep 13, 2026
<!-- Thank you for your contribution! --> ## What do these changes do? `to_dict()` (via `md_to_dict()`) marks every entry's hash while it walks the table to collect keys, then clears all the marks in one pass via `_md_restore_all_hashes()`, while holding `self`'s critical section. That excludes other mutators but not a lock-free reader: `__contains__` and `get()`/`getone()`/`__getitem__` load the same field with an atomic op and 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 no equivalent 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 - [x] I think the code is well written - [x] Unit tests for the changes exist - [x] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` (N/A: no `CONTRIBUTORS.txt` in this repo) - [x] Add a new news fragment into the `CHANGES/` folder - [x] `make doc-spelling` passes and any new technical words are added to `docs/spelling_wordlist.txt` <details> <summary>Agent run details (optional, for reviewers)</summary> Race confirmed with ThreadSanitizer (clang 21, `-fsanitize=thread`, `LD_PRELOAD`ed into a CPython 3.14.7 free-threaded build): on the pre-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 both `get()` (`_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`, races `to_dict()` against lock-free `contains()`/`get()` on never-deleted keys. Tests: `pytest -q` against the default C-extension build (1897 passed), a free-threaded debug build (`MULTIDICT_DEBUG_BUILD=1` on CPython 3.14.7 free-threaded, `--c-extensions`, 1892 passed, 5 skipped), and the pure-Python leg (`MULTIDICT_NO_EXTENSIONS=1`, 1897 passed). Lint: `pre-commit run` (ruff, clang-format, mypy) passed on the changed files. Docs: `make doc-spelling` passed with no wordlist additions needed. Drafted with Claude Code (Sonnet 5); reviewed by asvetlov. </details>
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?
getall()/popall()(viamd_find_next()/md_finder_cleanup()) andupdate()/extend()/merge()(viamd_post_update()) mark and unmarka matching entry's hash while holding
self'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. The mark/unmark writes were plain, non-atomic
stores, racing that atomic load under the free-threaded build. This
routes them through the existing
_md_entry_store_hash()helper,matching the pattern
_md_replace()/_md_update()already use for thesame mark bit.
Note this is unrelated to and much cheaper than the approach tried in
#1442 (which replaced the marking scheme entirely and was withdrawn for
a large performance regression): the fix here keeps the existing
in-place marking and only makes the one racy write atomic, at the cost
of a relaxed atomic store in place of a plain one.
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
N/A
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)
Tests:
pytest -qagainst the default C-extension build (1839 passed),a free-threaded debug build (
MULTIDICT_DEBUG_BUILD=1on CPython3.14.7 free-threaded, 1834 passed, 5 skipped), and the pure-Python leg
(
MULTIDICT_NO_EXTENSIONS=1,pytest -q --no-c-extensions, 926passed). New test:
tests/test_multidict.py::test_getall_update_vs_lock_free_reads_thread_safety,marked
c_extension, racesgetall()/update()against lock-freecontains()/get()on never-deleted keys.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.