Skip to content

Launch stoppable --spot as persistent Spot; cancel the request on every terminate (#922) - #951

Merged
scttfrdmn merged 6 commits into
mainfrom
fix/922
Oct 15, 2026
Merged

scttfrdmn merged 6 commits into
mainfrom
fix/922

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #922

Problem

Every --spot launch made a one-time Spot request, whose interruption behaviour is terminate. EC2 refuses to stop an instance from a one-time request. So none of these could work on Spot:

  • the idle timeout's default stop
  • --on-idle stop|hibernate
  • --on-complete stop|hibernate
  • spawn stop and spawn hibernate

Meanwhile spawn status said "stops the instance when idle".

Fix (issue option 1, plus options 3 and the status line)

Launch (pkg/aws/spot_lifecycle.go)

  • A Spot launch whose lifecycle stops or hibernates now sends SpotInstanceType=persistent with InstanceInterruptionBehavior=stop (hibernate when --hibernate configures it). "Stops or hibernates" means any of: an idle timeout greater than 0, --on-complete stop|hibernate, or --hibernate. The rule is pkg/spotrequest.NeedsStoppable.
  • The request is tagged with the instance's tags, spawn:managed=true included, so it can be found and the tag-conditioned cancel grants match it.
  • The instance gets spawn:spot-request-type=persistent|one-time.
  • A terminate-only --spot launch is unchanged: one-time, no interruption behaviour, no request tags.

Cancelling the persistent request on every terminate path. It is cancelled before the terminate; terminating first reopens the request. The shared code is in pkg/spotrequest.

path where on a refused cancel
CLI: terminate, cleanup, cohorts/taskpool/mpicohort, launch rollback, service, arraygroup, job arrays pkg/aws Client.Terminate Refuses to terminate a persistent request's instance and prints the aws ec2 cancel-spot-instance-requests command. A one-time request only warns, since it closes with its instance.
spored: TTL, on-complete terminate, cost limit pkg/provider EC2Provider.Terminate Terminates anyway and logs the request id. An expired instance must die.
TTL reaper lambda/ttl-reaper terminate(); the request id comes from the scan, with no extra Describe Terminates anyway and logs SPOT_CANCEL_FAILED with the request id (hard-deadline guarantee, #72).
dashboard DELETE lambda/dashboard-api Same as the CLI.

Sweep and pipeline orchestrator instances are launched one-time by the Lambdas themselves and are out of scope.

IAM. ec2:CancelSpotInstanceRequests is added, conditioned on ec2:ResourceTag/spawn:managed=true, to:

  • the spored baseline policy. CreateOrGetInstanceProfile re-applies it on every launch, which heals existing roles;
  • the shared spored/DCV role and scripts/setup-spawnd-iam-role.sh;
  • the portal onboarding role (cmd/onboard.go);
  • the reaper's scan-self policy (pkg/reaperiam, lambda/ttl-reaper/template.yaml) and the cross-account role (deployment/cloudformation/ttl-reaper-cross-account-role.yaml).

The reaper drift gates (TestPolicyCoversEveryReaperAPICall, TestCrossAccountRoleCoversEveryReaperAPICall) failed until those grants were added.

The reaper role doesn't allow the cancel yet. The live spawn-ttl-reaper-ec2 roles in onboarded accounts don't have the new grant until their stacks are redeployed. Until then the reaper still reaps; it logs SPOT_CANCEL_FAILED and the request stays open. Every request spawn creates persistent is tagged, so the condition will match once the roles are updated. #950 tracks a sweep for orphaned requests.

spawn stop / spawn hibernate on a one-time Spot instance now refuse before printing or calling anything. The message explains why, and suggests spawn terminate or a stop-based lifecycle at launch. The job-array path skips such instances with a message.

spawn status gives the real idle action: "cannot act: a one-time Spot instance can't be stopped …" for one-time Spot, and "hibernates" for --on-idle hibernate. Only the idle line changed, which keeps clear of #916's edits to the same function.

Tests

Each test was watched failing against a revert that compiles (16 reverts; all ran and failed):

  • pkg/aws, TestLaunch_SpotRequestShapeForEachLifecycle: runs the real Client.Launch against a fake EC2/STS endpoint and asserts the RunInstances wire parameters for 10 lifecycle combinations: SpotInstanceType, InstanceInterruptionBehavior, the request tag spec, and the type tag. Also TestLaunch_SpotMaxPriceKept.
  • pkg/aws, TestTerminate_*: describe, cancel, terminate in that order; refusal on a persistent request that can't be cancelled; one-time and untagged still terminate; on-demand makes no cancel call.
  • pkg/provider, TestEC2Provider_Terminate_*: spored cancels its own request first, and terminates even when the cancel is refused.
  • lambda/ttl-reaper, TestReap_*: driven through run(); cancel happens before terminate, a refused cancel still reaps and logs, on-demand makes no call.
  • lambda/dashboard-api, TestTerminateCancellingSpot_*.
  • cmd, TestStop_OneTimeSpotExplainsUpFront: no "Saving remaining TTL", zero AWS calls, and the message content. TestStop_PersistentSpotIsStopped, TestIdleActionDescription.
  • pkg/aws, TestSporedPoliciesGrantSpotCancelOnManagedRail; pkg/spotrequest unit tests.

make check: all green except the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker. make check-nested: all 9 nested modules clean. No AWS resources were touched.

Deploy needs

  • Reaper: redeploy the ttl-reaper Lambda (code), and the spawn-ttl-reaper-ec2 cross-account role stack in each onboarded account. Re-apply the scan-self policy wherever scan-self is used.
  • dashboard-api: redeploy the Lambda.
  • Portal onboarding role: existing onboarded accounts get the grant only when re-onboarded.
  • spored roles: heal automatically on the next launch, because the baseline is re-applied.

Notes

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 54.96183% with 59 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/spotrequest/spotrequest.go 18.46% 53 Missing ⚠️
cmd/state.go 72.22% 5 Missing ⚠️
cmd/onboard.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

…terminate (#922)

A --spot launch always made a one-time Spot request (interruption behaviour
terminate), and EC2 refuses to stop an instance from one. So the idle timeout's
default stop, --on-idle, --on-complete stop|hibernate, spawn stop and spawn
hibernate could never work on Spot, while spawn status promised "stops the
instance when idle".

- A Spot launch whose lifecycle stops or hibernates (idle timeout,
  --on-complete stop|hibernate, --hibernate) now makes a persistent request with
  InstanceInterruptionBehavior=stop (hibernate when configured). The request is
  tagged like the instance; the instance carries spawn:spot-request-type.
  Terminate-only --spot launches stay one-time.
- Every terminate path cancels the Spot request before terminating, so a
  persistent request cannot launch a replacement: pkg/aws Client.Terminate (all
  CLI paths; refuses if a persistent request cannot be cancelled), spored's
  self-terminate and the TTL reaper (best-effort, logged), and the dashboard.
  Shared in pkg/spotrequest.
- ec2:CancelSpotInstanceRequests on the spawn:managed rail for the spored role
  baseline, the DCV spored role, the portal onboarding role, and the reaper's
  scan-self and cross-account roles.
- spawn stop / hibernate on a one-time Spot instance refuse up front with an
  explanation instead of "Saving remaining TTL" then UnsupportedOperation.
- spawn status describes the real idle action for one-time Spot.

Fixes #922 (#922)
@scttfrdmn
scttfrdmn merged commit 55133d7 into main Oct 15, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the fix/922 branch October 15, 2026 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant