Skip to content

fix(experimental): keep CacheStore coherent for every inherited write - #494

Open
d-v-b wants to merge 7 commits into
mainfrom
fix/cachestore-passthrough-mutators
Open

d-v-b wants to merge 7 commits into
mainfrom
fix/cachestore-passthrough-mutators

Conversation

@d-v-b

@d-v-b d-v-b commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

CacheStore keeps its cache consistent with the source store for every write and delete it inherits: set_if_not_exists and _set_many now update the cache, and set_sync and delete_sync raise TypeError instead of writing around it. Its size budget no longer leaks when entries are deleted, and concurrent reads no longer raise KeyError when an entry is evicted.

Stack: zarr-developers#4696 merged upstream (12b4721) and has been merged in here, so this PR now shows only its own changes. Fork draft #495 is stacked on top of this PR.

Agent notes

Why. CacheStore subclasses WrapperStore, whose write and delete methods forward straight to the source store. Each one CacheStore doesn't override bypasses the cache, and later reads serve stale data. zarr-developers#4696 fixes clear and delete_dir. A review of zarr-developers#4696 found the rest, and adversarial review of this branch found two older bugs on main.

Commits.

  • fix(experimental): keep CacheStore coherent for every inherited write:
    • _set_many writes through set.
    • set_if_not_exists drops the key's cached entries, because whether the source wrote isn't reported.
    • set_sync and delete_sync raise TypeError; _supports_sync_io already keeps zarr's own code off them.
    • A test classifies every WrapperStore and Store method as safe to inherit or required to be overridden, so a newly added method fails the test until someone decides which it is.
  • fix(experimental): release cached sizes in one place in CacheStore. Fixes a leak in current_size:
    • _remove_from_tracking dropped entries without subtracting their size. delete, set with cache_set_data=False, and a read of a missing key all leaked budget on main.
    • The previous commit made set_if_not_exists a frequent new caller, since zarr calls it on the parent group's metadata for every array created. The leak then grew until eviction emptied the cache.
    • The fix: _remove_from_tracking now releases the size and every path goes through it.
    • _track_entry releases the entry it replaces before making room. Previously, eviction could delete the new value from the cache store and release the old size twice.
    • _set_many keeps only the last value for a repeated key.
  • fix(experimental): check CacheStore LRU membership under the lock. On main, a cache hit could raise KeyError from move_to_end when a concurrent miss evicted the entry while the hit waited for the lock.

Not addressed (all also on main).

  • Stale value after a race: a read miss racing a set/delete of the same key can cache the old value: the source is read outside the lock, and the cache is written afterwards.
  • Oversized values: values larger than max_size are written to the cache store before the size check, untracked.
  • Budget drift: a few concurrent interleavings leave untracked data in the cache store or tracking with nothing behind it. Values stay correct.

All three come from the same design: tracking and cache-store writes happen as separate, unlocked steps across many call sites. Fixing them as a class means one locked "install entry / drop entry" pair, with a per-key version counter to reject stale fills from read misses.

Behavior change: CacheStore.set_sync / delete_sync used to write through to a sync-capable source; they now raise. CacheStore is experimental, and the old behavior served stale reads afterwards.

Verification. tests/test_experimental/test_cache_store.py: 81 passed. The new tests fail on zarr-developers#4696's head (10 failures), and the size-accounting tests also fail on the first commit alone (7 failures), which is how the leak was confirmed. tests/test_experimental, tests/test_store/test_wrapper.py and tests/test_store/test_logging.py: 262 passed. mypy and ruff (pre-commit pin) clean.

Adversarial review. Three rounds by fresh reviewer agents.

  • Round 1: one serious finding, confirmed in the code and by test. set_if_not_exists invalidation exposed the size leak in _remove_from_tracking. Fixed in the second commit.
    • Minor: the classification test didn't cover methods inherited from Store (fixed); a misleading test comment (fixed); repeated keys in _set_many could leave cache and source different (fixed, last-wins).
    • Noted, not changed: a race in _invalidate_key between its three steps, which predates this branch.
  • Round 2: nothing serious introduced. A 3000-example Hypothesis state machine and a 3000-seed concurrency probe kept the size invariant and read correctness on this branch; upstream main fails the same probes immediately.
    • It found the KeyError race on main (fixed in the third commit).
    • It flagged a test gap for the _track_entry change (test added in the third commit).
    • The other main bugs it found are listed above.
  • Round 3 (fix diff only): nothing serious. No deadlock, since the two callers never hold the lock. The concurrency test fails with KeyError when the fix is reverted.

🤖 Generated with Claude Code

Harshit-2118 and others added 6 commits December 4, 2026 14:18
CacheStore inherits WrapperStore's mutating methods, which forward straight
to the source store. Each one it does not override writes or deletes around
the cache, so later reads serve stale data; zarr-developers#4696 fixed this for clear and
delete_dir.

Close the rest:
- `_set_many` writes each pair through `set`.
- `set_if_not_exists` drops the cached entries for its key, since whether
  the source wrote is not reported.
- `set_sync` and `delete_sync` raise `TypeError` instead of bypassing the
  cache; `_supports_sync_io` already keeps zarr's own code off them.

A test classifies every WrapperStore method as safe to inherit or required
to be overridden, so a method added to WrapperStore fails until it is
handled.

Assisted-by: ClaudeCode:claude-opus-5-5
`_remove_from_tracking` dropped an entry from the size map without
subtracting it from `current_size`, so `delete`, `set` with
`cache_set_data=False`, and a missed read leaked budget. The previous
commit made `set_if_not_exists` a new caller, and zarr calls it on the
parent group's metadata for every array created, so the leak grew until
eviction emptied the cache.

`_remove_from_tracking` now releases the size, and `_evict_key`,
`_invalidate_range_entries`, `delete_dir` and `_track_entry` all go
through it. `_track_entry` releases the entry it replaces before making
room, so eviction cannot release that size twice. `_set_many` writes only
the last value for a repeated key.

Tests check `current_size == sum(key_sizes)` after every mutator, with
and without `cache_set_data`, and across repeated array creation. The
classification test now covers methods inherited from `Store` as well.

Assisted-by: ClaudeCode:claude-opus-5-5
`_update_access_order` tested `entry_key in cache_order` before awaiting
the lock, so a concurrent miss that evicted the entry while the hit waited
made `move_to_end` raise `KeyError` out of a plain read. Test membership
inside the lock.

Also test that replacing a tracked value under size pressure keeps the new
value cached with its size counted once; no test covered that change from
the previous commit.

Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5
…through-mutators

# Conflicts:
#	src/zarr/experimental/cache_store.py
#	tests/test_experimental/test_cache_store.py
@d-v-b
d-v-b marked this pull request as ready for review December 4, 2026 15:12

This branch has not been deployed

No deployments
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.

2 participants