Skip to content

fix(scale-down): re-read the runner before deregistering instead of trusting the cycle snapshot - #5536

Open
andersjanmyr wants to merge 3 commits into
github-aws-runners:mainfrom
smrt:fix/scale-down-reread-before-deregister
Open

andersjanmyr wants to merge 3 commits into
github-aws-runners:mainfrom
smrt:fix/scale-down-reread-before-deregister

Conversation

@andersjanmyr

@andersjanmyr andersjanmyr commented Oct 19, 2026 •

Copy link
Copy Markdown

Description

Follow-up to #5397. The idle confirmation window fixes the stale busy flag, but scale-down still decides on a stale instance snapshot: scaleDown lists every runner once, then walks them with one or more GitHub calls each. On a fleet of 60+ runners the snapshot is a minute old by the time a runner is reached. That is long enough for a job-started hook to have tagged the instance ghr:bypass-removal, or for a parallel busy reading to have cleared its idle marker, and removeRunner deregisters on what it listed. That is how we still saw runners terminated seconds into a job with the window enabled.

This PR re-reads the runner right before deregistering:

  • ScaleDownComputeProvider gains describe(id), returning the current RunnerInfo or undefined when the runner no longer exists. Ec2RunnerResourceOperations gets the matching describe, backed by DescribeInstances for that one instance and mapped through the same getRunnerInfo as list. The template provider stubs it as not implemented.
  • removeRunner calls it after the not-busy reading has passed the idle-confirmation window and before deleteGitHubRunner. A runner that now carries bypass-removal is skipped. If the current idle marker differs from the listed one, the window is re-evaluated on the current marker, so a busy reading elsewhere that cleared the marker restarts the window rather than terminating on stale evidence. A runner whose compute resource is gone is still de-registered from GitHub, without a terminate call, so no registration is left behind.
  • The EC2 describe restricts to running/pending instances like list does, and maps InvalidInstanceID.NotFound to "gone".

Cost is one DescribeInstances per runner that would otherwise be terminated in this cycle, not per listed runner and not per candidate still inside the window. No IAM or Terraform changes.

This composes with #5463: paging shrinks the staleness but a runner can still pick up a job between its page's list call and its turn.

Test Plan

  • yarn format-check, yarn lint, yarn test, yarn build all green (671 tests across control-plane and compute-providers)
  • New tests in scale-down.test.ts: runner tagged bypass-removal after the list call is not deregistered; runner that disappeared during the cycle is de-registered but not terminated; the window is decided on the current marker, not the list snapshot; no re-read while the window is still open. Provider contract test and EC2 capability/operations tests cover describe, including the NotFound mapping.
  • Deployed to our fleet (up to 72 runners, scale_down_idle_confirmation_seconds = 300, this change on top of v7.11.0 plus feat(scale-down): opt-in idle confirmation window before terminating not-busy runners #5397) on 2026-10-19. Will report observations here.

Related Issues

#5085, #5397, #5463

🤖 Generated with Claude Code

@andersjanmyr
andersjanmyr marked this pull request as ready for review October 19, 2026 12:02
@andersjanmyr
andersjanmyr requested a review from a team as a code owner October 19, 2026 12:02
andersjanmyr added a commit to smrt/terraform-aws-github-runner that referenced this pull request Oct 20, 2026
… terminating

Review feedback on github-aws-runners#5536:

- A runner whose compute resource disappeared during the cycle was
  skipped, leaving its registration behind on GitHub. It is now
  de-registered without a terminate call; nothing can be running on a
  resource that is gone.
- DescribeInstances was called for every runner reaching the deregister
  path. The idle-confirmation window is now evaluated on the listed
  snapshot first, and the runner is only re-read when that evaluation
  would terminate it, so the call is paid once per termination rather
  than once per candidate. The current marker is re-evaluated only when
  it differs from the listed one.
- The EC2 describe restricts to running and pending instances, matching
  list, and maps InvalidInstanceID.NotFound to "gone" instead of
  throwing.
- Drop a duplicated key in the registry contract test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
andersjanmyr added a commit to smrt/terraform-aws-github-runner that referenced this pull request Oct 20, 2026
… terminating

Review feedback on github-aws-runners#5536:

- A runner whose compute resource disappeared during the cycle was
  skipped, leaving its registration behind on GitHub. It is now
  de-registered without a terminate call; nothing can be running on a
  resource that is gone.
- DescribeInstances was called for every runner reaching the deregister
  path. The idle-confirmation window is now evaluated on the listed
  snapshot first, and the runner is only re-read when that evaluation
  would terminate it, so the call is paid once per termination rather
  than once per candidate. The current marker is re-evaluated only when
  it differs from the listed one.
- The EC2 describe restricts to running and pending instances, matching
  list, and maps InvalidInstanceID.NotFound to "gone" instead of
  throwing.
- Drop a duplicated key in the registry contract test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
andersjanmyr and others added 3 commits November 19, 2026 14:34
…rusting the cycle snapshot

The cycle lists every runner once, then walks them with one or more
GitHub calls each. On a fleet of 60+ runners the instance snapshot is a
minute old by the time a runner is reached, which is long enough for a
job-started hook to have tagged it ghr:bypass-removal, or for a parallel
busy reading to have cleared its idle marker. Deregistering on the stale
snapshot is how a runner gets terminated seconds into a job.

ScaleDownComputeProvider gains describe(id), which returns the current
state of one runner, or undefined when it no longer exists. The EC2
operations client backs it with DescribeInstances for that one instance;
the template provider stubs it as not implemented.

removeRunner calls describe after the not-busy reading and before
deregistering:

- a runner that no longer exists is skipped
- a runner that now carries bypass-removal is skipped, since the
  job-started hook set the tag between the list call and this turn
- the idle-confirmation window is evaluated on the current marker, not
  the listed one; a busy reading elsewhere that cleared the marker
  restarts the window instead of terminating on stale evidence

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… terminating

Review feedback on github-aws-runners#5536:

- A runner whose compute resource disappeared during the cycle was
  skipped, leaving its registration behind on GitHub. It is now
  de-registered without a terminate call; nothing can be running on a
  resource that is gone.
- DescribeInstances was called for every runner reaching the deregister
  path. The idle-confirmation window is now evaluated on the listed
  snapshot first, and the runner is only re-read when that evaluation
  would terminate it, so the call is paid once per termination rather
  than once per candidate. The current marker is re-evaluated only when
  it differs from the listed one.
- The EC2 describe restricts to running and pending instances, matching
  list, and maps InvalidInstanceID.NotFound to "gone" instead of
  throwing.
- Drop a duplicated key in the registry contract test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The incremental EC2 cleanup tests (github-aws-runners#5463) swap in paged providers and
legacy list fixtures without a describe stub, so the pre-deregister
re-read saw every runner as vanished. A paged provider's describe now
reports what its latest page listed.

Also covers a vanished paged runner without a stored GitHub id: it is
de-registered without persisting the id onto the missing instance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andersjanmyr
andersjanmyr force-pushed the fix/scale-down-reread-before-deregister branch from 8fe7ed1 to e7967ca Compare November 19, 2026 13:35
@andersjanmyr

andersjanmyr commented Nov 19, 2026 •

Copy link
Copy Markdown
Author

Rebased onto main (e167368) after #5463 changed scale-down to clean runners incrementally.

Two things changed while porting:

  • Vanished instances skip the GitHub runner ID write. fix(scale-down): clean EC2 runners incrementally and isolate failures #5463 saves a name-verified GitHub runner ID with setGitHubRunnerId before deregistering. If the re-read finds the instance gone, there is nothing to tag, and the tag write would fail and keep the registration. The runner is now de-registered without that write and without a terminate call, which matches the earlier review feedback.
  • Test fixtures stub describe. The incremental-inventory tests from fix(scale-down): clean EC2 runners incrementally and isolate failures #5463 swap in paged providers (and one legacy list fixture) without a describe stub, so the re-read saw every runner as vanished. A small withPagedDescribe helper makes a paged provider's describe return what its latest page listed, which is how DescribeInstances behaves. A new test covers the vanished-instance path above.

control-plane (515) and compute-providers (394) tests pass. eslint and tsc are clean for control-plane.

The re-read has run in our CI fleet on v7.11.0 since October. We are about to deploy this rebased version on v8.1.1.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants