Repository navigation
fix(cache): ignore ErrNotFound in purgeNarInfo store deletes - #1275
Conversation
purgeNarInfo guards each store delete with a Has* check, but the check and the delete are not atomic. A concurrent goroutine can remove the file between the check (true) and the delete call, causing deleteNarFromStore (which re-checks internally) to return ErrNotFound. This surfaces as an intermittent "error removing nar from store: not found" test failure. Drop the outer Has* guards and treat ErrNotFound as success on both the NarInfo and Nar store deletes. Purge is idempotent by intent: if the file is already gone, the goal is achieved.
|
This change is part of the following stack: Change managed by git-spice. |
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe PR hardens the ChangesStorage cleanup race condition handling
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
Code Review
This pull request refactors the purgeNarInfo function in pkg/cache/cache.go to eliminate time-of-check to time-of-use (TOCTOU) race conditions. Instead of checking for existence before deletion, it now directly attempts to delete the NAR info and NAR files from the store, safely ignoring storage.ErrNotFound errors to ensure the purge operation is idempotent. There are no review comments to address.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pkg/cache/cache.go`:
- Around line 4338-4347: Add a regression test for purgeNarInfo that ensures
idempotent handling of storage.ErrNotFound: write a test in the same package
that constructs a cache instance (or table-driven setup) where
deleteNarInfoFromStore and deleteNarFromStore are mocked/stubbed to return
storage.ErrNotFound, set narURL.Hash to a non-empty value so both delete
branches run, call purgeNarInfo(ctx, hash) and assert it returns no error; use
the existing storage.ErrNotFound symbol and the purgeNarInfo,
deleteNarInfoFromStore, deleteNarFromStore identifiers so the test directly
verifies the code path where both deletes return ErrNotFound and the function
succeeds.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
Summary
purgeNarInfoguarded each store delete with aHas*check, but the check and the delete are not atomic. A concurrent goroutine can remove the file betweenHasNarInStorereturningtrueanddeleteNarFromStorebeing called — which re-checks internally and returnsErrNotFound. This surfaced as an intermittent"error removing nar from store: not found"test failure in bothncps-mysql-testsandncps-redis-testson aarch64.Has*guards and treatErrNotFoundas success on both the NarInfo and Nar store deletes. Purge is idempotent by intent.Test plan
task fmt/task lint/task testpassTestCacheBackends/.../narinfo_with_transparent_encryptionno longer flakes