Skip to content

Reap orphaned persistent Spot requests and bound them with ValidUntil (#950) - #981

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

scttfrdmn merged 1 commit into
mainfrom
fix/950

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #950

Stacked on #961 (base fix/949), which is stacked on #951 (fix/922). Merge in that order.

Do replacements inherit tags? (the issue's open question)

Not verified live, because the cost rules forbid launching anything. The evidence points to no. RunInstances' instance TagSpecifications are applied when RunInstances runs. A Spot request's LaunchSpecification, which is what EC2 relaunches from, has no tag field at all (checked in the SDK's types.LaunchSpecification). EC2's docs say a request's own tags are not copied to the instances it launches. So the sweep handles both cases:

  • untagged replacement (expected): found because the request's current instance has no spawn:managed=true;
  • tagged replacement (in case tags are inherited): found because the request's new spawn:original-instance-id tag, written right after RunInstances, names a different instance.

What changes

TTL reaper sweep (lambda/ttl-reaper/spotrequests.go). Per account and region, after the instance reaps, the sweep runs DescribeSpotInstanceRequests with filters tag:spawn:managed=true, type=persistent, state=open|active|disabled|cancelled. It then runs a single DescribeInstances filtered on instance-id. It uses a filter, not InstanceIds, because one unknown ID fails the whole InstanceIds call.

  • instance terminated, shutting down or unknown → cancel
  • open with no instance, past a 15-minute grace → cancel
  • replacement → cancel, then terminate it, in that order. If the cancel fails there is no terminate, because terminating would reopen the request.
  • live original that is past its own deadline or max-age (via evaluate) → cancel (the instance scan terminates it)
  • live original inside its deadline → left alone, even if the request's own spawn:ttl-deadline is past. That tag goes stale after spawn extend, and cancelling would strand the instance at its next stop. This is a deliberate deviation from the issue's literal "cancel when the request's ttl-deadline is past", and there is a test for it.
  • a cancelled request whose replacement is still running → terminate again, or report it again.

IAM, tag-scoped as asked. The only new grant is the read-only ec2:DescribeSpotInstanceRequests, which has no resource-level ARN. Cancelling uses the #922 tag-conditioned ec2:CancelSpotInstanceRequests, and terminating uses the existing tag-conditioned ec2:TerminateInstances. As a result an untagged replacement cannot be terminated by the reaper. The sweep cancels its request so nothing more launches. Then it logs the alarmed sentinel SPOT REPLACEMENT NOT TERMINATED with the exact aws ec2 terminate-instances command, every run, until a person ends the instance (follow-up: #984). A role without the describe grant is reported once per account as REAPER SPOT REQUESTS UNREACHABLE (alarmed) and is not counted in Errors (#469). Both new sentinels have metric filters and alarms.

Staged like NetResources. The new parameter is SpotRequests / REAPER_SPOT_REQUESTS: report (default) logs and notifies what it would do, reap acts, and off skips the sweep. DryRun always wins. The parameter is added to the Makefile PARAM_MAP, the deploy echo, and the "plain deploy emits no mode parameter" gate.

ValidUntil. A persistent request now gets ValidUntil set to the TTL deadline (the same instant as spawn:ttl-deadline). With no TTL it is launch + 7 days, the reaper's default ceiling, kept in step with pkg/ttl by a test. The instance and the request carry spawn:spot-valid-until.

  • Interaction with spawn extend: EC2 has no API to move a request's end date. So extending past it prints a warning that once the request expires, a stop (the idle timeout, spawn stop, a Spot interruption) may not be restartable.
  • Interaction with spawn start: if EC2 refuses to start the instance after that point, the error names the expired request as the likely cause.

New gate: TestEveryTemplateParameterIsReferenced. Removing the REAPER_SPOT_REQUESTS environment line passed every existing test, because the parameter was still declared and mapped but never reached the function.

Tests (each seen to fail against a revert that compiles)

  • In the reaper module, spotrequests_test.go covers 10 decision cases plus grace, filters, the instance-id filter vs InstanceIds, report/dry-run inertness, off, a failed cancel blocking the terminate, a stranded replacement reported on two consecutive runs, a failed lookup acting on nothing, and through run() the denied sentinel once per account and the sweep wiring.
  • Reverts that made these fail: the original-id check, the untagged check, using the request's stale deadline, the grace, report-only, the cancel-failure continue, recordSpot, the run() call, the lookup-failure guard, the expired-original branch, InstanceIds instead of the filter, and the stranded branch.
  • pkg/reaperiam drift gates fail when the grant is dropped from either the scan-self policy or the cross-account role. TestEverySentinelHasAMetricFilter fails when a filter is renamed.
  • pkg/aws: ValidUntil equals the TTL deadline; without a TTL it is the 7-day ceiling; one-time requests send none; the persistent request is tagged with spawn:original-instance-id. Each fails against a revert.
  • pkg/spotrequest: ExtendWarning and StartFailureNote. cmd: startFailedError.

Commands, all run with AWS credentials stripped (see spawn#968): make check passes except the known Docker-only TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker. make check-nested reports all 9 modules clean. lambda/ttl-reaper and lambda/dashboard-api go test ./... pass.

Deploy steps (NOT done)

  1. Cross-account role first, in every spore-launching account (today 435415984226): update the spawn-ttl-reaper-ec2 stack from deployment/cloudformation/ttl-reaper-cross-account-role.yaml. It adds ec2:DescribeSpotInstanceRequests, and also carries Launch stoppable --spot as persistent Spot; cancel the request on every terminate (#922) #951's ec2:CancelSpotInstanceRequests if that hasn't been applied yet. Done the other way round, the new Lambda raises REAPER SPOT REQUESTS UNREACHABLE until the role catches up, which is harmless but noisy.
  2. Reaper Lambda in the infra account (966362334030, us-east-1): cd lambda/ttl-reaper && make deploy ENV=production with no new variables. SpotRequests arrives as report, and every other parameter keeps its live value (lambda/ttl-reaper: 'make deploy' defaults would DISARM the live production reaper and silently drop its DNS/alarm config #650). Check the echoed SpotRequests report and DryRun false lines.
  3. Read the report. Look for WOULD cancel orphaned spot request lines and spot_requests_* in the run summary in /aws/lambda/spawn-ttl-reaper-production across a few runs (10-minute schedule). Confirm both new alarms exist: spawn-ttl-reaper-production-spot-requests-unreachable and -spot-replacement-stranded.
  4. Arm it: make deploy ENV=production SPOT_REQUESTS=reap.
  5. Self-hosted reapers (spawn reaper deploy): re-run it to pick up the scan-self policy change. These reapers don't set REAPER_SPOT_REQUESTS, so they run the sweep in report.
  6. CLI release: ValidUntil, the request tagging and the extend/start messages ship with the next spawn release. They need no IAM change: callers already hold tag-conditioned ec2:CreateTags, and the request carries spawn:managed=true.

Caveats

…#950)

A persistent Spot request whose instance was terminated outside spawn reopens
and makes EC2 launch an untagged replacement, invisible to spawn list and the
reaper, and nothing cancelled it.

TTL reaper: a new staged sweep (REAPER_SPOT_REQUESTS / SpotRequests, default
report) describes spawn-tagged persistent requests and cancels those whose
instance is gone, replaced (spawn:original-instance-id mismatch, or missing
spawn:managed) or past its own deadline/max-age, then terminates a replacement
after the cancel. It never cancels a live original's request on the request's
stale ttl-deadline (spawn extend updates only the instance). An untagged
replacement is outside the tag-scoped terminate grant, so it raises the
alarmed SPOT REPLACEMENT NOT TERMINATED sentinel every run. A role lacking
ec2:DescribeSpotInstanceRequests raises REAPER SPOT REQUESTS UNREACHABLE. That
read-only grant is added to the scan-self policy, the SAM template and the
cross-account role.

Launch: persistent requests get ValidUntil = the TTL deadline (or launch + the
7-day ceiling) and spawn:spot-valid-until, and are tagged with
spawn:original-instance-id after RunInstances. spawn extend warns past
ValidUntil, and spawn start names an expired request as the likely cause.

Also gates that every template parameter is referenced.

(#950)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.23077% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/aws/spot_lifecycle.go 82.35% 2 Missing and 1 partial ⚠️
cmd/extend.go 0.00% 2 Missing ⚠️
cmd/state.go 66.66% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

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.

Nothing reclaims an orphaned persistent Spot request (instance terminated outside spawn); consider a reaper sweep

1 participant