Skip to content

Announce an on-complete stop once and back off a failed one, sharing the idle stop's backoff (#978) - #1005

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/978
Oct 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/978

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #978. Stacked on #986 (fix/954) → #977 → #969 → #960 → #955 → #936. The base is fix/954, so the diff shows only #978.

What was wrong

checkCompletion runs whenever the completion file exists. After --on-complete stop or hibernate fails (a one-time Spot instance, a role without ec2:StopInstances), the file is still there, so every one-minute tick repeated all of it:

  • another completion notification and another POST to the completion webhook, which a caller can treat as "done" each time;
  • another copy of a failed job's command log to the serial console;
  • another grace-period sleep, which held up the TTL and cost-limit checks every tick;
  • another stop attempt.

What changed (pkg/agent/agent.go)

  • The announcement runs once. The log line, the console copy, the completion notification, the webhook, the user warning and the grace period move into announceCompletion, behind completionAnnounced.
  • Back off a failed idle stop, announce only success, and restore DNS on failure (#948) #977's idle backoff is now a shared mechanism. stopRetry holds the failure count and retryAt. stopWithBackoff runs a stop/hibernate closure, sets the 5/10/20/40-minute-then-hourly delay (stopRetryDelay, renamed from idleActionRetryDelay), and sends the trigger's failure notification on the first failure only. idleTimeoutReached and checkCompletion both use it. Because the closure keeps each a.stop/a.hibernate call at its call site, TestEveryLifecycleExitPathCarriesAReasonToken still sees each taskproto.Exit* token.
  • There is one backoff per agent, shared by both triggers. Whether EC2 will stop the instance does not depend on why spored asked. So a refused on-complete stop also holds off the idle stop, and the failure is reported once.
  • While backing off, checkCompletion returns false, so the TTL and cost-limit checks still run. They are the backstop for an instance that cannot be stopped.
  • A failed on-complete stop or hibernate sends completion_stop_failed once.
  • --on-complete terminate is unchanged and still retries every tick, as the issue scopes.

Tests (pkg/agent/completion_stop_retry_test.go)

  • TestFailedCompletionStopIsAnnouncedOnceAndBacksOff (stop and hibernate): over 5 ticks there is 1 completion notification, 1 webhook POST, 1 grace sleep (timed against a 300 ms grace period), 1 attempt and 1 completion_stop_failed. When the backoff runs out it retries without announcing again.
  • TestTTLStillEnforcedWhileCompletionStopBacksOff: through checkAndAct, an expired TTL terminates while the on-complete stop is backing off.
  • TestIdleStopSharesTheCompletionStopBackoff: an idle stop inside the on-complete backoff makes no call, and only one failure notification is sent.
  • TestSuccessfulCompletionStop.

Revert checks. Each revert compiled, and the test ran and failed:

revert failing tests
announce every tick, no backoff, return true while backing off FailedCompletionStopIsAnnouncedOnceAndBacksOff (both subtests: 5 notifications, 5 webhooks, 1.5 s of grace sleep, 5 attempts), TTLStillEnforcedWhileCompletionStopBacksOff
idle keeps its own backoff IdleStopSharesTheCompletionStopBackoff (2 calls, 2 failure notifications)

Checks

Caveat

After a successful on-complete hibernate and a resume, the completion file is still on disk, so the next tick hibernates again. That was already the case before this PR, and the announcement is no longer repeated. It belongs to the resume fix (#987, stacked on this one).

@scttfrdmn

Copy link
Copy Markdown
Contributor Author

Heads-up: this adds a new spored notification event, completion_stop_failed, which spore-bot doesn't handle yet (it would fall back to the generic message). Filed spore-host/spore-host#563. Once #1011 (the #1004 gate) merges, this PR needs that issue fixed and testdata/spore-bot/spored_events.txt re-vendored before it can go green.

scttfrdmn added a commit that referenced this pull request Oct 16, 2026
)

TestSporedNotifyEventsHandledBySporeBot parses every non-test Go file of the
root module, finds each Notifier.Notify call and resolves its event argument
(string literals, consts, locals assigned in the function such as
idle_stopped/idle_hibernated, and struct fields such as #1005's
stopAttempt.failEvent). It fails on an event missing from spore-bot's list, on
an argument it cannot resolve, on a Notify method value, on a second Notify
method, and on empty discovery.

spore-bot's list is vendored at testdata/spore-bot/spored_events.txt so the
unit test is offline; TestSporeBotEventsVendoredCopyIsCurrent runs in CI with
SPORE_BOT_EVENTS_SYNC=1, fetches spore-host main and fails on drift.

(#1004)
scttfrdmn added a commit that referenced this pull request Oct 16, 2026
)

TestSporedNotifyEventsHandledBySporeBot parses every non-test Go file of the
root module, finds each Notifier.Notify call and resolves its event argument
(string literals, consts, locals assigned in the function such as
idle_stopped/idle_hibernated, and struct fields such as #1005's
stopAttempt.failEvent). It fails on an event missing from spore-bot's list, on
an argument it cannot resolve, on a Notify method value, on a second Notify
method, and on empty discovery.

spore-bot's list is vendored at testdata/spore-bot/spored_events.txt so the
unit test is offline; TestSporeBotEventsVendoredCopyIsCurrent runs in CI with
SPORE_BOT_EVENTS_SYNC=1, fetches spore-host main and fails on drift.

(#1004)
scttfrdmn added a commit that referenced this pull request Oct 16, 2026
) (#1011)

* Gate that every spored notification event is handled by spore-bot (#1004)

TestSporedNotifyEventsHandledBySporeBot parses every non-test Go file of the
root module, finds each Notifier.Notify call and resolves its event argument
(string literals, consts, locals assigned in the function such as
idle_stopped/idle_hibernated, and struct fields such as #1005's
stopAttempt.failEvent). It fails on an event missing from spore-bot's list, on
an argument it cannot resolve, on a Notify method value, on a second Notify
method, and on empty discovery.

spore-bot's list is vendored at testdata/spore-bot/spored_events.txt so the
unit test is offline; TestSporeBotEventsVendoredCopyIsCurrent runs in CI with
SPORE_BOT_EVENTS_SYNC=1, fetches spore-host main and fails on drift.

(#1004)

* Run the spore-bot event gate under the hermetic wrapper; put the network sync check behind -tags=spore_bot_sync (#1004)

* Re-vendor spore-bot's event list (completion_stop_failed, spot_replacement_retired) (#1004)
@scttfrdmn
scttfrdmn force-pushed the fix/954 branch 2 times, most recently from 8fdcffe to 77d3562 Compare October 17, 2026 01:57
@scttfrdmn
scttfrdmn changed the base branch from fix/954 to main October 17, 2026 09:38
…the idle stop's backoff (#978)

checkCompletion runs whenever the completion file exists, and the file
stays on disk after a stop or hibernate that EC2 refused. So every
one-minute tick sent another `completion` notification, POSTed the
completion webhook again, copied the command log to the console again,
slept the grace period again (holding up the TTL and cost-limit checks)
and tried the stop again.

- The announcement (log, console copy of a failed job's log,
  `completion` notification, webhook, user warning, grace period) moves
  into announceCompletion and runs once per spored.
- #948's idle backoff becomes a shared mechanism: stopRetry (failures,
  retryAt) and stopWithBackoff, which runs a stop/hibernate closure,
  records a failure with the 5/10/20/40-minute-then-hourly delay, and
  sends the trigger's failure notification on the first failure only.
  The closure keeps each a.stop/a.hibernate call, and its
  taskproto.Exit* token, at the call site.
- One stopRetry per agent, shared by both triggers: whether EC2 will
  stop the instance does not depend on why spored asked, so a refused
  on-complete stop also holds off the idle stop and the failure is
  reported once.
- While backing off, checkCompletion returns false so the TTL and
  cost-limit checks run; they are the backstop for an instance that
  cannot be stopped.
- A failed on-complete stop sends `completion_stop_failed` once.
- on-complete terminate is unchanged: retried every tick.

(#978)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
pkg/agent/agent.go 96.66% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit df7c059 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/978 branch October 17, 2026 09:55
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.

A failed --on-complete stop/hibernate repeats the completion notification, webhook, grace period and stop on every tick

1 participant