Skip to content

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

Closed

Description

@JuroOravec

What happens

With Component.Cache or <c-cache> in use, Citry calls the configured
cache backend (citry.cache) while it holds the cache extension's revision
lock. That lock is also taken:

  • by every render that builds a cache key (CacheExtension._revision_snapshot), and
  • by every reset (reset_template(), reset_files(), Citry.clear()), which
    holds it for the whole reset.

So a slow backend call, such as a network round trip to Redis, holds up
cached renders on every other thread of the engine, and every hot reload
waits for it. A backend that itself waits for another thread that renders
a cached component would hang both threads.

Where

On main (25595af):

  • packages/py/citry/citry/ext/cache/extension.py:192: _stable_revision()
    holds _revision_lock while the code inside it runs.
  • packages/py/citry/citry/ext/cache/extension.py:530: citry.cache.set(...) /
    citry.cache.delete(...) run inside _stable_revision().
  • packages/py/citry/citry/ext/cache/replay.py:678: cache.get, cache.set
    and cache.delete run inside _stable_revision().
  • packages/py/citry/citry/citry.py Citry.clear(): self.cache.clear() runs
    inside _render_cache_invalidation(), which holds the same lock.

Why the lock is there

_stable_revision(expected) makes one store or replay atomic with respect
to a reset: if a reset moved the revision between the key snapshot and the
store, the store raises _CacheRevisionChanged instead of writing an entry
built from old content. That guarantee must stay.

Suggested direction

Use the same rule as #292 / #293 / #315: no user-provided code under a Citry
lock. The cache backend is user-provided.

  • Read and write the backend with no lock held.
  • Check the revision before and after the backend call, under the lock but
    without the call inside it. If it moved, treat the result as stale: drop a
    read, and delete or overwrite a write that may have landed after the reset.
    A backend write tagged with the revision it was built under would let a
    later reader reject it.
  • Keep Citry.clear()'s backend clear() after the invalidation commits.

What would show this direction is wrong: a reset during a slow backend set
that leaves the old entry readable afterwards. A regression test should
start a slow set (a backend whose set waits on an event), run
reset_template() on another thread, release the set, and check that the
next render does not replay the old output. A second test should show that
a cached render on another thread does not wait for the slow set.

Error modes to decide

  • A backend call that raises while the revision moved: report the backend
    error, or treat it as a miss?
  • A backend whose delete fails during the stale-write cleanup: the entry
    may stay readable; the revision tag above is what protects readers then.

Found during the lock audit for #292, #293 and #315.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions