Repository navigation
libstore: re-check free space after auto-GC acquires the GC lock - #14
Merged
Merged
Conversation
autoGC computed maxFreed before blocking on the exclusive gc.lock, and collectGarbage never re-checked free space once it won the lock. The gcRunning guard is per-process, so each nix-daemon worker that observed low disk queued its own full GC pass behind the lock — the "GC herd": after the first pass reclaimed the space, N more passes still ran. Add GCOptions::skipIfAvailAbove: when set, collectGarbage re-checks filesystem free space right after acquiring gc.lock and returns early if enough is free. autoGC sets it to minFree so queued passes skip themselves once an earlier collector has done the work. getAvail is factored into LocalStore::getStoreAvail() for reuse. Assisted-by: devin:claude Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
ethancedwards8
approved these changes
Sep 16, 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.
Motivation
On a busy CI runner,
min-freeauto-GC produces a thundering herd: every daemon worker process independently noticesavail < min-free, and each one queues for the exclusivegc.lockto run its own full GC pass. The first pass reclaims the space, but the queued passes still run —collectGarbagenever re-checks disk after acquiring the lock — so a 700k-path store stays wedged for hours while N-1 redundant full passes drain (each holding the exclusive lock and blocking every build/copy on the host). This is what took downnix-runner-x86-1today: ~70 queued passes, 6+ hours after free space was already back abovemax-free.The
gcRunningguard inautoGCdoes not help: it is per-process, and every daemon worker is its own process.Context
Fix: let auto-GC ask
collectGarbageto re-check free space after it holds the lock, and return without collecting if space is already fine. The first waiter does the real pass; every other one takes the lock for milliseconds and moves on.min-freestays useful as a safety net with no herd.struct GCOptions { uint64_t maxFreed{...}; + std::optional<uint64_t> skipIfAvailAbove; // internal, not on the daemon wire }; void LocalStore::collectGarbage(const GCOptions & options, GCResults & results) { FdLock gcLock(fdGCLock.get(), ltWrite, true, "waiting for the big garbage collector lock..."); + if (options.skipIfAvailAbove && getStoreAvail() >= *options.skipIfAvailAbove) { + printInfo("skipping auto-GC: %d bytes free after acquiring the GC lock (threshold %d)", ...); + return; + } void LocalStore::autoGC(bool sync) { ... options.maxFreed = gcSettings.maxFree - avail; + options.skipIfAvailAbove = gcSettings.minFree;autoGCis factored intoLocalStore::getStoreAvail()socollectGarbagecan reuse it (same_NIX_TEST_FREE_SPACE_FILEoverride andstatvfspath).nix-store --gc/nix store gcare unchanged.daemon.cc/remote-store.ccuntouched), so no wire-format change.Test:
tests/functional/gc-auto-skip.sh— process A holds the GC lock (via the_NIX_TEST_GC_SYNC_2fifo), process B's build triggers auto-GC and queues behind it, fake free space is raised abovemin-freebefore A releases; asserts B logsskipping auto-GCand none of the garbage paths were deleted.gc-auto.shandgc-non-blocking.shstill pass. Ran insidenix develop .#native-clangStdenv(the default gcc devshell has a preexisting-Wchanges-meaningerror inclosure.hh, unrelated to this change).Companion operational fix (already merged): exa-labs/monorepo#146910 reaps orphaned
nix-daemon --stdiosessions. Rolling this out requires bumping thenix-srcrev pinned in the Colmena flake.Link to Devin session: https://app.devin.ai/sessions/cb6b9562567c47a898ccb1ed0a050c9a
Open in Devin Desktop: https://app.devin.ai/desktop/session/cb6b9562567c47a898ccb1ed0a050c9a?variant=devin
Requested by: @schew2381