Repository navigation
Fix a data race on the MultiDict version counter - #1514
Conversation
md_version() read md->version with a plain load while every mutation site wrote it with a plain store under the object's own critical section, so getversion() could race a concurrent writer on Py_GIL_DISABLED builds. Both sides now go through the same relaxed atomic load/store pair len() already uses for md->used.
|
This comment has been minimized.
This comment has been minimized.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1514 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 38 38
Lines 6347 6381 +34
Branches 574 579 +5
=========================================
+ Hits 6347 6381 +34
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:
|
|
@aiolibsbot rr |
The writer had a fixed iteration count and the readers looped until stop was set, so on a GIL build the writer could run to completion and set stop before any reader was ever scheduled, leaving `seen` empty for reasons unrelated to the atomic version fix. Give the readers the fixed count instead and let the writer loop until every reader is done, mirroring the extend-vs-mutation test's existing pattern, plus a start barrier so no thread gets a head start.
# Conflicts: # multidict/_multilib/hashtable.h
The version-backwards check in the new getversion race test, and the pre-existing reader RuntimeError handler in the iterator race test, never execute on a passing run; mark both no-cover, matching the sibling except blocks already annotated that way.
Re-reviewed. No plain store to |
PR Review — Fix a data race on the MultiDict version counterCorrect and complete fix; four non-blocking suggestions, none of which hold up the merge. The diff does what it says and does it thoroughly. Verified against the tree at HEAD:
Suggestions:
Not re-raised: the 🟢 Suggestions
1. MSVC uint64 load/store is not atomic on 32-bit x86
|
|
Thanks for the thorough re-review. The two remaining suggestions are valid follow-ups, but neither is blocking for this fix: the 32-bit MSVC tearing window is extremely narrow, and the helper refactor is cleanup rather than a correctness issue. The race fix itself is complete and the test/TSan coverage is sufficient. |
|
The high-severity finding at The remaining findings are non-blocking test-hardening suggestions. Capturing writer exceptions would ensure failed mutations are reported through the test rather than only through The MSVC 32-bit |
Rebase with requested adjustmentsBranch Warning Force-push safety check — the rewrite of Commits whose patch changed in-flight and whose content could not be verified as preserved — check these survived:
Previous PR head: Changes applied
Not changed (and why)
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
…aring _md_pop_all_locked's restart check compared md->version with a plain read after _md_del_at(), which can run a __del__ that suspends the critical section and lets another thread bump the version through the atomic store just above. Use atomic_load_uint64_relaxed there too. Also fixed atomic_load_uint64_relaxed/atomic_store_uint64_relaxed on 32-bit x86 MSVC: a plain 64-bit load/store there is two 32-bit accesses and can tear. Route both through _InterlockedCompareExchange64, matching the existing 32-bit fallback in atomic_fetch_add_uint64_relaxed.
… fix master (aio-libs#1514) made md_version() an atomic relaxed read, matching md_len(). Update the note and the Atomic bucket's definition in the new Thread safety section accordingly, since it previously described this counter as a best-effort, unsynchronized read that is no longer accurate.
What do these changes do?
md_version()readmd->versionwith a plain, non-atomic load, whileevery mutation site wrote it with a plain store under the object's own
critical section. On a free-threaded build,
getversion()(which callsmd_version()without taking that critical section) could race aconcurrent writer. Both sides now use the relaxed atomic load/store pair
already used for the object's item count, so the read and every write
site go through
atomic_load_uint64_relaxed()/atomic_store_uint64_relaxed().Are there changes in behavior for the user?
No.
getversion()still returns the same opaque, per-mutation token;this only removes a data race on free-threaded builds.
Is it a substantial burden for the maintainers to support this?
No, it mirrors the existing atomic-op pattern in
atomic_helpers.h.Related issue number
Related to #1444, which fixed the analogous race on the shared global
counter but left the per-object
md->versionfield itself unaddressed.Checklist
CONTRIBUTORS.txtCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Sonnet 5); reviewed by asvetlov.
Agent run details (optional, for reviewers)
Tests:
pytest -q, all green on the C extension under a normal GIL build (2160 passed), a free-threaded 3.14t build (2148 passed), and the pure-Python build (2160 passed, unchanged since this is C-extension only).New test:
tests/test_free_threading.py::test_race_condition_getversion_vs_mutation, markedc_extension.Sanitizer: confirmed with
MULTIDICT_TSAN_BUILD=1against a TSan-instrumented free-threaded CPython. Before the fix, TSan reliably flagged the exact race (_md_replacewrite vs.md_version/getversionread); after the fix, TSan is silent on the new test and on the full suite (2139 passed, 0 warnings).Lint: pre-commit (ruff check/format, clang-format, mypy) passed on the changed files.
Docs:
make doc-spellingpassed with no wordlist additions needed.