Repository navigation
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Oct 15, 2026
Merged
Merged
A pipeline stage instance terminated itself only at the end of its success path. A failing or timed-out command, or a bootstrap failure under set -e, exited first. The orchestrator never read the FAILED marker, and its fallback read instance states recorded at launch and never refreshed, so the stage stayed running and the Lambda re-invoked itself every ~13 minutes. The instances carried no spawn:managed tag, so the TTL reaper could not see them, and the spored role's ec2:TerminateInstances is conditioned on that tag, so self-termination was refused by IAM as well. The instance id came from ec2-metadata, which Ubuntu and Debian do not ship. The user data now installs an EXIT trap before anything can fail. It writes FAILED unless COMPLETE was written, terminates the instance through the API with its id read from IMDSv2, and halts. RunInstances sets shutdown behaviour terminate, so the halt alone reclaims the instance without IAM or the AWS CLI. Stage instances carry LifecycleTags plus spawn:launch-time, spawn:ttl and spawn:ttl-deadline, as `spawn launch` writes them. The TTL is the stage timeout plus a 30-minute bootstrap allowance. The orchestrator reads FAILED, which wins over COMPLETE. It also fails a stage whose instances have all gone without COMPLETE (read from EC2) or that has outlived its TTL. A failed stage's instances are terminated and on_failure applies: stop skips the remaining stages and terminates the ones in flight; continue skips the failed stage's dependents, which used to stay pending forever. A pipeline past the sum of its stage TTLs plus 30 minutes is failed and nothing re-invokes it. Every terminal path, including an exceeded budget that used to set CANCELLED without saving or terminating anything, now goes through one function that terminates each stage's instances in that stage's own region instead of us-east-1. The tests run the generated user data under bash with faked commands and no ec2-metadata. Each gate was watched failing against a compiling revert.
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 #912.
Cost safety. This was found by reading the code and was not reproduced on AWS. No instances were launched and nothing was deployed.
What was wrong (each claim checked in the code)
generatePipelineUserData, the failure branch wroteFAILEDand ranexit. Only the success path reachedterminate-instances. A stagetimeouttakes the same branch. So does any bootstrap failure underset -e(spored download, checksum, AWS CLI install, unsupported arch).checkStageCompletionreadCOMPLETEonly. Its fallback readInstanceInfo.State, which is written once at launch (pending) and never refreshed. The stage stayedrunningand the Lambda re-invoked itself every ~13 minutes with no bound.spawn:managedand no TTL tags, soscanRegionnever listed them. One more thing the issue didn't mention: the spored role'sec2:TerminateInstancesis conditioned onec2:ResourceTag/spawn:managed=true(pkg/aws/iam.go). So self-termination was refused by IAM even on Amazon Linux, success path included.ec2-metadataexists only on Amazon Linux. On Ubuntu and Debian the terminate step got an empty id.Bugs found next to these and fixed in this PR, because they are on the same terminal paths:
CANCELLEDandbreak-ed out of theswitchonly. The status was never saved, the loop kept going, and the invocation boundary then returned "terminal state, not reinvoking" without terminating anything.TerminateInstancesto us-east-1 (with a comment admitting it), so it never terminated stages in any other region.on_failure: continue: a stage depending on a failed stage stayedpendingforever, so the pipeline could never finish.What changed (defence in depth)
Instance side (user data):
EXITtrap, installed before anything can fail, runs on every exit. HUP, INT and TERM are routed through it. It writesFAILEDunlessCOMPLETEwas written, terminates through the API with the instance id from IMDSv2 via curl, then runsshutdown -h now.RunInstancessetsInstanceInitiatedShutdownBehavior=terminate, so the halt alone reclaims the instance, with no IAM permission or AWS CLI needed.timeout -k 60, so a command that ignores SIGTERM is killed.Tags:
spawnaws.LifecycleTags(spawn:managed=true,spawn:created,spawn:purpose=pipeline,Name), plusspawn:launch-timeandspawn:ttl/spawn:ttl-deadlinefrompkg/ttl, asspawn launchwrites them. The pipeline and stage tags are kept. TTL = stagetimeout(default 1h) + 30 min to cover bootstrap and the 60s linger, so the reaper never reclaims a stage still inside its own timeout. The bashtimeoutand the tag both come from onestageTimeout().Orchestrator:
checkStageOutcome:FAILEDwins overCOMPLETE. Next comesCOMPLETE. Then instance states refreshed from EC2: all gone withoutCOMPLETEmeans failed (the old fallback counted that as completed). Last, a stage past its TTL fails. An S3 error other than not-found is returned, not read as absent.failStageterminates the stage's instances, in its region, and applieson_failure.stop(the default) skips the remaining stages and terminates the in-flight ones.continueleaves independent stages running, and the failed stage's dependents are skipped.created_at + Σ stage TTLs + 30 minis failed, its instances are terminated, and nothing re-invokes it.finishPipelinehandles every terminal path (complete, failed, cancelled, budget, deadline). It terminates every live instance in its stage's region, saves, then deletes network resources.markers,ec2ForRegion,load/save/reinvoke/notify) for tests.Tests
The new
lambda/pipeline-orchestrator/main_test.gois the first test file for this Lambda. It is in the root module, so it runs ingo test ./....curl/aws/shutdown/timeout/etc. and noec2-metadataon PATH. Three scenarios: failing command, successful command, bootstrap download failure. Each must write the right marker, callterminate-instanceswith the IMDSv2-derived id, and halt.<prefix>:managed=true(viatagprefix), an RFC3339 deadline after the stage timeout and within an hour of it, andlaunch-time.InstanceInitiatedShutdownBehavior=terminatefor on-demand and spot. A source gate checks that everyRunInstancesgoes throughbuildStageRunInstancesInput.continueandstoppolicies; deadline means failed, terminated, no reinvoke; a healthy serial run fits inside the deadline; budget exceeded terminates and finishes.Every gate was watched failing against a compiling revert (16 reverts, each
go vet-clean). Reverted: no trap,ec2-metadatarestored, no halt, no managed tag, TTL = timeout only, shutdown behaviourstop, FAILED ignored, no liveness refresh, no stage TTL, S3 error read as absent, no pipeline deadline, no dependency blocking, stop sparing in-flight stages, failed stage not terminated, the old budgetbreak, cleanup hardcoded to us-east-1. One test was order-dependent at first (topological order of independent stages is unordered). Its fixture was fixed and the package run with-count=50.make check: everything passes except the known Docker-onlyTestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker(no Docker daemon locally). That failure stopsmake checkbefore step 5, somake check-nestedwas run separately and every nested module passes. staticcheck and golangci-lint are clean on the package, andGOOS=linux GOARCH=arm64builds.Deployment needed (not done here)
spawn-pipeline-orchestratorlives in the shared infra account (seedocs/infra-account.md, "Invoked directly by the CLI"). None of this takes effect until it is redeployed:The template is unchanged. The role already grants
ec2:CreateTags,ec2:DescribeInstancesandec2:TerminateInstanceson*, which tag-on-create, the refresh and the terminate need. After deploy, the next invocation of a pipeline still in flight will fail it under the new rules if it is past its deadline, or as soon as its stage has a FAILED marker, and will terminate its instances. Stage instances launched before the deploy still lack the tags. They are reclaimed only through the orchestrator or a manual cancel.Not in this PR
run-pipeline-stage) and pipeline: data_input / data_output are validated but silently ignored; stages get no inputs and their outputs are lost #911 (data handoff) are untouched.instance_count > 1the first instance marks the stage complete; the budget uses a flat $0.10/h).RunInstancesacceptsInstanceInitiatedShutdownBehavior=terminatetogether with one-time spotInstanceMarketOptions. Terminate is the only shutdown behaviour a one-time spot instance supports, so it should be accepted, but nothing here exercised it.