Skip to content

fix(cache): ignore ErrNotFound in purgeNarInfo store deletes - #1275

Merged
kalbasit merged 1 commit into
mainfrom
user/wnasreddine/fix-purge-nar-race
May 25, 2026
Merged

kalbasit merged 1 commit into
mainfrom
user/wnasreddine/fix-purge-nar-race

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Summary

  • purgeNarInfo guarded each store delete with a Has* check, but the check and the delete are not atomic. A concurrent goroutine can remove the file between HasNarInStore returning true and deleteNarFromStore being called — which re-checks internally and returns ErrNotFound. This surfaced as an intermittent "error removing nar from store: not found" test failure in both ncps-mysql-tests and ncps-redis-tests on aarch64.
  • Fix: drop the outer Has* guards and treat ErrNotFound as success on both the NarInfo and Nar store deletes. Purge is idempotent by intent.

Test plan

  • task fmt / task lint / task test pass
  • Integration test TestCacheBackends/.../narinfo_with_transparent_encryption no longer flakes

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.
@kalbasit

kalbasit commented May 25, 2026 •

Copy link
Copy Markdown
Owner Author

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working go Pull requests that update go code labels May 25, 2026
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced cache maintenance resilience by making cleanup operations idempotent and handling concurrent access scenarios more gracefully
    • Improved error handling during storage operations to prevent race condition-related failures

Walkthrough

The PR hardens the purgeNarInfo cache cleanup function against concurrent access by making storage deletion operations idempotent. Narinfo and nar files are now unconditionally deleted from storage after DB transaction completion, and storage.ErrNotFound errors are treated as non-fatal to avoid races and TOCTOU conditions.

Changes

Storage cleanup race condition handling

Layer / File(s) Summary
Idempotent storage deletion in purgeNarInfo
pkg/cache/cache.go
narinfo and nar deletion logic is changed to proceed unconditionally and ignore storage.ErrNotFound errors, eliminating the race window between DB transaction completion and storage backend deletion.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • kalbasit/ncps#1201: Both PRs modify pkg/cache/cache.go's deletion flow in purgeNarInfo (and related deleteNarInfoFromStore logic), with the main PR adjusting store-delete idempotency/ErrNotFound handling while #1201 converts the corresponding DB/NarInfo+NarFile delete paths to Ent within the same cleanup sequence.

Suggested labels

bug, go, size:S, merge-stack

Poem

A rabbit hopped through cache so deep,
Found race conditions in cleanup's sweep,
Now deletes run once, no matter the dance,
ErrNotFound won't ruin the prance! 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: ignoring ErrNotFound errors in the purgeNarInfo store deletion operations to fix a race condition.
Description check ✅ Passed The description clearly explains the race condition causing the issue and the idempotent fix applied, directly related to the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@kalbasit kalbasit mentioned this pull request May 25, 2026
7 tasks done

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5447e6c8-d9b2-4355-9c06-b899f71e918e

📥 Commits

Reviewing files that changed from the base of the PR and between 754fc3a and 3b59990.

📒 Files selected for processing (1)
  • pkg/cache/cache.go

Comment thread pkg/cache/cache.go Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code merge-stack size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant