Skip to content

fix(ci): terminate build instances when a CI run is cancelled - #25894

Closed
iAmMichaelConnor wants to merge 1 commit into
nextfrom
mike/ci-cancel-cleanup
Closed

iAmMichaelConnor wants to merge 1 commit into
nextfrom
mike/ci-cancel-cleanup

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, 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 Engineer should see the sequencer state move to 'rollup published' once the rollup txs have been sent #28. aztec-packages pins ci3 to a commit, so Engineer should see the sequencer state move to 'rollup published' once the rollup txs have been sent #28 has no effect there until this lands.

Problem

When a CI3 run is cancelled, the EC2 build instance it started keeps running. Nothing on our side terminates it, and if the run was cancelled in its first minute, the instance has no shutdown timer either.

The cancel never reaches ci3:

  • On cancellation the GitHub runner sends SIGINT, and SIGTERM 7.5 s later, only to the Run step's own shell (kill(pid) in actions/runner's ProcessInvoker.cs, not the process group).
  • That shell runs ./.github/ci3.sh as a child, so ci3.sh, ci.sh, parallel and bootstrap_ec2 receive nothing.
  • A few seconds later the runner SIGKILLs every process that is left. bootstrap_ec2's cleanup trap, which terminates the instance, never runs.

This bites whenever a pull request is merged or closed straight after it is opened:

  1. The CI3 run starts after the merge, often 5–10 s in.
  2. The run requests an instance.
  3. cancel-stale-workflows.yml cancels the run, which leaves the instance running until something else removes it.

A push to the same branch would normally terminate it by name, but a closed PR gets no more pushes.

Changes

ci3.yml and ci3-external.yml: run: exec ./.github/ci3.sh $CI_MODE.

  • The step's shell is replaced by ci3.sh, which already execs ci.sh, so the runner's signals now land on ci.sh.
  • This is the chain the comment at the top of ci3.yml intends ("We use exec a lot to ensure signal propagation"); the Run steps were the missing link.
  • ci3-external.yml's Run step also now gets PR_NUMBER, for the check below. bootstrap_ec2 also passes it into the build container and instance telemetry, as it already does for internal runs; nothing there changes behaviour on it.

.github/ci3.sh and ci.sh: exit on a cancel that arrives during start-up.

  • A non-interactive bash ignores a SIGINT that arrives while it waits for a command, unless that command also died of it. Before multi_job_run sets its trap, a cancel during bootstrap.sh init, the PR lookup or check_cache would be dropped, and the run would go on to request an instance.
  • Both scripts now set exit traps at the top (130 for INT, 143 for TERM), so they exit once the current command finishes. ci.sh sets them only under GitHub Actions, so a local Ctrl-C at start-up behaves as before. multi_job_run restores them after its own trap.

ci.sh: multi_job_run passes the signal on to its jobs.

  • bash runs a trap only after its foreground command finishes, so a signal to ci.sh would wait for the whole parallel | add_timestamps | cache_log pipeline.
  • The pipeline now runs in the background, and ci.sh waits for it; wait returns as soon as a trapped signal arrives.
  • On INT or TERM, the trap sends TERM to parallel, whose pid comes from jobs -p %%. parallel's existing --termseq 'TERM,10000' then TERMs each job's process group, and bootstrap_ec2's trap terminates the instance.
  • The trap is installed before parallel starts. parallel ignores TERM while it starts up, so once a signal has arrived, ci.sh re-sends TERM every second until the pipeline exits.
  • The function's exit status is unchanged: still the pipeline's, under the pipefail that ci3/source sets.

.github/ci3.sh: skip runs for pull requests that are already closed.

  • On pull_request and pull_request_target events, ci3.sh looks up the PR state, with a 20 s timeout.
  • If the PR is closed, it exits 0 before check_cache, so no CI success is recorded for the tree. This applies only to a run's first attempt, so a manual re-run still runs CI. It also writes CI3_PR_CLOSED=1 to GITHUB_ENV, and .github/ci3_success.sh (Post-Actions) then does nothing, so a ci-squash-and-merge label is not acted on.
  • Any doubt runs CI as before: another event, no PR number, or the API call failing.
  • Drafts are not skipped, so drafts with ci-draft still get CI; cancel-stale-workflows.yml already cancels runs when a PR is converted to draft.

Limits

  • Behaviour change for release runs. Cancelling a tag or release run (multi_job_run 'x-release …') now terminates its instance mid-publish. Before, the instance finished its part of the publish, but publish-npm (needs: ci) was still skipped, so a cancelled release was already only partly published.

  • Local runs. A local Ctrl-C during multi_job_run's jobs now waits for their clean-up, instance termination included, before ci.sh exits.

  • One window stays open until the ci3 bump. With the ci3 pinned on next, a cancel that lands while create-fleet is in flight still leaks. The TERM kills the aws call before the instance id is recorded, so clean-up has nothing to terminate. ci3#28 closes that window, by tagging each launch and adding the boot timer. Until the pin moves past it, the closed-PR check is what prevents those leaks.

  • Not covered: the nightly and socket-fix workflows, where bootstrap_ec2 runs directly under ci.sh rather than through multi_job_run, and the chonk-input-update run inside Post-Actions. A cancel there behaves as today: before the instance is claimed, ci3#28's boot timer bounds it; after, it runs to its AWS_SHUTDOWN_TIME.

Testing

No CI was run for this; it is a draft. Local checks:

  • ci.sh: I imitated the runner's cancellation.
    • Setup: a step shell exec'd ci3 and then ci.sh, with multi_job_run copied verbatim and the real parallel from ci3. The step was started with default signal handling, and a stand-in job's TERM trap writes a file.
    • On next, SIGINT to the step's shell alone never reaches the job.
    • With this change, the job's trap ran and the step exited within seconds. That held for SIGINT at 0.15–0.5 s, inside parallel's start-up, and at 3 s; at one delay the job was stopped before it started.
    • With stand-ins for the slow start-up commands in both scripts, SIGINT during either script's start-up made the step exit within about a second, and no job started.
    • Exit status matched on success (0) and failure (the job's code); on failure, the script stops as before.
  • .github/ci3.sh: I ran the gh api … --jq .state lookup against real merged and open PRs.

Before merging, a reviewer should:

  • open a test PR, let CI3 reach x-fast (past the instance request), cancel the run, and check in EC2 that its instance (aztec-packages_<pr>_amd64_x-fast) is terminated within a few seconds;
  • open and immediately close a test PR, and check that the run logs "is closed; not running CI", starts no instance, and Post-Actions does nothing;
  • cancel a run within its first ~5 s, and check that it exits and starts no instance.

Related

Related CI fixes:

A cancelled CI3 run leaves its EC2 build instance running. The runner
signals only the Run step's shell, which runs .github/ci3.sh as a child,
then SIGKILLs everything left a few seconds later, so bootstrap_ec2's
cleanup never runs. Merging a pull request right after opening it hits
this every time: the run starts after the merge, requests an instance,
and is cancelled by cancel-stale-workflows.yml.

- ci3.yml, ci3-external.yml: `exec ./.github/ci3.sh`, so the signal
  reaches ci3.sh and, through its exec, ci.sh.
- .github/ci3.sh, ci.sh (in CI): exit on INT/TERM from the start, since
  bash otherwise drops a SIGINT while it waits on a command.
- ci.sh: multi_job_run runs the parallel pipeline in the background and
  passes INT/TERM on to parallel, which TERMs its jobs; bootstrap_ec2's
  trap then terminates their instances. A foreground pipeline would
  defer the trap until it ended.
- .github/ci3.sh: a first-attempt run for a pull request that is already
  closed exits before requesting anything, and Post-Actions then does
  nothing.
charlielye added a commit that referenced this pull request Nov 25, 2026
…king, skip closed PRs (#25900)

One CI update combining #25900, #25901, #25902 and #25903, with ci3
pinned to `main` (aztec-labs-eng/ci3@bfd124d). Each part is unchanged
from its PR; the only conflict was the ci3 pin.

## 1. Cancelling a run terminates its instances (was #25900)

Cancelled CI runs left their build instances running until the shutdown
timer.

GitHub cancels a step by signalling only the process it started: SIGINT,
SIGTERM 7.5s later, then SIGKILL. The workflow steps ran
`./.github/ci3.sh` without `exec`, and `ci.sh` ran `bootstrap_ec2`
inside `multi_job_run` and pipelines, so the signal stopped at the
step's shell and never reached the script that terminates instances.

**Changes**
- Workflow steps now `exec ./.github/ci3.sh …`. `ci3.sh` already execs
`./ci.sh`.
- Every `ci.sh` mode that launches instances now `exec`s into
`bootstrap_ec2`, or into ci3's new `bootstrap_ec2_multi`.
`bootstrap_ec2_multi` replaces `multi_job_run` with the same behaviour,
including `INSTANCE_NAME_SUFFIX` and bench-upload only on the first job.
- `merge-queue` keeps its dashboard section choice, which moves from
`multi_job_run` into the mode itself.
- ci3 is bumped to aztec-labs-eng/ci3#30:
- `bootstrap_ec2` terminates its instance on SIGINT/SIGTERM, including
while the instance is still being requested.
- Instances launched from a build instance (the bench box) are tagged
`ParentInstance` and terminated along with it.

**Testing**
- The ci3 PR adds `tests/cancel_test`, which simulates the runner's
signal sequence and Ctrl-C.
- I also ran that harness, with a stub aws, against this branch's real
chain: `bash -c 'exec ./.github/ci3.sh fast'` and `merge-queue` (two
instances). Each run exits on the first SIGINT, cancels the SSM
commands, and terminates every instance plus the bench child.

## 2. Nightly npm releases pack again (was #25901)

Every `next` nightly since the move to the shared ci3 (#25516) has
failed at its first npm package with `npm error code ENEEDAUTH`.

The release build is meant to only **pack** the npm packages
(`bootstrap.sh release` sets `NPM_PACK_DIR`), and the `publish-npm` job
publishes the tarballs from a GitHub runner with npm trusted publishing.
The shared ci3's `deploy_npm` had no pack mode, so the build machine
attempted a real publish without credentials. aztec-labs-eng/ci3#31
restores it, together with the `repository` stamp npm provenance
requires.

Checked offline, with npm unable to reach any registry or credential:
`protocol/genesis/bootstrap.sh release` packs
`@aztec-foundation/protocol-genesis@7.0.0-nightly.20261126` with
`repository` set to this repository and `protocol/genesis`, and
`DRY_RUN=1 scripts/publish_npm_release` accepts the tarball.

## 3. Stop running ci3's own tests here (was #25902)

Removes the `ci3-tests` target. ci3 now runs its tests in its own CI on
every pull request and push to `main` (aztec-labs-eng/ci3#33). Running
them here meant a ci3 change was only tested once a pin bump picked it
up, and they ran on a build machine whose build was using the same ci3
scripts at the same time, which broke `cancel_test`'s leftover-process
check.

## 4. Skip CI for a pull request that is already closed (was #25903)

A pull request merged straight after it was opened gets a CI run that
only starts after the merge:
- **`ci3.yml`:** the run requests a build instance, then
`cancel-stale-workflows.yml` cancels it.
- **`ci3-external.yml`:** nothing cancels the run, so CI runs in full
for a pull request that no longer needs it.

**Change**
- `.github/ci3.sh` now checks the pull request's state before doing
anything else, and skips CI when it is closed.
- It does this only on a run's first attempt, so a manual re-run still
runs CI.
- If anything is uncertain, CI runs as normal. That covers another event
type, no PR number, or an API error or 20s timeout.
- When it skips, `.github/ci3_success.sh` (Post-Actions) does nothing:
it records no CI success for the tree and doesn't act on labels such as
`ci-squash-and-merge`.
- `ci3-external.yml`'s Run step now gets `PR_NUMBER`. `ci3.yml`'s
already did.

This is the `pr_closed` part of #25894, by iAmMichaelConnor, taken on
its own. Part 1 replaces #25894's cancellation changes, so a cancelled
run terminates its instances. This part means a closed PR's run never
requests one.

**Testing**
I tested `pr_closed` with a stub `gh`:

| Case | Result |
|---|---|
| Closed PR (`pull_request`) | Skips |
| Closed fork PR (`pull_request_target`) | Skips |
| Open PR | Runs |
| API failure | Runs |
| Push event | Runs |
| No PR number | Runs |

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01DSxF9AgGYcAcCTjXEeJT4L

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@charlielye

Copy link
Copy Markdown
Contributor

Closing as superseded by work that has now merged:

  • #25900: CI execs from the workflow step into ci3's bootstrap_ec2/bootstrap_ec2_multi, which terminate their instances when a run is cancelled. This pins ci3 with aztec-labs-eng/ci3#30 and follow-ups.
  • #25903: skipping CI for a pull request that is already closed, taken from this PR.

Thanks for this. It conflicts with next 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