Repository navigation
fix(aws): bound the lifetime of build instances whose launcher is killed - #28
Closed
iAmMichaelConnor wants to merge 1 commit into
Closed
iAmMichaelConnor wants to merge 1 commit into
iAmMichaelConnor wants to merge 1 commit into
Conversation
iAmMichaelConnor
force-pushed
the
mike/ec2-orphan-timer
branch
2 times, most recently
from
November 24, 2026 18:33
1b3cebc to
a346f7b
Compare
Closed
3 tasks
iAmMichaelConnor
force-pushed
the
mike/ec2-orphan-timer
branch
4 times, most recently
from
November 24, 2026 19:26
7294da8 to
f2b0ddb
Compare
This was referenced Nov 24, 2026
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.
iAmMichaelConnor
force-pushed
the
mike/ec2-orphan-timer
branch
from
November 24, 2026 22:09
f2b0ddb to
c2ae93e
Compare
Contributor
|
Closing as superseded by #34, now merged. Each build instance has a lease, its 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. |
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.
Two PRs
This is one of two PRs for the same problem: build instances left running after a cancelled CI run.
ci.shcall ci3The fix needs changes in both repositories, so it is two PRs. Neither depends on the other.
Merge order
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):kill(pid), not the process group. That process is thebash -e {0}wrapper aroundrun: ./.github/ci3.sh $CI_MODE. ci3.sh runs as its child and is not exec'd, so ci3.sh, ci.sh,parallel,bootstrap_ec2andaws_request_instance_typereceive nothing.Process.Kill()(SIGKILL) on every remaining process, about 13 s after the cancel. The SIGINT/SIGTERM/EXIT traps inbootstrap_ec2andaws_request_instance_typenever fire.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.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
awsprocess was in the middle of the fleet request: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.
AWS_BOOT_SHUTDOWN_TIMEminutes. 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.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 itsAWS_SHUTDOWN_TIME, or that someone powers off by hand (for example ashell-neworUNSAFE_AWS_KEEP_ALIVEbox), used to stop and keep its volume; now it is terminated.AWS_SHUTDOWN_TERMINATES=0keeps the timer but leaves shutdown as stop.aws/ami_update.shsets it as a precaution:create-imagereboots the instance from the EC2 side, which probably doesn't count as a shutdown started from the OS, and the checklist's fullcreate-imagerun will settle it.Whoever uses the instance claims it.
aws_request_instance_typeclaims the instance with the caller'sAWS_SHUTDOWN_TIMEonce SSM is online.aws/ami_update.shor 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.
shutdown -h +Nreplaces an earlier one. A slow boot could otherwise put the 15-minute timer back under a running build.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.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.
CI3LaunchId, which also names the launch template and is tagged on the instance. It is written to the state dir just beforecreate-fleet.aws_terminate_instancehas a launch ID but no instance ID (it was interrupted insidecreate-fleet), it finds pending or running instances by that tag and terminates them. It checks three times, 2 s apart.parallel --termseq. The ID is unique to the request, so the lookup can't match another launch's instance.Tests
tests/aws_instance_lifetime_testdrives the real scripts against a stubaws, with credentials, shared config and instance metadata all disabled, so it makes no AWS calls. It checks:shutdown, and confirms the claim's timer holds either way;AWS_SHUTDOWN_TERMINATES=0keeps the boot timer but not terminate-on-shutdown;bootstrap_ec2's build script sets the timer, or stops the script if it can't. The line itself is run; thatbootstrap_ec2uses it is checked by searching the source;All 16 checks pass. Against
mainthe first checks fail and the script stops.Not tested here: a real launch. Before merging, reviewers should:
cat /run/systemd/shutdown/scheduledshows theAWS_SHUTDOWN_TIMEdeadline; compare the user-data time in/var/log/cloud-init-output.logwith the first SSM command;NO_SPOT=1launch, to confirmcreate-fleet --type instantacceptsInstanceInitiatedShutdownBehaviorfrom the launch template, and that a timed-out on-demand instance ends terminated with its volume deleted;create-fleetand confirm the instance terminates at about 15 minutes;aws/ami_update.shin full (NO_AMI=0, socreate-imageruns) 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_TIMEtimer (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.shimages 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.