Repository navigation
fix(storage/local): don't wipe store/tmp on startup - #1313
Conversation
setupDirs() called os.RemoveAll(storeTMPPath()) on every boot, intending a clean slate. On a shared NFS PVC with two replicas, the incoming pod deleted in-flight downloads that the outgoing pod still owned, racing with active writes and crashing the rolling update. The downstream os.MkdirAll call already handles both the "dir exists" and "dir doesn't exist" cases idempotently, so removing RemoveAll is sufficient. Leftover partial files are benign because the write path uses an atomic tmp-then-rename pattern and partial files are never promoted to the NAR path. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
|
This change is part of the following stack: Change managed by git-spice. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR makes local storage directory initialization idempotent by removing the startup deletion of ChangesIdempotent Local Store Directory Setup
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 makes the local store directory initialization idempotent by removing the 'os.RemoveAll' call on the temporary directory in 'pkg/storage/local/local.go'. This change prevents rolling updates on shared persistent volumes from deleting active temporary files and causing crashloops. The test suite in 'pkg/storage/local/local_test.go' has been updated to assert that existing temporary files are preserved and that initialization can be safely run multiple times. There are no review comments to address, and I have no further 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.
|
Documentation Updates 1 document(s) were updated by changes in this PR: NAR File Download and StreamingView Changes@@ -191,7 +191,7 @@
When CDC is enabled and decompression is performed, the cleanup goroutine removes the uncompressed temporary file (`assetPath`) after all readers finish. The `compressedAssetPath` field is kept in the `downloadState` struct for backward compatibility but remains empty in the CDC pipeline since PR #1071 replaced the compressed temp file with an `io.Pipe`. This ensures that no temporary files persist after the NAR is stored.
-On startup, ncps cleans up any leftover temporary files from previous runs by removing the temp directory and recreating it. The server also validates that the temporary directory is writable during startup by creating and removing a test file. If the temporary directory is not writable, the server will fail to start immediately with an error, preventing silent failures that could occur later during NAR download operations. This boot-time validation ensures configuration issues are detected early, before the server begins accepting requests [source](https://github.com/kalbasit/ncps/blob/b7653d4303670bb88fd34b8bf82ed3aade09885d/pkg/storage/local/local.go#L305-L459).
+On startup, the temporary directory is created if it doesn't exist using `os.MkdirAll`, which is idempotent and will not modify the directory if it already exists. Any temporary files from previous runs are preserved rather than removed. This design supports shared storage scenarios (e.g., NFS PVC with multiple replicas) where removing the temp directory during startup could delete in-flight downloads owned by other pods, causing race conditions during rolling updates. Leftover partial files are harmless because the write path uses atomic tmp-then-rename, so they are never promoted to the NAR path. The server validates that the temporary directory is writable during startup by creating and removing a test file. If the temporary directory is not writable, the server will fail to start immediately with an error, preventing silent failures that could occur later during NAR download operations. This boot-time validation ensures configuration issues are detected early, before the server begins accepting requests [source](https://github.com/kalbasit/ncps/blob/b7653d4303670bb88fd34b8bf82ed3aade09885d/pkg/storage/local/local.go#L305-L459).
### Configuration of Temporary Download Paths
The temporary directory for downloads is configurable via the `--cache-temp-path` flag or the `CACHE_TEMP_PATH` environment variable. By default, ncps uses the OS temporary directory. For local storage backends, a `tmp` subdirectory under the store path is used [source](https://github.com/kalbasit/ncps/blob/b7653d4303670bb88fd34b8bf82ed3aade09885d/cmd/serve.go#L176-L529). |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
pkg/storage/local/local_test.go (1)
127-133:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winClose the temp file before asserting preservation.
os.CreateTempleaves the descriptor open here, and this subtest never closes it. That leaks an FD and can make cleanup flaky on platforms that don't allow deleting open files.Suggested fix
f, err := os.CreateTemp(filepath.Join(dir, "store", "tmp"), "hello") require.NoError(t, err) + require.NoError(t, f.Close()) _, err = local.New(newContext(), dir) require.NoError(t, err) assert.FileExists(t, f.Name())🤖 Prompt for 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. In `@pkg/storage/local/local_test.go` around lines 127 - 133, The temp file created with os.CreateTemp (variable f) is left open; close it before proceeding to avoid leaking file descriptors and flaky cleanup—call f.Close() (or defer f.Close() immediately after creation) before invoking local.New(newContext(), dir) and before asserting the file exists; update the test in pkg/storage/local/local_test.go to close f so the descriptor is released prior to the assertion.
🤖 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.
Outside diff comments:
In `@pkg/storage/local/local_test.go`:
- Around line 127-133: The temp file created with os.CreateTemp (variable f) is
left open; close it before proceeding to avoid leaking file descriptors and
flaky cleanup—call f.Close() (or defer f.Close() immediately after creation)
before invoking local.New(newContext(), dir) and before asserting the file
exists; update the test in pkg/storage/local/local_test.go to close f so the
descriptor is released prior to the assertion.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 03019660-24cc-4712-8cd5-013f9ca22587
📒 Files selected for processing (8)
openspec/changes/archive/2026-06-01-fix-store-tmp-mkdir-idempotent/.openspec.yamlopenspec/changes/archive/2026-06-01-fix-store-tmp-mkdir-idempotent/design.mdopenspec/changes/archive/2026-06-01-fix-store-tmp-mkdir-idempotent/proposal.mdopenspec/changes/archive/2026-06-01-fix-store-tmp-mkdir-idempotent/specs/local-store-initialization/spec.mdopenspec/changes/archive/2026-06-01-fix-store-tmp-mkdir-idempotent/tasks.mdopenspec/specs/local-store-initialization/spec.mdpkg/storage/local/local.gopkg/storage/local/local_test.go
💤 Files with no reviewable changes (1)
- pkg/storage/local/local.go
The "store/tmp files are preserved on boot" subtest left the os.CreateTemp descriptor open, leaking an FD and risking flaky cleanup on platforms that don't allow deleting open files. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
## 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)
Summary
os.RemoveAll(storeTMPPath())fromsetupDirs()inpkg/storage/localin-flight downloads owned by the outgoing pod during rolling updates,
racing with active writes and crashing on startup
os.MkdirAll(called immediately after) already handles both the"dir exists" and "dir doesn't exist" cases idempotently — the
RemoveAllwas never necessary for correctness, only for a "clean slate" that is
unsafe in a multi-pod topology
tmp-then-rename and partials are never promoted to the NAR path
Test plan
task fmt— no changestask lint— 0 issuestask test— all packages passTestSetupDirsIdempotent— callingNew()twice on same dir succeedsTestSetupDirsPreservesExistingTmpFiles— existing tmp files survive startupTestNew/store/tmp_files_are_preserved_on_boot— updated to assert new behaviour