You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Scale-down can terminate a runner that picks up a job between busy check and termination #5085
We hit an issue where the scale-down lambda terminated an EC2 instance while it was actively running a GitHub Actions job (a Helm deploy, in our case). The job failed with:
The runner has received a shutdown signal. This can happen when the runner service is stopped, or a manually started runner is canceled.
We traced it back to a race condition in removeRunner in scale-down.ts. The current flow is:
Check if the runner is busy via the GitHub API → returns false
Deregister the runner from GitHub (delete)
Terminate the EC2 instance
The problem is that a job can be assigned to the runner between step 1 and step 2. By the time the instance is terminated, it's already running a job. The runner gets killed mid-execution.
We confirmed this by matching the CloudTrail TerminateInstances event to the exact runner that was running the failed job. The scale-down lambda terminated the instance 13 seconds after the Helm deploy started.
Suggested fix
Swap the order: deregister first, then re-check busy state.
Check busy (fast-path to skip obviously busy runners)
Deregister from GitHub (prevents new job assignment server-side)
Re-check busy state — this check is now stable since no new jobs can be assigned after deregistration
If the runner became busy between step 1 and step 2, the in-flight job still completes because the runner worker uses job-scoped OAuth credentials, not the runner registration. The worker creates its own VssConnection from the job message and never checks registration status during execution. Deregistration only affects the listener (no new job pickup), not the worker (current job).
If the re-check finds the runner busy, we skip termination and let the instance be cleaned up as an orphan once the job finishes.
Environment
Module version: 7.3.0 (also verified the same code exists in 7.5.0)
Runner type: on-demand, non-ephemeral, repo-level
Runner config: aws-4-ubuntu (c7a.xlarge)
Happy to put up a PR if this approach sounds reasonable.
Reacted by Nelson Wolf, jeremy-ke, tom-pavz, Samuel Pelegrinello Caipers, Alex Faunt, eli, Jason Anderson and Chiara Campagnola
This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs. Thank you for your contributions.
CloudTrail confirms TerminateInstances was called by the *-scale-down-lambda role at 18:22:16Z, StateReason: Client.UserInitiatedShutdown (i.e. an explicit API call, not a spot reclaim — this was an on-demand instance).
The GitHub Actions job log for that runner shows the job had just started its next step (a run: step invoking rake parallel:create && rake parallel:load_schema) about 1.8 seconds before the shutdown signal arrived:
##[error]The runner has received a shutdown signal. This can happen when the runner service is stopped, or a manually started runner is canceled.
##[error]The operation was canceled.
This lines up with the busy-check race described above — the runner was almost certainly picking up the next job step (or the runner agent briefly reported idle between steps) in the ~60s window between the "Busy: false" observation and termination.
+1 on the suggested fix (deregister first, then re-check busy state before terminating). Happy to help test a patch against our setup if useful.
We hit this same race in production on 2026-07-02 on module v6.7.3 (multi-runner, non-ephemeral, on-demand, org-level runners, scale-down on the default 5-minute schedule), and can add two things to the reports above: frequency data and evidence that the busy flag can read false for runners that have been mid-job for tens of minutes, which affects how far the proposed fix goes.
Frequency. After tracing one killed job, we scanned the last 3 days of failed runs in the affected repo for the signature (job conclusion failure + "The operation was canceled." annotation + self-hosted runner) and cross-checked every hit against CloudTrail: 22 jobs killed in 3 days, all 22 matching a TerminateInstances call from a scale-down Lambda at the second the job died. One scale-down invocation terminated 5 "idle" instances of which 3 were running just-assigned jobs. Kill rate scales with job churn (short-job fan-outs make it much worse).
Variant A: the assignment race described in this issue. Representative timeline (all UTC, from the jobs API, the scale-down Lambda log, and CloudTrail):
17:55:39 previous job completes on i-0f2f82c334e502c07 (runner ID 8374)
17:55:42 GitHub assigns a new job to that runner
17:55:44 scale-down invocation starts
17:55:51 "Runner 'i-0f2f82c334e502c07' - GitHub Runner ID '8374' - Busy: false"
17:55:52 TerminateInstances (CloudTrail, scale-down Lambda role)
17:55:53 job log: "The runner has received a shutdowan signal."
Variant B: busy: false long after job start. Two cases from the same day where the runner had been executing a job for a long time when the Lambda read busy:
false and terminated it:
We verified via the workflow-jobs API that each killed job's runner_id exactly matches the runner ID the Lambda checked, so this is not a stale/duplicate
registration being consulted — the API returned busy: false for the correct, actively-working runner. This matches @npwolf 's true → true → false observation
above.
Opened #5228 with an opt-in fix that covers both this race and the harder variant in the comments (busy reading false for runners mid-job for tens of minutes, where deregister-then-recheck can't help because the DELETE also succeeds): an idle-confirmation window — terminate only when not-busy readings span two consecutive scale-down evaluations, any busy reading resets. Default off. Running on our fleet since 2026-07-28: baseline ~1 kill burst/hour → zero mid-job terminations in 14 hours.
Timeline: (runner's own diagnostic log, the scale-down lambda log and CloudTrail)
11:45:48Z runner accepts job (Runner_ diag: "Running job: ...")
11:50:1xZ scale-down: "Runner 'i-0449…' - GitHub Runner ID '1193575' - Busy: false"
11:50:14Z DELETE self-hosted runner succeeds (lambda logs "terminated and GitHub
runner is de-registered", which 7.7.1 only logs when every
deregistration returned 204)
11:50:15Z CloudTrail TerminateInstances (user: scale-down lambda)
11:50:15Z job log: "The runner has received a shutdown signal"
Two things stand out compared to the 13-second window in the original report:
The runner had been executing the job for 4.5 minutes when the per-runner busy re-check (getGitHubRunnerBusyState, the call added
specifically to minimize this race) still returned busy: false.
GitHub then accepted the deregistration (204) for a runner that was
mid-job — the documented behavior is that removing a busy runner fails,
which is presumably why terminating after an all-204 response was
considered safe.
#5397 carries @jensenbox's idle confirmation window from #5228 forward onto current main (the plugin refactor meant it no longer applied). Opt-in via scale_down_idle_confirmation_seconds, default off. Covers both the assignment-lag and the stale-mid-job variants reported here.
Activity
fix(lambda): add post-deregister busy check to prevent terminating ac…
github-actions commentedon Jun 29, 2026
This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs. Thank you for your contributions.
npwolf commentedon Jul 2, 2026
We hit this exact race condition today (2026-07-02) on module version
v7.4.0. Confirmed via CloudTrail + Lambda logs, timeline below.Runner:
i-0e6df2f4029298b42(on-demand, non-ephemeral,c6a.4xlarge, ubuntu 22.04), running an RSpec job via a GitHub Actions workflow.Scale-down Lambda log (
ghrun-*-scale-down), polling roughly every 60s:CloudTrail confirms
TerminateInstanceswas called by the*-scale-down-lambdarole at18:22:16Z,StateReason: Client.UserInitiatedShutdown(i.e. an explicit API call, not a spot reclaim — this was an on-demand instance).The GitHub Actions job log for that runner shows the job had just started its next step (a
run:step invokingrake parallel:create && rake parallel:load_schema) about 1.8 seconds before the shutdown signal arrived:This lines up with the busy-check race described above — the runner was almost certainly picking up the next job step (or the runner agent briefly reported idle between steps) in the ~60s window between the "Busy: false" observation and termination.
+1 on the suggested fix (deregister first, then re-check busy state before terminating). Happy to help test a patch against our setup if useful.
ben-smyth commentedon Jul 2, 2026
We hit this same race in production on 2026-07-02 on module v6.7.3 (multi-runner, non-ephemeral, on-demand, org-level runners, scale-down on the default 5-minute schedule), and can add two things to the reports above: frequency data and evidence that the busy flag can read false for runners that have been mid-job for tens of minutes, which affects how far the proposed fix goes.
Frequency. After tracing one killed job, we scanned the last 3 days of failed runs in the affected repo for the signature (job conclusion failure + "The operation was canceled." annotation + self-hosted runner) and cross-checked every hit against CloudTrail: 22 jobs killed in 3 days, all 22 matching a TerminateInstances call from a scale-down Lambda at the second the job died. One scale-down invocation terminated 5 "idle" instances of which 3 were running just-assigned jobs. Kill rate scales with job churn (short-job fan-outs make it much worse).
Variant A: the assignment race described in this issue. Representative timeline (all UTC, from the jobs API, the scale-down Lambda log, and CloudTrail):
Variant B: busy: false long after job start. Two cases from the same day where the runner had been executing a job for a long time when the Lambda read busy:
false and terminated it:
We verified via the workflow-jobs API that each killed job's runner_id exactly matches the runner ID the Lambda checked, so this is not a stale/duplicate
registration being consulted — the API returned busy: false for the correct, actively-working runner. This matches @npwolf 's true → true → false observation
above.
Happy to test a patched build against our fleet
jensenbox commentedon Jul 28, 2026
Opened #5228 with an opt-in fix that covers both this race and the harder variant in the comments (busy reading false for runners mid-job for tens of minutes, where deregister-then-recheck can't help because the DELETE also succeeds): an idle-confirmation window — terminate only when not-busy readings span two consecutive scale-down evaluations, any busy reading resets. Default off. Running on our fleet since 2026-07-28: baseline ~1 kill burst/hour → zero mid-job terminations in 14 hours.
tom-pavz commentedon Aug 5, 2026
We are also facing this issue currently, thanks for opening the fix PR!
spcaipers-arm commentedon Aug 12, 2026
Also facing this issue. Reproduced in the v7.10.1 (latest at the moment).
andersjanmyr commentedon Aug 27, 2026
We also got this problem yesterday.
Setup: module 7.7.1, org-level non-ephemeral runners, pool + idle_config, spot instances.
Timeline: (runner's own diagnostic log, the scale-down lambda log and CloudTrail)
Two things stand out compared to the 13-second window in the original report:
per-runner busy re-check (
getGitHubRunnerBusyState, the call addedspecifically to minimize this race) still returned
busy: false.mid-job — the documented behavior is that removing a busy runner fails,
which is presumably why terminating after an all-204 response was
considered safe.
spcaipers-arm commentedon Aug 27, 2026
@andersjanmyr I am seeing similar issues everyday, hopefully they merge the fix soon.
lochlanbennettodlum-taktile commentedon Sep 8, 2026
#5397 carries @jensenbox's idle confirmation window from #5228 forward onto current
main(the plugin refactor meant it no longer applied). Opt-in viascale_down_idle_confirmation_seconds, default off. Covers both the assignment-lag and the stale-mid-job variants reported here.feat(scale-down): opt-in idle confirmation window before terminating …