Skip to content

pipeline: a failed stage's instance is never terminated, and the TTL reaper cannot see it (no spawn:managed tag) #912

Closed

Description

@scttfrdmn

Found while fixing #906. Cost safety. Found by reading the code; not reproduced against AWS (no instances launched).

When a pipeline stage's command fails, its instance is never terminated and nothing else will ever reclaim it:

  1. The instance doesn't terminate itself on failure. In the user data from lambda/pipeline-orchestrator/main.go generatePipelineUserData, the failure branch (timeout %s bash -c "$STAGE_CMD" || { ...; exit $EXIT_CODE }) writes the FAILED marker and exits. Only the success path reaches aws ec2 terminate-instances. The stage timeout firing takes the same branch.
  2. The orchestrator never notices the failure. checkStageCompletion looks for the COMPLETE marker only; nothing reads FAILED. Its fallback, "all instances terminated", reads InstanceInfo.State, which is set once at launch ("pending") and never refreshed (allInstancesRunning is a 1-minute heuristic with a TODO). The stage stays running, allStagesComplete never holds, and the Lambda re-invokes itself every ~13 minutes indefinitely. StageStatusFailed is set only when the launch call itself fails.
  3. The TTL reaper can't see it. launchStageInstance tags only spawn:pipeline-id, stage-id, stage-index, instance-index and Name: no spawn:managed, no spawn:ttl-deadline, no launch time. The reaper lists only spawn:managed=true instances (lambda/ttl-reaper/main.go scanRegion), and its terminate grants are conditioned on that tag (see the comment on the pipeline security group, which got LifecycleTags for exactly this reason). The instances also get no TTL and no InstanceInitiatedShutdownBehavior.

Also on the success path, self-termination uses ec2-metadata --instance-id, which exists on Amazon Linux but not on Ubuntu/Debian AMIs. On those, the terminate step gets an empty id and fails, so even a successful stage instance runs on, and is only cleaned up if the user cancels the pipeline.

Result: a stage whose command exits non-zero leaves an instance running, and billing, until someone cancels the pipeline by hand. The reaper is no backstop.

Suggested:

  • Tag stage instances with spawn.LifecycleTags plus a TTL deadline taken from the stage timeout, so the reaper covers them.
  • Terminate on the failure path too, or shutdown -h with InstanceInitiatedShutdownBehavior=terminate.
  • Have checkStageCompletion read FAILED and mark the stage failed.
  • Get the instance id from IMDS instead of ec2-metadata.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions