Skip to content

Scale-down can terminate a runner that picks up a job between busy check and termination #5085

Closed

Description

@JVenberg

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:

  1. Check if the runner is busy via the GitHub API → returns false
  2. Deregister the runner from GitHub (delete)
  3. 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.

  1. Check busy (fast-path to skip obviously busy runners)
  2. Deregister from GitHub (prevents new job assignment server-side)
  3. 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.

Activity

  1. added a commit that references this issue on Mar 30, 2026
    b9e8871
  2. github-actions commented on Jun 29, 2026

    @github-actions
    Contributor

    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.

  3. npwolf commented on Jul 2, 2026

    @npwolf

    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:

    18:20:15 - Runner 'i-0e6df2f4029298b42' - Busy: true  -> skipped
    18:21:15 - Runner 'i-0e6df2f4029298b42' - Busy: true  -> skipped
    18:22:15 - Runner 'i-0e6df2f4029298b42' - Busy: false -> proceeds
    18:22:16 - AWS runner instance 'i-0e6df2f4029298b42' is terminated and GitHub runner is de-registered.
    

    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.

  4. ben-smyth commented on Jul 2, 2026

    @ben-smyth

    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.

    Happy to test a patched build against our fleet

  5. jensenbox commented on Jul 28, 2026

    @jensenbox
    Contributor

    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.

  6. tom-pavz commented on Aug 5, 2026

    @tom-pavz

    We are also facing this issue currently, thanks for opening the fix PR!

  7. spcaipers-arm commented on Aug 12, 2026

    @spcaipers-arm
    Contributor

    Also facing this issue. Reproduced in the v7.10.1 (latest at the moment).

  8. andersjanmyr commented on Aug 27, 2026

    @andersjanmyr

    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)

    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:

    1. 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.
    2. 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.
  9. spcaipers-arm commented on Aug 27, 2026

    @spcaipers-arm
    Contributor

    @andersjanmyr I am seeing similar issues everyday, hopefully they merge the fix soon.

  10. lochlanbennettodlum-taktile commented on Sep 8, 2026

    @lochlanbennettodlum-taktile
    Contributor

    #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.

  11. added a commit that references this issue on Sep 9, 2026
    595f3e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions