fix(cache): Write files atomically and mutex cache reads - #50
Merged
Conversation
Thanks Henrik <3
There was a problem hiding this comment.
Pull request overview
Adds atomic cache-file publication and per-artifact synchronization to prevent partial reads and duplicate downloads.
Changes:
- Introduces atomic temporary-file writes with explicit commit.
- Adds keyed locking and cache-miss rechecks.
- Updates cache test doubles for the new writer interface.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
services/modules/cache.go |
Implements atomic writes and synchronized downloads. |
services/modules/cache_test.go |
Updates mocks for commit-based cache writes. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Changes: 1. **Atomic writes (fixes read-while-writing).** `FileStorage.Create` now returns a new `CacheWriter` interface (`io.Writer` + `Commit()` + `Close()`). `StoreInPath.Create` writes to a temp file (`os.CreateTemp` in the same dir) and only `os.Rename`s it into place on `Commit()`. Since rename is atomic on the same filesystem, readers see either the complete old file or the complete new file — never a partial one. Closing without committing discards the temp file, so failed/partial downloads leave no garbage. 2. **Ref-counted keyed mutex (fixes write/write + dedups downloads).** `Cache` gained a `map[string]*keyLock` guarded by a `sync.Mutex`. `lockKey(filename)` serializes work per-artifact and cleans up the entry once no goroutine holds or waits on it. On a cache miss, only one goroutine downloads; others block, then serve the freshly cached file. 3. **Rewrote `ProxyDownload`** into: verify `RepoHead` → serve-from-cache fast path → acquire per-file lock → re-check cache → download-to-cache → serve → fall back to direct proxy if caching is unavailable. `RepoHead` now runs up front on every path, so cached content is never served without an access check. Prompt: > Do we need a mutex or similar to protect against two go routines writing to the same file and/or reading from a file while writing in `ProxyDownload`?
nikolaik
force-pushed
the
nikolaik/ashamed-waves
branch
from
August 18, 2026 07:44
6559d3e to
5bd6901
Compare
marcusramberg
approved these changes
Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Atomic writes (fixes read-while-writing).
FileStorage.Createnow returns a newCacheWriterinterface (io.Writer+Commit()+Close()).StoreInPath.Createwrites to a temp file (os.CreateTempin the same dir) and onlyos.Renames it into place onCommit(). Since rename is atomic on the same filesystem, readers see either the complete old file or the complete new file — never a partial one. Closing without committing discards the temp file, so failed/partial downloads leave no garbage.Ref-counted keyed mutex (fixes write/write + dedups downloads).
Cachegained amap[string]*keyLockguarded by async.Mutex.lockKey(filename)serializes work per-artifact and cleans up the entry once no goroutine holds or waits on it. On a cache miss, only one goroutine downloads; others block, then serve the freshly cached file.Rewrote
ProxyDownloadinto: verifyRepoHead→ serve-from-cache fast path → acquire per-file lock → re-check cache → download-to-cache → serve → fall back to direct proxy if caching is unavailable.RepoHeadnow runs up front on every path, so cached content is never served without an access check.Prompt