Fix TOCTOU race condition in scale-down that terminates busy runners#6
Closed
Fix TOCTOU race condition in scale-down that terminates busy runners#6
Conversation
The scale-down lambda had a 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 (e.g., Helm deploys) to be killed mid-execution when the instance was terminated. The fix adds a post-deregistration busy re-check: 1. Check busy state (fast-path to skip busy runners) 2. Deregister the runner from GitHub (prevents new job assignment) 3. Re-check busy state after deregistration After deregistration, GitHub cannot assign new jobs to the runner, so the re-check is stable (no more race window). If the runner became busy between step 1 and step 2, the in-flight job will complete using its job-scoped OAuth token and the instance is left for orphan cleanup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Context
On 2026-03-27, the `deploy-ssr-rover` job in the Webapp Deploy workflow was killed mid-Helm-deploy when the scale-down lambda terminated instance `i-06d1f1037840a1952` while it was actively running a job. CloudTrail confirmed the `github-actions-aws-4-ubuntu-scale-down` lambda terminated the instance at exactly the moment the runner received a shutdown signal.
The root cause was a TOCTOU (time-of-check to time-of-use) race:
How the fix works
The new flow:
If the re-check finds the runner busy, we leave the instance running. The in-flight job continues to completion because the runner worker uses job-scoped OAuth credentials, not the runner registration:
The worker never checks runner registration status during job execution. Deregistration only affects the listener loop (which polls for new messages), not the worker process running the current job.
Test plan