Skip to content

feat(aws): shut down build instances whose launcher has gone - #29

Closed
iAmMichaelConnor wants to merge 1 commit into
aztec-labs-eng:mainfrom
iAmMichaelConnor:mike/launcher-watchdog
Closed

iAmMichaelConnor wants to merge 1 commit into
aztec-labs-eng:mainfrom
iAmMichaelConnor:mike/launcher-watchdog

Conversation

@iAmMichaelConnor

Copy link
Copy Markdown
Contributor

Problem

bootstrap_ec2 runs on a CI runner. It starts a build on an EC2 instance, sends it the build script over SSM, and waits for the result. If the runner side dies (the job is cancelled, the runner crashes, the job times out), nothing tells the instance. It keeps building, with nobody waiting for the result, until its shutdown -h +$AWS_SHUTDOWN_TIME timer fires 60 to 180 minutes later.

Design

A heartbeat already runs from the build container to Redis (hb-<CI_LOG_ID>). This PR adds one in the other direction, from the launcher to the build, using the same Redis. The logic lives in internal/source_launcher_watchdog.

  1. Launcher (bootstrap_ec2). After the instance is provisioned, it sets ci3-launcher-<CI_LOG_ID> with SETEX ... 120 1, then refreshes it every 30 s from a background loop. Its cleanup trap stops the loop, and so does a spot-eviction retry before it execs. If the launcher is SIGKILLed, the trap never runs. The loop is normally killed along with it. If the loop survives, it notices on its next turn that the launcher has gone and stops. The loop's output goes to /dev/null, so it never holds open a pipe that parallel or the CI runner waits on. CI3_LAUNCHER_WATCHDOG=1 is passed into the container only if the first write returned OK.

  2. Build container. Next to the existing heartbeat, watch_launcher runs redis_cli EXISTS ci3-launcher-<CI_LOG_ID> every 60 s:

    • reply 1: the watcher is armed, and the miss count goes back to zero;
    • reply 0 once armed: one miss;
    • anything else (a connection error, which redis_cli hides behind empty output, a timeout, an error reply, any other text): nothing changes.

    After 3 misses in a row it prints a LAUNCHER WATCHDOG: ... line saying that the launcher has gone and the instance is shutting down. It then creates /tmp/ci3-launcher-gone (the host's /tmp is mounted at /tmp in the container) and stops. The line goes to the container's output, and is also appended to the run's log file, which cache_log publishes to the dashboard every few seconds. That copy is best effort: the instance may go down before the next publish.

  3. Host. The host script's monitoring loop, which already checks for spot-termination notices every 5 s, also checks for the flag file. When it finds one, it prints a HOST: line and runs sudo shutdown -h now. If the launch template sets InstanceInitiatedShutdownBehavior: terminate (fix(aws): bound the lifetime of build instances whose launcher is killed #28 does), the instance is terminated. Otherwise it is stopped. The host script deletes any old flag file when it starts, so a file left by an earlier build cannot stop a new one.

Timing: the key expires at most 120 s after the launcher's last refresh, and 3 misses take up to another 180 s. So the instance starts shutting down about 5 minutes after its launcher dies.

Safety properties

  • The container does nothing until it has seen the key at least once. Seeing the key proves that both sides use the same Redis. If they used different ones, the key would look missing forever, and the watcher stays unarmed.
  • A Redis error never counts as a miss. Only an exact 0 reply from EXISTS does. An unreachable or slow Redis leaves the build alone.
  • A launcher that cannot write the key does not turn the watchdog on.

When it is off

  • Outside GitHub Actions, unless CI3_LAUNCHER_WATCHDOG=1. Someone launching from a laptop may close the lid and still expect the build to finish. The setting keeps the mechanism vendor-neutral: another CI system can turn it on with CI3_LAUNCHER_WATCHDOG=1, and a GitHub Actions job can turn it off with CI3_LAUNCHER_WATCHDOG=0.
  • With NO_TERMINATE=1, whose instances are meant to outlive their launcher.
  • For releases (REF_NAME is a semver).
  • When Redis is unavailable to the launcher, or its first SETEX does not reply OK.

Tests

tests/launcher_watchdog_test needs no Redis server and makes no AWS calls; it unsets AWS credentials. It drives watch_launcher with a stub check command, checks 0.01 s apart, and checks:

  • a key that was never seen never trips, however many misses follow;
  • seen once and then 3 misses trips on the third miss, and prints the line;
  • seen and then only 2 misses does not trip;
  • connection errors, error replies and unexpected output never trip, and errors before the key is seen do not arm the watcher;
  • errors between misses neither count as a miss nor reset the count;
  • seen, 2 misses, seen again starts the count over, and 3 new misses then trip.

It also checks when the launcher turns the watchdog on (GitHub Actions, NO_TERMINATE, releases, CI3_LAUNCHER_WATCHDOG=0/1). It checks that the heartbeat starts only if Redis is available and the first write returns OK, and that stop_launcher_heartbeat stops the loop. Finally, it checks the wiring in bootstrap_ec2 by reading the source.

All 21 checks pass, in about 2 s. I also broke the watcher on purpose (acting before the key is seen; counting errors as misses), and the relevant checks failed. bash -n passes on every changed file.

Not tested here: a real launch.

Checks a maintainer should do on a real run

  • Cancel a CI run mid-build. Confirm the instance shuts down within about 5 minutes, and that the dashboard log or the SSM output shows the LAUNCHER WATCHDOG and HOST: lines. Without fix(aws): bound the lifetime of build instances whose launcher is killed #28 the instance ends stopped, not terminated.
  • Let a normal build run longer than 10 minutes. Confirm it is unaffected: the ci3-launcher-<CI_LOG_ID> key is refreshed throughout, and no /tmp/ci3-launcher-gone appears on the instance.
  • Run a release or NO_TERMINATE=1 build, and confirm the container gets CI3_LAUNCHER_WATCHDOG=0.

Related

When the process running bootstrap_ec2 dies (the job is cancelled, the
runner crashes, the job times out), nothing tells the build instance. It
builds on with nobody waiting for the result until its shutdown timer.

The launcher now keeps a Redis key, ci3-launcher-<CI_LOG_ID>, alive while
it runs (120 s expiry, refreshed every 30 s). The build container checks
the key every 60 s. Once the key has been seen, 3 misses in a row make it
log that the launcher has gone and leave a flag file in /tmp, and the host
shuts the instance down. The container never acts before it has seen the
key, and never on a Redis error or unexpected reply.

On by default under GitHub Actions only; CI3_LAUNCHER_WATCHDOG=0/1 turns
it off or on. Always off with NO_TERMINATE=1, for releases, and when the
launcher cannot write the key.
@iAmMichaelConnor

Copy link
Copy Markdown
Contributor Author

Closing. As written this never turns on: GitHub-hosted runners have no Redis access, so the launcher can't write its liveness key. Cancels and job timeouts are handled by the normal cleanup path (see AztecProtocol/aztec-packages#25894), and unclaimed instances are bounded by the boot timer in #28. The remaining case, a runner lost after the instance is claimed, is uncommon and still bounded by the instance's shutdown timer.

@iAmMichaelConnor
iAmMichaelConnor deleted the mike/launcher-watchdog branch November 24, 2026 21:43
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.

1 participant