fix: prevent git fetch from corrupting mirror snapshots#189
Merged
Conversation
jrobotham-square
approved these changes
Mar 13, 2026
generateAndUploadMirrorSnapshot tars the live bare mirror directory while git fetch can run concurrently on a separate scheduler queue. When git fetch replaces packed-refs (via temp file + rename) mid-tar, the archive captures a truncated file. Restoring this snapshot produces a mirror with an unterminated packed-refs line, breaking all subsequent git operations. Add WithFetchExclusion() which holds the repo's fetch semaphore, preventing concurrent fetches while the tar runs. This ensures the snapshot captures a consistent view of the mirror directory. Amp-Thread-ID: https://ampcode.com/threads/T-019ce597-b4ff-72ad-b096-d2851a7058ff Co-authored-by: Amp <amp@ampcode.com>
ab9d98a to
b1131ce
Compare
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
generateAndUploadMirrorSnapshottars the live bare mirror directory usingWithReadLock, butgit fetchdoesn't hold a write lock during the actual fetch — it only briefly locks to updatelastFetch. Since fetch runs on a separate scheduler queue (upstream+"/fetch"), it can execute concurrently with the tar.When
git fetchreplacespacked-refs(via temp file + rename) whiletaris reading it, the archive captures a truncated file. Restoring this snapshot produces a mirror with an unterminatedpacked-refsline:This breaks all subsequent git operations (
fetch,for-each-ref,clone) on the restored mirror.Fix
Add
WithFetchExclusion()toRepository— acquires the fetch semaphore to prevent concurrentgit fetchwhile the caller runs. Used ingenerateAndUploadMirrorSnapshotto ensuretarcaptures a consistent view of the mirror directory.Workstation snapshots (
generateAndUploadSnapshot) are not affected because theygit clonethe mirror into a separate temp directory before tar-ing.