Skip to content

fix(aws): bound the lifetime of build instances whose launcher is killed - #28

Closed
iAmMichaelConnor wants to merge 1 commit into
aztec-labs-eng:mainfrom
iAmMichaelConnor:mike/ec2-orphan-timer
Closed

iAmMichaelConnor wants to merge 1 commit into
aztec-labs-eng:mainfrom
iAmMichaelConnor:mike/ec2-orphan-timer

Conversation

@iAmMichaelConnor

@iAmMichaelConnor iAmMichaelConnor commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Two PRs

This is one of two PRs for the same problem: build instances left running after a cancelled CI run.

PR Repository What it changes
aztec-labs-eng/ci3#28 ci3, which launches and terminates the EC2 instances Each instance shuts itself down about 15 minutes after boot unless a build claims it. This is the backstop, for cases where nothing on the runner side gets to run.
AztecProtocol/aztec-packages#25894 aztec-packages, whose CI workflow and ci.sh call ci3 A cancelled run now reaches ci3's clean-up, so its instance is terminated at once, and a run for a pull request that is already closed requests nothing.

The fix needs changes in both repositories, so it is two PRs. Neither depends on the other.

Merge order

  1. aztec-packages#25894. It stands alone and prevents most leaks, because a closed pull request's run never requests an instance.
  2. ci3#28, after the real-launch checks in its description.
  3. A one-line aztec-packages PR moving the ci3 submodule pin past fix(aws): bound the lifetime of build instances whose launcher is killed #28. aztec-packages pins ci3 to a commit, so fix(aws): bound the lifetime of build instances whose launcher is killed #28 has no effect there until this lands.

Problem

When a GitHub Actions CI job is cancelled, ci3's cleanup never runs, and the EC2 instance it launched keeps running. Cancelling soon after launch is the worst case, because the instance has no shutdown timer yet.

What happens when a job is cancelled (actions/runner ProcessInvoker.cs, JobExtension.cs):

  1. The runner sends SIGINT to the step's process only: kill(pid), not the process group. That process is the bash -e {0} wrapper around run: ./.github/ci3.sh $CI_MODE. ci3.sh runs as its child and is not exec'd, so ci3.sh, ci.sh, parallel, bootstrap_ec2 and aws_request_instance_type receive nothing.
  2. 7.5 s later the runner sends SIGTERM, again to the wrapper only. The wrapper dies, but its orphans still hold the step's output pipe, so the runner waits another 5 s.
  3. Job teardown then calls Process.Kill() (SIGKILL) on every remaining process, about 13 s after the cancel. The SIGINT/SIGTERM/EXIT traps in bootstrap_ec2 and aws_request_instance_type never fire.
  4. Until now the instance's only time limit was shutdown -h +$AWS_SHUTDOWN_TIME, sent fire-and-forget over SSM after the instance registers, about a minute after launch. A job cancelled before then leaves an instance with no timer.
  5. When a ref is pushed again, the next run terminates the old instance by name. Nothing does that when the run was cancelled because its PR was merged or closed.

An example log (a PR merged seconds after it was opened, which cancelled its CI run) shows the pipeline still alive when the job ends. The aws process was in the middle of the fleet request:

10:48:40 x-fast
10:48:53 ##[error]The operation was canceled.
10:48:54 Cleaning up orphan processes
10:48:54 Terminate orphan process: pid (2274) (bash)
10:48:54 Terminate orphan process: pid (2420) (parallel)
   ... 11 more bash / tail / perl ...
10:48:55 Terminate orphan process: pid (2946) (aws)

Each such instance is a 64–192 vCPU box that runs idle until the reaper finds it. Opening and merging PRs in quick succession leaves one for each PR.

Fix

The instance arms its own shutdown timer at boot. This is the part that still works under SIGKILL.

  • The launch template gets user data, which cloud-init runs at first boot, setting a timer of AWS_BOOT_SHUTDOWN_TIME minutes. The default is 15. Values under 10 are refused, because the timer must outlast provisioning: the 300 s SSM wait plus up to three claim attempts, about 8 minutes.
  • It also gets InstanceInitiatedShutdownBehavior: terminate, so a timed-out instance is terminated rather than stopped and stops paying for its volume. This changes behaviour: an on-demand instance that reaches its AWS_SHUTDOWN_TIME, or that someone powers off by hand (for example a shell-new or UNSAFE_AWS_KEEP_ALIVE box), used to stop and keep its volume; now it is terminated.
  • AWS_SHUTDOWN_TERMINATES=0 keeps the timer but leaves shutdown as stop. aws/ami_update.sh sets it as a precaution: create-image reboots the instance from the EC2 side, which probably doesn't count as a shutdown started from the OS, and the checklist's full create-image run will settle it.
  • An instance that nobody claims lives at most about 15 minutes, whatever happens to the process that launched it.

Whoever uses the instance claims it.

  • aws_request_instance_type claims the instance with the caller's AWS_SHUTDOWN_TIME once SSM is online.
    • The claim now waits for the result and is fatal. It tries three times (up to 60 s each); if all three fail, the instance is terminated and provisioning exits 1.
    • Before, a lost claim left the instance with no timer. Now it would leave the 15-minute boot timer under a long job, such as aws/ami_update.sh or a consumer's image build.
  • bootstrap_ec2's build script claims the instance again as its first step, and fails if it can't.

A claim always beats the boot timer.

  • cloud-init's final stage, which runs user data, is not ordered against the first SSM command, and on the instance a later shutdown -h +N replaces an earlier one. A slow boot could otherwise put the 15-minute timer back under a running build.
  • So the boot script and the claim take the same lock (flock /run/ci3-timer.lock), and a successful claim leaves a marker (/run/ci3-claimed) that the boot script respects. The marker is written only after the timer is set, so a failed claim leaves the boot timer in charge.
  • Whichever runs first, the claim's timer stands. Both sides live in internal/source_shutdown_timer.

NO_TERMINATE is unchanged. NO_TERMINATE instances get no user data, no claim, and keep the default stop-on-shutdown.

Clean-up can find an instance whose ID was never recorded.

  • Each launch has a unique CI3LaunchId, which also names the launch template and is tagged on the instance. It is written to the state dir just before create-fleet.
  • When aws_terminate_instance has a launch ID but no instance ID (it was interrupted inside create-fleet), it finds pending or running instances by that tag and terminates them. It checks three times, 2 s apart.
  • This helps when the clean-up handler does run, for example after a local Ctrl-C or a parallel --termseq. The ID is unique to the request, so the lookup can't match another launch's instance.

Tests

tests/aws_instance_lifetime_test drives the real scripts against a stub aws, with credentials, shared config and instance metadata all disabled, so it makes no AWS calls. It checks:

  • the guarded boot timer and terminate-on-shutdown;
  • the launch tag;
  • the exact claim sent at provisioning (decoded from the SSM payload);
  • no timer and stop-on-shutdown under NO_TERMINATE;
  • that boot timers under 10 minutes are refused before anything is launched;
  • the 128-character launch template name limit, with a 200-character instance name;
  • ordering: it runs the real boot script and claim, both ways round, against a stub shutdown, and confirms the claim's timer holds either way;
  • that a failed claim leaves no marker, so the boot timer still applies;
  • that a claim failing three times terminates the instance and fails provisioning;
  • that AWS_SHUTDOWN_TERMINATES=0 keeps the boot timer but not terminate-on-shutdown;
  • that the claim line used in bootstrap_ec2's build script sets the timer, or stops the script if it can't. The line itself is run; that bootstrap_ec2 uses it is checked by searching the source;
  • clean-up by tag (one or several instances), by recorded ID, and when no instance appears.

All 16 checks pass. Against main the first checks fail and the script stops.

Not tested here: a real launch. Before merging, reviewers should:

  • launch a few instances, including a cold one, and one minute into the build confirm cat /run/systemd/shutdown/scheduled shows the AWS_SHUTDOWN_TIME deadline; compare the user-data time in /var/log/cloud-init-output.log with the first SSM command;
  • do one spot and one NO_SPOT=1 launch, to confirm create-fleet --type instant accepts InstanceInitiatedShutdownBehavior from the launch template, and that a timed-out on-demand instance ends terminated with its volume deleted;
  • kill a launcher with SIGKILL during create-fleet and confirm the instance terminates at about 15 minutes;
  • run aws/ami_update.sh in full (NO_AMI=0, so create-image runs) and check that the AMI is created and the instance survives imaging, then run a consumer's image build past 15 minutes.

Not covered

A cancel after provisioning still leaves the instance running until its AWS_SHUTDOWN_TIME timer (60–180 minutes, depending on the job) unless the trap runs. In aztec-packages, #25894 makes the trap run.

Also not covered, as before: after aws/ami_update.sh images its instance, the reboot clears the shutdown timer. If that script is killed while it waits for the image, the instance stays up until someone removes it.

A cancelled GitHub Actions job never runs bootstrap_ec2's cleanup trap. The
runner signals only the step's top-level shell, which runs ci3 as a child,
and a few seconds later it SIGKILLs the orphaned processes. An instance
launched by such a job keeps running. It also has no shutdown timer: that
was set over SSM only after the instance registered, about a minute after
launch, and fire-and-forget. It runs until the reaper finds it.

- The instance arms its own shutdown timer at boot, from user data
  (AWS_BOOT_SHUTDOWN_TIME, default 15 minutes, at least 10), and a shutdown
  it starts terminates it.
- Provisioning claims the instance by setting the caller's timer. It now
  waits for the result, tries three times, and is fatal: a failed claim
  terminates the instance. bootstrap_ec2's build script claims it again as
  its first step.
- Boot is not ordered against the first SSM command, and a later
  `shutdown -h +N` replaces an earlier one. So the boot script and the
  claim take one lock, and a successful claim leaves a marker the boot
  script respects; a claim always wins, whichever runs first
  (internal/source_shutdown_timer).
- NO_TERMINATE instances get no timer and keep stop-on-shutdown.
  AWS_SHUTDOWN_TERMINATES=0 keeps the timer but not terminate-on-shutdown;
  aws/ami_update.sh sets it, since create-image powers the instance down.
- Each launch is tagged with a unique CI3LaunchId, written just before
  create-fleet. aws_terminate_instance uses it to terminate an instance
  whose id never came back from create-fleet, for interruptions where the
  trap does run.
@charlielye

Copy link
Copy Markdown
Contributor

Closing as superseded by #34, now merged. Each build instance has a lease, its LeaseUntil tag, that its launcher renews every minute, and it shuts itself down when the lease runs out. So an instance outlives a killed launcher by at most 5 minutes, and the shutdown timer is gone. Cancelled runs terminate their instances straight away (#30, #36).

The launch-id lookup here, for a launcher killed before it records its instance id, is a possible follow-up. The lease already bounds such an instance to 10 minutes from boot.

Thanks for this. It conflicts with main now that those have landed.

@charlielye charlielye closed this Nov 25, 2026
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.

2 participants