Skip to content

Stop a Spot replacement from re-running the user's workload (#1007) - #1021

Merged
scttfrdmn merged 5 commits into
mainfrom
fix/1007
Oct 17, 2026
Merged

scttfrdmn merged 5 commits into
mainfrom
fix/1007

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1007
Stacked on #1008. Review and merge #1008 first; this PR's base is fix/995.

What

  • spored is-spot-replacement: runs spotrequest.DetectReplacement and exits 0 for a replacement, 1 for not a replacement, 2 for unknown. It retries an inconclusive read every 5s for up to --timeout (default 60s). An IAM refusal (UnauthorizedOperation/AccessDenied*) is not retried. It reads IMDS and EC2 directly rather than through provider.NewEC2Provider, so an IMDS failure reports unknown instead of falling back to the local provider.
  • Linux bootstrap (pkg/launcher/bootstrap.go): a spawn_is_spot_replacement shell function, called in two places:
    1. inside the backgrounded --command subshell, before the gates and the command. Inside the subshell, a slow check can't delay spored's start (spored#65). A skipped command writes no SPAWN_COMPLETE.
    2. after systemctl start spored and the plugin declarations. On a replacement the script exits there, so storage, the container, custom user data and the MPI, batch-queue and pool-worker scripts appended after the bootstrap don't run either. The stop comes after plugins.json because spored reads that file only at startup: the check's EC2 round trip, placed ahead of the declarations, would make every original miss its plugins. TestSpotReplacementStopComesAfterPluginDeclarations gates the order, and the underlying race is filed as Linux bootstrap writes /etc/spawn/plugins.json after starting spored, which reads it only at startup #1023.
      Definite answers (0 or 1) are cached in /run/spawn, which is tmpfs.
  • Windows bootstrap: no change. It starts no workload: buildWindowsUserData runs no --command and no custom user data. A comment says to gate any future workload start the same way.
  • Detection.Err, so the subcommand can tell an IAM refusal apart from a transient failure.

Unknown: run the workload, with a bounded retry

Only exit 0 skips the workload. Unknown runs it, and so does a missing spored, an older spored without the subcommand (cobra exits 1; checked) and a timeout 120 hit. Holding instead would strand a real original whenever EC2 or IMDS is slow, or a custom --iam-role can't call DescribeInstances. Originals vastly outnumber replacements. A replacement that slips through is still retired by spored's own check (#1008) and reported by the reaper's alarm (#984). An original normally answers on the first DescribeInstances, because it is tagged spawn:managed=true. So the retry budget is only spent on a launch that is already degraded.

Cost: one IMDS read and one or two EC2 calls before the post-spored steps of every launch.

Tests

  • pkg/launcher/bootstrap_spot_replacement_test.go runs the real generated --command block and the post-spored tail under bash. spored and su are stubbed, and absolute paths are rewritten into a temp dir. Cases cover spored exiting 0, 1, 2, 3 and 124, and spored being missing. They also check that the guard sits inside the subshell ahead of su, that only definite answers are cached, and that the script passes bash -n.
  • cmd/spored/is_spot_replacement_test.go: the exit-code contract, the retry bound (fake clock), and no retry on an IAM refusal. Also TestRunDaemonStartsTheReplacementCheck (the gap Have spored retire a Spot replacement it finds itself running on (#995) #1008 noted): it reads runDaemon's AST and requires go replacementCheck{...}.run(...) with shutdown: powerOff and r/notify/sleep set, started before agent.NewAgent.
  • Each new test was watched failing against a revert that compiles (go vet clean). Reverts tried: the command guard changed to if false; the stop script replaced with _ =; unknown failing closed; runDaemon's go statement replaced with _ = replacementCheck{}; the exit mapping swapped; the cache disabled.
  • make check: the only failure is the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker. make check-nested passes; golangci-lint reports 0 issues on the touched packages.

Filed

Not verified

Nothing here has been run against a real Spot replacement. As with #995, it assumes cloud-init runs user data on the replacement.

A replacement EC2 launches from a leaked persistent Spot request boots the
original's user data. The bootstrap started --command in the background
before spored, so the replacement re-ran the workload from the start, side
effects included, while spored was deciding to retire it.

New `spored is-spot-replacement` runs spotrequest.DetectReplacement and
exits 0 (replacement), 1 (not) or 2 (unknown). It retries an inconclusive
read every 5s for up to --timeout (60s), and not at all on an IAM refusal.

The Linux bootstrap calls it twice through spawn_is_spot_replacement:
inside the backgrounded --command subshell, ahead of the command, so it
never delays spored's start; and right after spored starts, where a
replacement exits the script. So storage, plugins, the container, custom
user data and the MPI, batch-queue and pool-worker scripts appended later
don't run either. Definite answers are cached in /run/spawn.

Only exit 0 skips. Unknown, a missing spored, an older spored without the
subcommand (cobra exits 1) and a timeout all run the workload, as before.
Holding would strand originals whenever EC2/IMDS is slow or a custom role
can't read tags; a replacement that gets past it is still retired by
spored and alarmed by the reaper.

The Windows bootstrap starts no workload (it runs no --command or user
data), so it has nothing to gate; a comment says what to do if it gains one.

Also adds TestRunDaemonStartsTheReplacementCheck, which fails if runDaemon
stops starting the #995 check (the gap #1008 noted), and a Detection.Err
field so the subcommand can tell an IAM refusal from a transient failure.

The bootstrap tests run the generated --command block and the post-spored
tail under bash with stubbed spored and su; each new test was watched
failing against a compiling revert.

(#1007)
…1007)

spored reads /etc/spawn/plugins.json once, at startup, and the bootstrap
writes it just after `systemctl start spored`. Putting the stop's EC2 round
trip between the two would have made spored miss the declarations on every
original instance. The stop now follows the plugins block; a gate keeps it
there. The existing race is filed as #1023.

(#1007)
The notification comes from the replacement, whose instance id nobody
registered with spore-bot, so the bot's registration gate dropped every
DM and SMS for it. spored now also sends original_instance_id (via
Notifier.WithOriginalInstance), which spore-bot uses for this event alone.
Older bots ignore the unknown field.

(#1007)
…1023's plugin order (#1007)

The sandboxed bootstrap tests execute the Linux user data under bash with
stubbed spored and su, which the Windows CI job (#973) cannot reproduce, so
they skip there as the other Linux-bootstrap execution tests do (#1002).
Rebased onto #1023/#1045, plugins.json is written before spored starts, so
the post-spored stop sits straight after the bootstrap body; the changelog
and the ordering test's comment now say so.

(#1007)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.82353% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/spored/is_spot_replacement.go 72.88% 13 Missing and 3 partials ⚠️
cmd/spored/spot_replacement.go 0.00% 1 Missing ⚠️
pkg/agent/notifier.go 95.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit c505671 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1007 branch October 17, 2026 03:43
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 Spot replacement re-runs the embedded --command workload before spored can retire it

1 participant