Skip to content

fix(cache): delete the stored variant on NAR eviction - #1341

Merged
kalbasit merged 1 commit into
mainfrom
fix-deletenar-orphaned-bytes
Jun 7, 2026
Merged

kalbasit merged 1 commit into
mainfrom
fix-deletenar-orphaned-bytes

Conversation

@kalbasit

@kalbasit kalbasit commented Jun 7, 2026

Copy link
Copy Markdown
Owner

Summary

Cache.DeleteNar removed only the exact URL variant via narStore.DeleteNar, but a Compression:none NAR's physical object is stored under the .nar.zst variant. Deleting nar/<H>.nar therefore left the real .nar.zst blob orphaned on disk while still clearing bytes_stored_at — leaking storage and making the /upload presence check lie (report absent while bytes remain). It also spuriously errored, since the bare none object never exists.

This deletes the actually-stored variant (the .nar.zst variant for none, mirroring statNarInStore) and tracks whether any variant was present, returning ErrNotFound only when none was — preserving the established "deleting an absent NAR errors" contract while no longer orphaning the blob.

Surfaced by a CodeRabbit "outside diff range" finding on #1331; scoped out of that PR as pre-existing, now fixed on its own.

Specs: adds a requirement to upload-reference-presence (deletion removes the stored variant before clearing the marker; not-found only when no variant was present).

Test plan

  • task fmt, task lint, task test pass
  • New: TestDeleteNar_RemovesNoneNarZstdVariant (none NAR removes the .nar.zst object)
  • New: TestDeleteNar_AbsentNoneNarReturnsNotFound (contract: absent → ErrNotFound, marker kept)
  • Existing xz DeleteNar "absent → ErrNotFound" backend test still passes

DeleteNar removed only the exact URL variant via narStore.DeleteNar, but
a Compression:none NAR's physical object is stored under the .nar.zst
variant. Deleting nar/<H>.nar therefore left the real .nar.zst blob
orphaned on disk while still clearing bytes_stored_at — leaking storage
and making the /upload presence check lie (report absent while bytes
remain). It also spuriously errored, since the bare none object never
exists.

Delete the actually-stored variant (the .nar.zst variant for none,
mirroring statNarInStore) and track whether any variant was present,
returning ErrNotFound only when none was — preserving the established
'deleting an absent NAR errors' contract while no longer orphaning the
blob. Surfaced by CodeRabbit review on PR #1331.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@kalbasit

kalbasit commented Jun 7, 2026 •

Copy link
Copy Markdown
Owner Author

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working go Pull requests that update go code labels Jun 7, 2026
@coderabbitai

coderabbitai Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cb0e72b2-befc-4630-8f1e-82743b11cc05

📥 Commits

Reviewing files that changed from the base of the PR and between 04cb40b and 3f05dc7.

📒 Files selected for processing (8)
  • openspec/changes/archive/2026-06-06-fix-deletenar-orphaned-bytes/.openspec.yaml
  • openspec/changes/archive/2026-06-06-fix-deletenar-orphaned-bytes/design.md
  • openspec/changes/archive/2026-06-06-fix-deletenar-orphaned-bytes/proposal.md
  • openspec/changes/archive/2026-06-06-fix-deletenar-orphaned-bytes/specs/upload-reference-presence/spec.md
  • openspec/changes/archive/2026-06-06-fix-deletenar-orphaned-bytes/tasks.md
  • openspec/specs/upload-reference-presence/spec.md
  • pkg/cache/cache.go
  • pkg/cache/nondestructive_narinfo_purge_internal_test.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where deleting certain NAR files would leave orphaned data behind on disk. The deletion process now properly removes all physical copies of a NAR before clearing the storage marker, preventing storage leaks.
  • Documentation

    • Added design documentation and specification updates detailing the corrected NAR deletion behavior and error handling.

Walkthrough

This PR fixes a bug in Cache.DeleteNar where deleting a Compression:none NAR would clear the bytes_stored_at marker but leave the underlying .nar.zst blob orphaned in storage. The fix ensures the actual physical variant is deleted first, then the marker is cleared only after at least one delete succeeds, and returns not-found only when no variant exists.

Changes

Fix Cache.DeleteNar orphaned-bytes handling

Layer / File(s) Summary
Problem definition and design approach
openspec/changes/archive/2026-06-06-fix-deletenar-orphaned-bytes/.openspec.yaml, design.md, proposal.md, tasks.md
Archive documents outline the orphaned .nar.zst blob issue for Compression:none NARs, proposed fix approach of variant-aware deletion before clearing bytes_stored_at, and TDD task checklist with reproduction, fix, and verification steps.
Specification requirements for DeleteNar deletion semantics
openspec/specs/upload-reference-presence/spec.md
Updated spec defines required deletion order (remove physically stored compression variant before clearing bytes_stored_at), error-handling rules (return not-found only if no variant exists and preserve marker on failure), and scenarios validating the fix behavior.
DeleteNar implementation and variant-aware deletion tests
pkg/cache/cache.go, pkg/cache/nondestructive_narinfo_purge_internal_test.go
DeleteNar now conditionally deletes the Zstd variant for Compression:none NARs, tracks whether any delete succeeded, and clears bytes_stored_at only after successful variant deletion. New tests verify .nar.zst removal and not-found semantics with marker preservation.

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • kalbasit/ncps#1330: Introduces nar_file.bytes_stored_at marker and updates NAR deletion to clear it, which directly relates to this PR's fix ensuring the marker is only cleared after physical variant deletion for Compression:none.

Suggested labels

bug, go

Poem

🐰 A NAR sits stored in zstd's disguise,
Yet marked as none—a byte-sized surprise!
Delete it right, clear markers with care,
No orphans left floating in storage air. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: fixing Cache.DeleteNar to delete the stored variant (.nar.zst for Compression:none) during NAR eviction, which is the core purpose of this PR.
Description check ✅ Passed The description provides clear context on the bug (orphaned .nar.zst blobs when deleting Compression:none NARs), the fix applied, and test coverage, all directly related to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

@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 addresses an issue where deleting a Compression:none NAR left its physical .nar.zst variant orphaned on disk while clearing the bytes_stored_at marker. The DeleteNar function in pkg/cache/cache.go has been updated to delete the .nar.zst variant for none NARs before clearing the marker, and to return storage.ErrNotFound only if no variant was present. Corresponding unit tests have been added to verify this behavior. There are no review comments to address, and I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@kalbasit
kalbasit enabled auto-merge (squash) June 7, 2026 04:54
@kalbasit
kalbasit merged commit 10b0fab into main Jun 7, 2026
42 checks passed
@kalbasit
kalbasit deleted the fix-deletenar-orphaned-bytes branch June 7, 2026 04:55
@codecov

codecov Bot commented Jun 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (04cb40b) to head (3f05dc7).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff      @@
##   main   #1341   +/-   ##
============================
============================

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

kalbasit added a commit that referenced this pull request Jun 8, 2026
## Summary

The `[Unreleased]` section was populated in #1309 but skewed toward the
recent CDC-drain, in-flight-staging, and Ent-migration work, omitting
several user-facing features and fixes that also landed since v0.9.4.
This backfills the missing notable changes, each cross-referenced by PR.

**Added**
- `ncps fsck` command + Helm fsck CronJob (#975, #996, #998, #999,
#1000, #1004, #1006, #1176)
- Closure pinning HTTP endpoints `POST`/`DELETE /pin/{hash}.narinfo`,
`GET /pins` (#1108)
- pprof server via `--pprof-addr` (#1063)
- CDC lazy chunking as a feature + `--cache-cdc-lazy-cleanup-schedule` +
recovery cron (#1082, #1083, #1089, #1096)
- Helm: auto `GOMEMLIMIT` (#1061), configurable temp volume /
`volumeClaimTemplate` (#1074, #1077), SQLite `Recreate` strategy (#1012)
- Startup storage reporting + deprecated-narinfo warning (#959, #961)
- `docker-dev` image (#1097)

**Fixed**
- `store/tmp` no longer wiped on startup (#1313)
- `nix-hash` NAR verification (#1101)
- Prometheus counters primed at startup (#1337, #1343)
- 404 on un-reassemblable chunked NARs (#1319), purged narinfo no longer
500 (#1318), fail-fast tmp validation (#1086)
- Further CDC/cache reliability fixes (#1317, #1346, #1341, #1342,
#1331, #1347)

**Changed**
- CDC config now persisted in the DB config table (#971)

Dependency bumps, CI/test-only changes, and the internal Ent-conversion
PRs are intentionally excluded; the existing Ent-migration entry already
covers that work.

## Test plan

- [x] `nix fmt` reports no changes (docs-only edit)
- [x] CHANGELOG section structure intact
(Added/Fixed/Changed/Removed/Migration/CI)
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:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant