Skip to content

fix: call the cache backend with no Citry lock held - #335

Merged
JuroOravec merged 2 commits into
mainfrom
fix/cache-backend-outside-lock
Oct 24, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/cache-backend-outside-lock

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #316

What changes

With Component.Cache or <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 when Citry.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.

  • Checking for a reset. CacheExtension._unchanged_revision (in ext/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.
  • Storing a cached render. If the revision moved, _publish_component deletes the entry it just stored. The key names the old revision, so no new lookup would read it anyway.
  • Replay. If the revision moved, ext/cache/replay.py undoes only the keys this replay created that are marked rollback_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.
  • Failed cleanup deletes are best-effort: a backend error there is logged, not raised.
  • 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.md now 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.

  • A slow backend write blocks nothing else. While a backend set waits, a reset and another cached render both finish.
  • A reset during a slow write is not undone. After the write returns, the entry it stored across the reset is gone, and the next render shows the new template.

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:

  • pytest over the citry and citry_ui suites (-m "not e2e"): 9,476 passed.
  • uv lock --check, ruff check, ruff format --check.
  • mypy, including --platform linux.
  • citry-client, protocol contracts, protocol Python copies, events protocol JavaScript.
  • validators.

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:

  • the over-broad replay rollback;
  • best-effort cleanup deletes;
  • a test that did not prove its claim;
  • stale design-doc text;
  • wording.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 24, 2026 18:27
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]>
@JuroOravec
JuroOravec merged commit ee9b16d into main Oct 24, 2026
20 of 26 checks passed
@JuroOravec
JuroOravec deleted the fix/cache-backend-outside-lock branch October 24, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache backend calls run while the cache extension's revision lock is held

1 participant