Skip to content

fix(agent): region-vacated check never delays Terminate (#1456) - #1460

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/1456-region-vacated-nonblocking
Oct 31, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/1456-region-vacated-nonblocking

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1456

Problem

terminate ran checkRegionVacated after the pre-stop hook, cleanup and the 5 s warning, and when no other spawn:managed instance was running or pending in the region it blocked 60 s (regionVacatedSettle) before provider.Terminate, even with no notifier configured. The last spawn instance in a region was billed an extra idle minute on every TTL, cost-limit, idle and on-complete termination.

Change (pkg/agent/region_vacated.go, terminate, checkCompletion)

  • No notifier (a.notifier == nil, i.e. spawn:notify-url unset; spored has no per-event subscription, the bot filters, so a notifier is the only signal): the check does not run. No DescribeInstances, no wait.
  • With a notifier: a probe starts where the termination is decided: in checkCompletion before announceCompletion (so before --completion-delay) for on-complete terminate, at the top of terminate for TTL, cost limit and idle terminate. It takes the first count in a goroutine. terminate cuts the settle at its user warning, so the re-count runs during the existing 5 s warning sleep. After the sleep it cancels the probe and reads the verdict. A count still in flight returns as unknown and is treated as "not vacated", so a slow EC2 API never holds Terminate. region_vacated is still sent before Terminate, from the same literal Notify call that TestSporedNotifyEventsHandledBySporeBot discovers.
  • What changes in the false-alarm protection (feat(app): container-based launch + --app-version on shared DCV base AMI (#290) #260): the rule is the same: two empty counts, one at the start of the termination and one at its end, and a relaunch in between suppresses the alarm. The window is now the existing waits (grace period, pre-stop, task flush, cleanup), not those plus 60 s. With no pre-stop that is about 30 s for on-complete terminate and close to 0 s for TTL, cost-limit and idle terminate. So a relaunch that lands 0-60 s after the old window would have closed can now get a region_vacated message (notify-only) where it did not before. The trade is deliberate: cost control over a chat message.
  • regionVacatedSettle is removed; the probe has no timer.

Latency, completion file to Terminate call (defaults, no pre-stop)

Case Before After
others in region 35-95 s 35-95 s
last in region, no notifier 95-155 s 35-95 s
last in region, notifier 95-155 s 35-95 s (+ goroutine wake-up after cancel)

TTL, cost-limit and idle terminate: the 60 s comes off the same way (tick phase + pre-stop + cleanup + 5 s). Stop/hibernate never ran the check. A Spot interruption is terminated by EC2, not spored.

Gates (pkg/agent/region_vacated_test.go), counted through the stub provider, the recording notifier and the graceSleep seam, never timed; each runs over all four paths (TTL, cost limit, idle terminate, on-complete terminate)

  • TestRegionVacatedCheckCostsNothingWithoutNotifier: no notifier, empty region → terminated, 0 counts.
  • TestRegionVacatedNotificationWithNotifier: vacated → exactly one region_vacated, already sent when Terminate is called; relaunch during the settle ({0, 1}) → none; others remain → none (1 count); unknown → none.
  • TestRegionVacatedSettleOverlapsCompletionDelay: the probe is running during the --completion-delay wait.
  • TestRegionVacatedCheckNeverHoldsTerminate: count 1 or count 2 blocks until its context is cancelled → Terminate is still called, and no region_vacated is sent.

Compiling reverts, each watched failing:

  • drop the a.notifier == nil guard → CostsNothing fails on all 4 paths ("2 region counts with no notifier configured, want 0");
  • replace the re-count with n := 0 → relaunch during settle fails on all 4 paths;
  • don't start the probe in checkCompletion → Overlaps fails ("not running during the --completion-delay wait");
  • drop p.cancel() in vacated() → NeverHolds hangs in (*regionVacatedProbe).vacated (panic: test timed out at -timeout 30s);
  • full revert of agent.go to origin/main, with region_vacated.go and the Overlaps test removed so it compiles: CostsNothing/ttl FAILS after 60.00 s (the billed minute, 2 counts); relaunch during settle passes (the protection is preserved); NeverHolds hangs in the old synchronous count.

Also

Checks: make check (from /private/tmp) OK, make lint 0 issues, semgrep scan --config=auto --error . clean, go test -race -count=3 on the agent lifecycle tests OK. No help text, exported API or user-data changes.

@codecov

codecov Bot commented Oct 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/agent/region_vacated.go 92.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 5a1c1e5 into main Oct 31, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1456-region-vacated-nonblocking branch October 31, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document on-complete termination latency (~1–1.5 min after --command exit) and where spored logs

1 participant