Repository navigation
Document a cache-invalidating watcher in the C API docs - #1586
Conversation
The example unwatches from its own callback, watches again on the next rebuild, uses the version to discard a result that changed underneath, and stops caching a multidict that keeps changing, the same shape as CPython's JIT globals watcher.
|
This comment has been minimized.
This comment has been minimized.
An event goes to whoever is watching when the operation finishes, so it can reach a rebuild that already saw its change. The callback now keeps the version the cache was built from and discards only a cache the multidict has moved past, never unwatches while a rebuild owns the watch, and does both under a lock the rebuild also stores under. The example also ignores bare BATCH brackets, stops listening after a failed parse, and counts every rebuild attempt toward the limit.
|
@aiolibsbot rr |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1586 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 41 41
Lines 7701 7797 +96
Branches 643 650 +7
=========================================
+ Hits 7701 7797 +96
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:
|
Re-reviewed at |
PR Review — Document a cache-invalidating watcher in the C API docsDocumentation-only addition that is genuinely correct under free threading; merge-ready with three non-blocking notes. The hard part here is the concurrency argument, and it holds up. I traced it against
On the two prior bot findings, both look resolved at head
🟢 Suggestions
1. `on_change` passes a refcount-zero `self` to the C API on DEALLOCATED
|
Rebase with requested adjustmentsBranch Changes applied
Not changed (and why)
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
The version check and the rebuild flag are needed under the GIL too, since parse() running Python code lets another thread mutate the multidict; only the lock is free-threaded specific. Put it behind Py_GIL_DISABLED so one source serves both builds, and save the parse error with PyErr_Fetch() so the example works back to 3.10.
What do these changes do?
Adds an "Invalidating a cache" example to
docs/capi.rst, after the existing watcher example. It shows the pattern CPython's JIT uses for a module's globals: the callback clears the cache and unwatches itself, the next rebuild watches again, and after a fixed number of rebuild attempts the cache stops trying on a multidict that keeps changing.An event goes to whoever is watching when the operation finishes, so it can reach a rebuild that already saw its change, under the GIL too once
parse()runs Python code. The cache therefore records the version it was built from: the callback discards only a cache the multidict has moved past, and never unwatches while a rebuild owns the watch. On the free-threaded build the fields also need a lock, which sits behindPy_GIL_DISABLED, so one source serves both builds and works back to 3.10. Bare BATCH brackets are ignored, and a failed parse stops listening.Are there changes in behavior for the user?
No, documentation only.
Is it a substantial burden for the maintainers to support this?
No.
Related issue number
Follow-up to #1551.
Checklist
CONTRIBUTORS.txt(N/A)CHANGES/foldermake doc-spellingpasses and any new technical words are added todocs/spelling_wordlist.txtDrafted with Claude Code (Claude Opus 5.5); to be reviewed by @asvetlov.
Agent run details (optional, for reviewers)
The example code is extracted verbatim from
docs/capi.rstinto a small extension (stubs only formy_mod_stateandparse()), built with-Wall -Wextra -Werror -std=c11against the installedmultidict_capi.hfor 3.14.7, 3.14.7t and 3.10.17.update()/extend()/merge()and self-update keep it; a failed parse leaves no watch; a parse that mutates every time hits the limit of 8 and then receives no further events; concurrent writers never leave it stale.make doc-spellingand pre-commit: pass