Skip to content

Have spored retire a Spot replacement it finds itself running on (#995) - #1008

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

scttfrdmn merged 1 commit into
mainfrom
fix/995

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #995
Stacked on #996 (→ #991 → #981 → #961 → #951). Review only the top commit.

Unverified against a real Spot replacement. Everything below is tested with fakes only. The unknowns listed in #995 are still unknown. They are: whether the replacement runs the request's UserData, whether it gets the instance profile, and whether an OS shutdown terminates or stops it. No AWS resources were used.

This implements option 1 of #995. Option 2, the reaper condition on ec2:InstanceProfile, is not addressed.

How spored gets there

A persistent request's LaunchSpecification (SDK types.LaunchSpecification) carries UserData and IamInstanceProfile. It has no TagSpecifications and no InstanceInitiatedShutdownBehavior. So the replacement should boot spawn's bootstrap and start spored under spawn's role, with no spawn tags.

Detection (pkg/spotrequest.DetectReplacement)

spored runs this at startup, off the critical path. It uses only DescribeInstances and DescribeTags, both already granted unconditioned. The verdict is replacement only if every check passes:

  1. Its own instance tags were read, and they lack spawn:managed=true.
  2. It has a SpotInstanceRequestId.
  3. The request's tags (DescribeTags resource-id=sir-…) include spawn:managed=true and spawn:spot-request-type=persistent.
  4. The request's spawn:original-instance-id starts with i- and is not this instance's id.

The original can't match. It is tagged spawn:managed=true. Separately, its request's original-instance-id is either absent (not yet written) or its own id, and an instance id survives stop, start and hibernate. Any read error, an empty tag set, or a malformed id gives unknown. Unknown is retried 5 times, 30s apart, and then nothing is done. The reaper alarm from #984 still applies.

Retirement (RetireReplacement)

  • Logs a SPOT REPLACEMENT DETECTED banner.
  • Sends a spot_replacement_retired notification through the request's tags (notify-url etc., copied from the original at launch).
  • Cancels the request with the existing tag-scoped CancelSpotInstanceRequests, then runs shutdown -h now (shutdown /s /t 0 on Windows).
  • If the cancel fails, there is no shutdown: a shutdown with the request still open might relaunch another replacement. An already-cancelled request, or one EC2 no longer knows (NotFound), counts as a successful cancel.
  • Shutdown may stop rather than terminate, because the launch spec can't set the shutdown behaviour. A stopped replacement then bills EBS only, can't restart with its request cancelled, and stays in the reaper's report.

IAM

None changed.

Tests

  • pkg/spotrequest/replacement_test.go covers the original (6 variants), a replacement (cancel happens before shutdown), unknown/unreadable cases, request not managed or not persistent, request already cancelled or gone, and a failed cancel.
  • cmd/spored/spot_replacement_test.go covers the retry loop.
  • 12 compiling mutations of the guards each made a test fail.
  • make check: only the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker fails. make check-nested and the ttl-reaper tests pass.
  • Semgrep was not run locally. The new exec.CommandContext calls use literal arguments only.

Also fixes the errcheck findings in lambda/ttl-reaper/failures_test.go: 5 bare rp.run calls, of which golangci-lint showed 3 by default.

Follow-up

#1007: a replacement re-runs the user-data-embedded --command before spored retires it.

scttfrdmn added a commit that referenced this pull request Oct 17, 2026
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)
@scttfrdmn
scttfrdmn changed the base branch from fix/984 to main October 17, 2026 02:57
EC2 relaunches a leaked persistent Spot request from its LaunchSpecification,
which carries UserData and IamInstanceProfile but no tags. So spawn's
bootstrap and spored run on the replacement, under spawn's role, with no TTL
and outside every tag-scoped terminate grant.

At startup spored now asks whether it is such a replacement, using only
DescribeInstances and DescribeTags, which its role already grants
unconditioned. It is one only if: its own tags were read and lack
spawn:managed=true; it has a Spot request; that request is tagged
spawn:managed=true and spawn:spot-request-type=persistent; and the request's
spawn:original-instance-id is an instance id other than its own. An original
instance is tagged spawn:managed and its request names it or is not yet
tagged, so it can't match. Any read failure is "unknown" and does nothing;
inconclusive checks retry 5 times, 30s apart.

On a match it logs SPOT REPLACEMENT DETECTED, sends spot_replacement_retired
through the notification settings the request's tags carry, cancels the
request (the tag-scoped grant from #922 covers it) and only then shuts the OS
down. A failed cancel does not shut down. A request that is already cancelled,
or that EC2 no longer knows, still shuts down.

The launch specification has no InstanceInitiatedShutdownBehavior, so the
shutdown may stop the replacement rather than terminate it. A stopped one has
a cancelled request, bills EBS only and stays in the reaper's alarm. None of
this has been checked against a real Spot replacement.

Also fixes the errcheck findings in lambda/ttl-reaper/failures_test.go: five
bare rp.run calls (golangci-lint showed three of them by default).

No IAM changes.

(#995)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.58824% with 40 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/spored/spot_replacement.go 61.11% 19 Missing and 2 partials ⚠️
cmd/spored/main.go 0.00% 7 Missing ⚠️
pkg/spotrequest/replacement.go 91.30% 4 Missing and 2 partials ⚠️
pkg/provider/ec2.go 0.00% 4 Missing ⚠️
cmd/spored/shutdown_other.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit c6e9ae5 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/995 branch October 17, 2026 03:12
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
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)
scttfrdmn added a commit that referenced this pull request Oct 17, 2026
…1021)

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

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)

* Run the post-spored replacement stop after the plugin declarations (#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)

* Name the original instance in spot_replacement_retired (#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)

* Skip the bash-run Spot replacement bootstrap tests on Windows; note #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)

* Use the repo's bare nosemgrep on the spot-replacement test harness (#1007)
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.

Live-verify a way to end an untagged Spot replacement without a person (follow-up to #984)

1 participant