Repository navigation
fix: call the cache backend with no Citry lock held - #335
Merged
Merged
Conversation
With Component.Cache or <c-cache> in use, Citry stored a cached render in the cache backend, and replayed the writes a cache hit needs, while it held the cache extension's revision lock. Every render that builds a cache key and every reset (reset_template(), reset_files(), Citry.clear()) takes that lock, so a slow backend call, such as a network round trip to Redis, held up cached renders on other threads and every hot reload. Citry.clear() also wiped the backend while holding that lock. The backend is the application's own code, so it now runs with no Citry lock held. CacheExtension._unchanged_revision checks the revision before the writes and again after them; reading the revision waits for an invalidation in progress, so one that overlapped the writes is seen. When it moved, a stored render's key is deleted (the key names the old revision, so no new lookup would read it anyway), and a replay undoes the keys it created that are marked for rollback and raises, so the caller renders again. These cleanup deletes are best-effort: a backend error there is logged, not raised. Citry.clear() wipes the backend after the invalidation released the lock; the revision has already moved, so an entry stored meanwhile under the old revision is never read. Reset hooks that call the backend (the dependencies extension deleting a component's stored scripts in on_files_reset) still run while the reset holds its locks. Tests: a slow backend set no longer holds up a reset or another cached render, and the entry a slow set stores across a reset is deleted, so the next render shows the new template. The caching design doc says where the revision is checked. Fixes #316 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Fixes #316
What changes
With
Component.Cacheor<c-cache>in use, Citry called the cache backend while it held the cache extension's revision lock. It did this when storing a cached render, when replaying the writes a cache hit needs, and whenCitry.clear()wiped the backend.Every render that builds a cache key takes that lock, and so does every reset (
reset_template(),reset_files(),Citry.clear()). So a slow backend call, such as a network round trip to Redis, held up cached renders on other threads and every hot reload.The backend is the application's own code, so it now runs with no Citry lock held. This follows the rule from #319.
CacheExtension._unchanged_revision(inext/cache/extension.py) checks the revision before the backend calls and again after them. Reading the revision waits for any invalidation in progress, so an invalidation that overlapped the calls is seen._publish_componentdeletes the entry it just stored. The key names the old revision, so no new lookup would read it anyway.ext/cache/replay.pyundoes only the keys this replay created that are markedrollback_delete, then raises, so the caller renders again. Content-addressed repairs stay; a review found that deleting those dropped a live page's CSS variables.Citry.clear()wipes the backend after the invalidation has released the lock. The revision has already moved, so an entry stored in the meantime under the old revision is never read.Unchanged: reset hooks that call the backend still run while the reset holds its locks. One example is the dependencies extension deleting a component's stored scripts in
on_files_reset. Resets keep their locks, as decided for #319.docs/design/caching.mdnow says where the revision is checked, and that the backend runs with no Citry lock held.Tests
New file:
tests/test_ext_cache_backend_threads.py.setwaits, a reset and another cached render both finish.Both tests fail on main, where the reset waits for the write. The second test also fails if the check after the write is removed.
The replay rollback path is covered by the existing single-threaded test that rolls back a newly staged write.
Evidence
Run locally on origin/main 503c9ec, with a citry_core extension built from main:
-m "not e2e"): 9,476 passed.--platform linux.PR CI runs the cargo phases. This change has no Rust.
Review
An independent adversarial review ran twice, each time with a separate prose pass. Its findings are all folded in:
🤖 Generated with Claude Code