fix(jobscheduler): cancel periodic re-arm goroutines on shutdown#311
Merged
Conversation
The graceful-shutdown change in #307 exposed a latent bug: after SIGTERM the scheduler sets q.done=true and workers exit, but the in-process re-arm goroutine spawned by SubmitPeriodicJob is a bare 'go func() { time.Sleep(interval); ... }()' with no awareness of ctx. It dies with the pod, and the next pod's warmExistingRepos re-registers each periodic job — but periodicDelay reads the bbolt lastRun and sleeps the remaining interval before the next firing. Net effect: each periodic job skips up to one full interval per pod restart, dropping background snapshot/repack/fetch throughput by ~90% during and after a rolling deploy. Fix: - Store the cancellable ctx on RootScheduler. - Replace time.Sleep in the re-arm and initial-delay goroutines with a select on q.ctx.Done() and a timer. - Drop Submit() calls once q.done is set so re-arm goroutines that win the race against ctx cancellation can't enqueue to a dead scheduler. Adds regression tests for both behaviours. Amp-Thread-ID: https://ampcode.com/threads/T-019e2cde-c5c3-729a-9364-202a128cf43e Co-authored-by: Amp <amp@ampcode.com>
joshfriend
approved these changes
May 15, 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.
The graceful-shutdown change in #307 exposed a latent bug: after SIGTERM the scheduler sets
q.done=trueand workers exit, but the in-process re-arm goroutine spawned bySubmitPeriodicJobis a barego func() { time.Sleep(interval); ... }()with no awareness ofctx. It dies with the pod, and the next pod'swarmExistingReposre-registers each periodic job — butperiodicDelayreads the bboltlastRunand sleeps the remaining interval before the next firing. Net effect: each periodic job skips up to one full interval per pod restart, dropping background snapshot/repack/fetch throughput sharply during and after rolling deploys.ctxonRootScheduler.time.Sleepin the re-arm and initial-delay goroutines with aselectonq.ctx.Done()and a timer (newsleepThenSubmithelper).Submit()calls onceq.doneis set so re-arm goroutines that win the race against ctx cancellation can't enqueue to a dead scheduler.Adds regression tests for both behaviours.