Repository navigation
Conversation
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)
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 Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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)
This was referenced Oct 17, 2026
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.
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(SDKtypes.LaunchSpecification) carriesUserDataandIamInstanceProfile. It has no TagSpecifications and noInstanceInitiatedShutdownBehavior. 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
DescribeInstancesandDescribeTags, both already granted unconditioned. The verdict is replacement only if every check passes:spawn:managed=true.SpotInstanceRequestId.resource-id=sir-…) includespawn:managed=trueandspawn:spot-request-type=persistent.spawn:original-instance-idstarts withi-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)SPOT REPLACEMENT DETECTEDbanner.spot_replacement_retirednotification through the request's tags (notify-url etc., copied from the original at launch).CancelSpotInstanceRequests, then runsshutdown -h now(shutdown /s /t 0on Windows).IAM
None changed.
Tests
pkg/spotrequest/replacement_test.gocovers 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.gocovers the retry loop.make check: only the known Docker-onlyTestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Dockerfails.make check-nestedand the ttl-reaper tests pass.exec.CommandContextcalls use literal arguments only.Also fixes the errcheck findings in
lambda/ttl-reaper/failures_test.go: 5 barerp.runcalls, of which golangci-lint showed 3 by default.Follow-up
#1007: a replacement re-runs the user-data-embedded
--commandbefore spored retires it.