We wrote the code that re-runs a CI job when AWS takes its spot instance away, and set up a test to interrupt one on purpose. AWS interrupted the test job for real before we could, and the re-run did not happen.
This is the fourth of five posts about running each GitHub Actions job on its own EC2 spot instance. The first post covers the design. Spot instances are why a job costs a fraction of a cent, and the price is that AWS can reclaim one with two minutes' warning. This post is about making that invisible to the person waiting for a green check. It took three attempts.
Attempt one: mark it, then re-run it
The plan was simple, and half of it worked.
AWS publishes the warning as an event. We route it to the worker, which finds the job running on that instance and marks it as interrupted. Two minutes later the instance is gone and the job fails. GitHub sends its completed event, the worker sees a failed job with the mark, and asks GitHub to re-run it. One re-run only: the re-run is a second attempt, and an interrupted second attempt stays failed.
We could not ask GitHub for the re-run at the moment of the warning, because GitHub refuses to re-run a job that is still running. So everything hung on the job failing promptly.
What actually happened
To test it we pushed a job that sleeps for six minutes, meaning to send it a fake interruption with AWS Fault Injection Service. Two minutes after the instance launched, AWS reclaimed it for real.
The first half worked: the warning arrived and the job was marked. Then:
- 13:13 UTC: the warning.
- 13:15: the instance is terminated.
- 13:26: GitHub ends the job as
cancelled, with "The job has exceeded the maximum execution time of 10m0s".
For eleven minutes after the machine had ceased to exist, GitHub showed the job as in progress. It ended only because our test job had a ten minute timeout. Our real jobs use GitHub's default, which is six hours.
Two things were wrong. The runner process died with the instance and never told GitHub anything, and GitHub does not notice a silent runner on any timescale that matters. And when the job did end, it ended as cancelled, not failure, so our re-run condition did not match.
A spot interruption on a real pull request would have left a job spinning until someone cancelled it by hand.
Attempt two: tell the runner
The two minute warning is the only window in which anything on the instance can act, and nothing on our image used it. So the image got a small service that polls the instance metadata for the interruption notice and stops the runner:
# IMDS holds the URL of the EC2 instance metadata service, set earlier in the script. NOTICED= while true; do if [ -z "$NOTICED" ]; then TOKEN=$(curl -fsS -m 2 -X PUT -H 'X-aws-ec2-metadata-token-ttl-seconds: 60' "$IMDS/api/token" 2>/dev/null) if [ -n "$TOKEN" ] && curl -fsS -m 2 -o /dev/null -H "X-aws-ec2-metadata-token: $TOKEN" "$IMDS/meta-data/spot/instance-action" 2>/dev/null; then NOTICED=1 echo "spot interruption notice: stopping the runner in 30 s" sleep 30 fi fi # No listener yet (the notice came during boot): keep trying until one exists to stop. if [ -n "$NOTICED" ] && pkill -INT -f Runner.Listener; then echo "runner stopped" exit 0 fi sleep 5 done An interrupt makes the runner fail its job at once and report it. GitHub records a failure while the instance is still alive to send it.
The sleep 30 came from review, and it matters. The watcher and the worker react to the same notice through different paths. If the watcher stopped the runner immediately, GitHub's completed event could reach the worker before AWS's warning did, and land on a job not yet marked as interrupted: a plain failure, no re-run. Thirty seconds makes that unlikely, since the warning normally arrives in a few, and still leaves ninety for the runner to report. It narrows the race; it does not close it. We also made the worker re-run an interrupted job that ends as cancelled, for the case where the signal comes too late.
This time the fault injector got there first:
Job failed 72 seconds after the warning, re-run queued at 94, running on a new instance at 119. We called it done.
Attempt three: the test had one job
Six days later, over one busy afternoon, AWS reclaimed several instances with jobs on them. Five jobs failed. Three of them, all from our largest repository, were not re-run. A colleague re-ran them by hand a few minutes later.
The worker had asked. GitHub had said no. GitHub refuses to re-run a job while its workflow run is in progress, and those jobs had a dozen siblings still running: the other test shards, the coverage job, the gate. Our test run had contained exactly one job, so its run finished the moment its job failed.
The re-run now waits for the run:
const rerunInterrupted = async (job: {repo: string; jobId: string; runId: number}, tries = 0) => { const run = await getRun(job.repo, job.runId); if (!run || run.run_attempt > 1) return 'rerun-superseded'; // someone re-ran it already if (run.status !== 'completed') { if (tries >= MAX_RERUN_TRIES) return 'rerun-given-up'; // 15 checks, 30 minutes await scheduleRerun({...job, tries: tries + 1}); // a one-shot schedule, 2 minutes from now return 'rerun-scheduled'; } await rerunJob(job.repo, job.jobId); return 'rerun'; }; The schedule is a one-shot EventBridge Scheduler entry that invokes the same worker and deletes itself. Its name is the job plus the check number, so a duplicate completed event that arrives while the entry exists collides with it and schedules nothing. One that arrives later starts a second chain of checks, which is harmless: whichever chain comes second finds the run already at its second attempt and stops.
This time the test had two jobs, four minutes and nine, and we interrupted the short one:
Four checks found the run still in progress. The fifth, 109 seconds after the run completed, was accepted.
What it does not do
- It does not save the work. The job starts again from the beginning. Checkpointing a test run across machines is a different project.
- It tries once. An interrupted re-run stays failed, and a person re-runs it.
- It gives up after 30 minutes. If a job's siblings are still running half an hour after it failed, nobody re-runs it automatically.
- The re-run waits for the slowest sibling. That is GitHub's rule, not ours. The pull request goes red first and green later. Polling every two minutes adds up to two minutes on top.
- Most interruptions need none of this. In the week we looked at, twelve interruptions hit a running job, and seven of those jobs finished inside the warning.
What we took from it
- "The platform will notice" is a hypothesis. GitHub did not notice a dead runner for eleven minutes and would not have for six hours. Use the warning you are given.
- Test with the shape of production. A one-job run and a twelve-job run are different systems to GitHub. Our first test passed for a reason that did not hold anywhere else.
- A fault injector is cheap. Each experiment cost a few cents and failed one job on purpose. The alternative was waiting for AWS to run the test for us, which it did, twice.



