fix: prevent concurrent snapshot restores from corrupting packed-refs#188
Merged
fix: prevent concurrent snapshot restores from corrupting packed-refs#188
Conversation
ensureCloneReady allowed multiple concurrent goroutines to enter startClone simultaneously. Each goroutine extracted the mirror snapshot tarball into the same directory, corrupting packed-refs when one goroutine's tar overwrote the file while another was running git fetch. Add TryStartCloning() to atomically gate the StateEmpty→StateCloning transition so only the winning goroutine performs the restore or clone. Update MarkRestored and Clone to accept StateCloning (since the caller already transitioned) so the fallback from failed restore to fresh clone still works. Amp-Thread-ID: https://ampcode.com/threads/T-019ce597-b4ff-72ad-b096-d2851a7058ff Co-authored-by: Amp <amp@ampcode.com>
jrobotham-square
approved these changes
Mar 13, 2026
alecthomas
approved these changes
Mar 13, 2026
worstell
added a commit
that referenced
this pull request
Mar 16, 2026
## Problem When a corrupt or empty mirror snapshot exists in S3, pods enter a poison cycle: 1. Pod restores corrupt snapshot → 80KB empty mirror (zero refs, no pack files) 2. Post-restore `git fetch` fails — `lowSpeedLimit` (1KB/s for 60s) trips during server-side pack computation for the large delta 3. Code logs a warning but proceeds: schedules snapshot jobs that immediately re-upload the empty mirror to S3 4. Next pod restart (or S3 cleanup) restores the same empty snapshot → repeat This cycle survived the fixes in #188 and #189 because those PRs prevented the *creation* of corruption (concurrent restores and concurrent fetch-during-tar) but not the *propagation* of already-corrupt snapshots. ## Fix Three changes break the cycle: 1. **`FetchLenient`**: Post-restore and startup fetches omit the `lowSpeedLimit` check, matching `executeClone`'s behavior. Large deltas after snapshot restore trigger GitHub's server-side pack computation which stalls at near-zero transfer rate for minutes, tripping the 1KB/s threshold. 2. **`ResetToEmpty` + fallback to clone**: When the post-restore fetch fails, the corrupt mirror directory is removed and the repo state is reset to Empty. The code then falls through to a fresh `git clone --mirror` instead of serving and re-uploading stale data. 3. **Skip snapshot scheduling on failed fetch**: Snapshot and repack jobs are only scheduled after a successful fetch, both in the startup path (`DiscoverExisting`) and the post-restore path (`startClone`). ## Testing All existing tests pass. The fix was validated against staging logs showing the poison cycle in action. Co-authored-by: Amp <amp@ampcode.com>
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.
Problem
ensureCloneReady(used by the snapshot HTTP endpoint) callsstartClonesynchronously on the request goroutine. When multiple concurrent snapshot requests arrive for the same repo, each goroutine entersstartCloneand extracts the mirror snapshot tarball into the same directory simultaneously. This corruptspacked-refs— one goroutine'startruncates the file while another is runninggit fetchon it:Once
packed-refsis corrupt,git fetch,git for-each-ref, andgit clone(for workstation snapshot generation) all fail, causing empty or error responses to clients.Fix
TryStartCloning()toRepository— atomically transitionsStateEmpty → StateCloning, returning true only for the winning goroutinestartClonewithTryStartCloning()so only one goroutine performs the restore/clone; others return immediately and wait inensureCloneReady's poll loopMarkRestoredandCloneto acceptStateCloning(sinceTryStartCloningalready transitioned), preserving the fallback path from failed restore to fresh clone