fix(autopilot): cancel the between-cycle sleep on SIGTERM/SIGINT (#204)#208
Open
sharziki wants to merge 1 commit intogarrytan:masterfrom
Open
fix(autopilot): cancel the between-cycle sleep on SIGTERM/SIGINT (#204)#208sharziki wants to merge 1 commit intogarrytan:masterfrom
sharziki wants to merge 1 commit intogarrytan:masterfrom
Conversation
The autopilot loop wrapped its between-cycle wait in `await new Promise(r => setTimeout(r, interval * 1000))`, which is not cancelable. When SIGTERM/SIGINT fired, the shutdown handler flipped `stopping = true`, but the `while (!stopping)` guard only re-evaluates after the current sleep resolves. With adaptive intervals scaling up to 600s on a healthy brain, that means systemd's default TimeoutStopSec=90 loses the race, SIGKILL preempts the drain path, and the autopilot lockfile at `~/.gbrain/autopilot.lock` is left stale. The next invocation has to either wait out the 10-minute staleness check or be cleaned up by hand. Extract an exported `sleepCancelable(ms, signal)` helper that resolves after `ms` or rejects with an AbortError as soon as `signal` aborts. Wire an `AbortController` into the cycle loop, abort it from the `shutdown` handler, and swallow only the abort-path rejection so the loop exits on its next `stopping` check. Added focused tests covering the never-aborted, pre-aborted, and mid-sleep abort paths. Fixes garrytan#204
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.
Summary
Fixes #204 — under systemd,
gbrain autopilotgets SIGKILL'd before the drain path runs because the between-cyclesetTimeoutis not cancelable.Root cause
The autopilot loop waited for the next cycle with:
The SIGTERM/SIGINT handler flips `stopping = true`, but the `while (!stopping)` guard only re-evaluates after the current sleep resolves. With adaptive intervals scaling up to 600s on a healthy brain, systemd's default `TimeoutStopSec=90` loses the race, SIGKILL preempts the shutdown path, and the lockfile at `~/.gbrain/autopilot.lock` is left stale — the next invocation then has to either wait out the 10-minute staleness check or be cleaned up by hand.
Fix
No behavior change in the worker-drain path (35s `Promise.race` stays intentional — it bounds how long we wait for the Minions worker to drain).
Test plan
🤖 Generated with Claude Code