Repository navigation
Retry the retired-table drain after pushing tables back - #1565
Merged
asvetlov merged 4 commits intoSep 24, 2026
Merged
Conversation
On the free-threaded build, a drain that reads the active-readers gate as nonzero pushes the tables it holds back onto md->retired and leaves them to the reader the gate showed. That reader can have run its own drain already, between the exchange and the push-back, and found the list empty, in which case nothing is left to free them: they sit there until the multidict is next used, and if it is dropped instead, the references its entries own are never released. Rereading the gate after the push-back separates that from the case the push-back is for. Nonzero means the reader it sees decrements after the read, so after the push-back, and its own drain observes the tables; zero means nothing else will, and another pass is what frees them. The emitted code is unchanged on builds with the GIL enabled, and _md_drain_retired_slow() itself keeps its instruction count.
asvetlov
marked this pull request as ready for review
September 24, 2026 11:21
Same fix as on the branch for aio-libs#1562, where review pointed it out: an exception in the reader before it signalled readiness blocked the test in an unbounded wait, and one out of clear() skipped the stop signal and the join, leaving a non-daemon thread reading a multidict the test had finished with.
Contributor
|
This comment has been minimized.
This comment has been minimized.
…red-pushback-recheck # Conflicts: # tests/test_multidict.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1565 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 41 41
Lines 7084 7148 +64
Branches 615 625 +10
=========================================
+ Hits 7084 7148 +64
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:
|
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?
On the free-threaded build, a drain that reads the active-readers gate as
nonzero pushes the tables it holds back onto
md->retiredand leaves them tothe reader the gate showed it. That reader can have run its own drain already,
between
_md_drain_retired_slow()'s exchange and its push-back, and found thelist empty; nothing is then left to free those tables. They sit there until
the multidict is next used, and if it is dropped instead, the references their
entries own are never released. Rereading the gate after the push-back tells
the two apart: nonzero means the reader it sees decrements after that read,
hence after the push-back, so its own drain observes the tables, while zero
means nothing else will and another pass is what frees them.
This is the window #1555 was reached through, so it is an alternative fix for
that leak as well as a fix for tables sitting on the list longer than they
should. It is complementary to #1562 rather than a replacement: this closes
the window, #1562 backstops the invariant at teardown whatever the drain does,
and the two do not conflict.
Are there changes in behavior for the user?
No, other than retired tables being freed when they should be. C extension
only; the pure-Python implementation has no retirement scheme. Builds with the
GIL enabled emit byte-identical code.
Is it a substantial burden for the maintainers to support this?
No. One reread of an atomic already being read on that path, on a branch taken
only when a drain finds a reader in flight.
Related issue number
Refs #1555
Checklist
CONTRIBUTORS.txtN/A, this repo has no such fileCHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Opus 5 in Claude Code; to be reviewed by @asvetlov before
it leaves draft.
Agent run details (optional, for reviewers)
Proof, with three sleeps compiled in behind
-DMULTIDICT_WIDEN_DRAIN: 300msat the end of
_md_reader_enter(), 100ms after the exchange, and 300ms beforethe push-back. A thread waits 50ms and then does one
d.get()while the mainthread clears a multidict holding one weakref'd value, then drops it.
On
6c1c9a8the table is pushed back and never freed, and the value survivesteardown, 3/3 attempts. With this branch the reread fires and the retry frees
it inside
clear()itself, 0/3, on every attempt in one extra pass:The committed test cannot force that ordering, so it runs the shape (a
clear()against continuous lock-free reads, then teardown with no furtheroperation) 50 times over.
Tests, CPython 3.14.7 unless noted, one venv and one tree per leg:
Lint:
pre-commit run --from-ref 6c1c9a8 --to-ref HEAD, all hooks passed.Docs:
make doc-spelling, build succeeded.Performance,
benchmarks/callgrind_driver.py --impl multidict_c --include-multidict-onlyon the free-threaded build, both arms built in thesame tree and venv: every one of the 26 cells within 0.14%, and
clear,copy,ctor_items,iter_items,getallandpopitemexactly flat, sodocs/benchmark.rstneeds no refresh.tools/codegen_diff.py --ref 6c1c9a8from a fresh worktree reports 0 of 203 functions changed with the GIL enabled
and 14 of 206 free-threaded, of which 13 are
.coldpartitions that movedwith unchanged instruction counts and call targets; the fourteenth is
_md_drain_retired_slow()itself, at 158 instructions before and after.Worth knowing for anyone measuring this: the first shape tried here split the
function in two, an
_md_drain_retired_attempt()returning whether it pushedback plus a
whileloop over it. That is the same fix, but it moved GCC'sinlining enough to cost
iter_items8% (and to win 4 Ir per entry onclear,ctor_itemsandcopy), reproducibly, across 60 changed functions.The backward jump inside the one function avoids all of it. Also note
tools/codegen_diff.pymust be run from a tree with nobuild/directory:its own in-place build is otherwise stale, and it will report dozens of
changed functions for source that is byte-identical to the ref.