Repository navigation
Conversation
…tive runners The scale-down lambda had a TOCTOU race condition where a job could be assigned to a runner between checking its busy state and terminating the EC2 instance. This caused in-flight jobs to be killed mid-execution. The fix adds a post-deregistration busy re-check: 1. Check busy (fast-path to skip busy runners) 2. Deregister from GitHub (prevents new job assignment) 3. Re-check busy (now stable since no new jobs can be assigned) If the runner became busy between step 1 and 2, the in-flight job completes using its job-scoped OAuth token and the instance is left for orphan cleanup. Fixes github-aws-runners#5085
|
This pull request 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. |
|
We independently hit the underlying race condition in production today and posted repro details on the linked issue (#5085 comment). This fix looks correct to us — the reorder (deregister → recheck busy) closes the TOCTOU window since in-flight jobs use job-scoped credentials that don't depend on registration state. This has been open since March without review — @edersonbrilhante @Brend-Smits @npalm would one of you be able to take a look? Happy to help test if useful. |
edersonbrilhante
left a comment
There was a problem hiding this comment.
@npwolf Can you fix the conflicts?
|
Rebased this on current |
|
🤖 Follow-up on #5201 (the rebase of this PR): ran an automated backend + security review of the mechanism, and both independently flagged the same core issue, which I verified against the code myself. The post-deregister busy re-check can't actually observe "busy" in production. Once step 2 deregisters the runner, a subsequent GET on that runner ID returns 404 — and this codebase's Net effect: this fix doesn't close the TOCTOU race it targets — an in-flight job can still be killed, just via a different path (or slightly delayed via orphan cleanup instead of immediately). The new test added for this also mocks a Full findings with file/line detail are posted as inline comments on #5201. Flagging here too since the mechanism gap applies to this PR as originally written, not just the rebase. Would be good to get eyes on whether deregistering before confirming idle is fundamentally the wrong order, or whether there's a way to keep the busy signal alive post-deregistration. |
|
@JVenberg I fixed the conflicts, but I need you to sign your commit. |
|
@JVenberg Can you please fix the conflict? |
…not-busy runners (#5397) ## Description Closes #5085. Supersedes #5228. This is @jensenbox's idle confirmation window from #5228, rebased onto the new provider layout and with the lint failures fixed. All credit for the approach and the production data is theirs, I just carried it forward since it stopped applying after the plugin refactor. Quick recap of why: the GitHub `busy` flag isn't trustworthy enough to terminate on a single reading. People on #5085 have CloudTrail evidence of it reading `false` seconds after a job started, and in some cases minutes into a running job. The deregister-then-recheck idea in #5086 can't help because a GET after DELETE returns 404, which we already treat as "not busy". So instead, opt in with `scale_down_idle_confirmation_seconds`. On a not-busy reading the runner gets tagged and termination is deferred. It's only terminated when a later evaluation still says not-busy and the window has elapsed. A busy reading in between clears the tag and starts over. Default is `0`, which is the current behaviour. A couple of things I changed while porting: - `ScaleDownComputeProvider` gains `markIdle` / `unmarkIdle`. They're required, per @edersonbrilhante's note below about DynamoDB becoming the state store. EC2 uses an instance tag for now, same as `ghr:orphan`, so no IAM changes. The template provider stubs both. - Runners kept warm by `idle_config` also get their tag cleared. They never go through the removal path, so an old tag on one would otherwise let it be terminated on the first not-busy reading once the idle count drops. Added a test for this. - The variable is threaded through the root module, `modules/runners` and `modules/multi-runner` (including the new translation layers). I left the experimental webhook orchestration module for a follow-up. ## Test Plan - `yarn format-check`, `yarn lint`, `yarn build`, `yarn test` all green - New tests for: window start, deferral, termination after the window, busy reset, kept-idle reset, disabled window, EC2 tag payloads - `terraform validate`, `fmt -check` and `terraform test` pass for `modules/runners` and `modules/multi-runner` - From the original PR: 14 hours on the author's fleet with a 300s window, zero mid-job terminations vs roughly one burst per hour before ## Related Issues #5085, #5228, #5086, #5201 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Christian Jensen <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
Summary
Fixes #5085
The scale-down lambda can terminate an EC2 instance while it's actively running a job. This happens because a job can be assigned to a runner between checking its busy state and calling
TerminateInstances.We hit this in production: a Helm deploy was killed mid-execution when the scale-down lambda terminated the instance 13 seconds after the job started. CloudTrail confirmed the
TerminateInstancescall came from the scale-down lambda at the exact moment the runner received a shutdown signal.The race condition
Current flow in
removeRunner:falseThe fix
Add a post-deregistration busy re-check:
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.
Why this is safe
Deregistering a runner does not affect in-flight jobs. The runner worker uses job-scoped OAuth credentials from the job message, not the runner registration:
JobRunner.cslines 80-95: the worker creates its ownVssConnectionusingsystemConnectioncredentials from the job messageTest plan