Skip to content

fix: keep in-flight builds from blocking the prebuild hard limit - #30060

Merged
BobbyHo merged 3 commits into
coder:mainfrom
PushTheLimit:fix/prebuilds-hard-limit-in-flight
Nov 9, 2026
Merged

BobbyHo merged 3 commits into
coder:mainfrom
PushTheLimit:fix/prebuilds-hard-limit-in-flight

Conversation

@PushTheLimit

Copy link
Copy Markdown
Contributor

Fixes #30058.

GetPresetsAtFailureLimit ranks a preset's newest start builds across every status, then counts only the failed ones among the top hard_limit. A pending or running build takes a slot without being counted, so while a build is in flight the count cannot reach the limit. The reconciler reads this once per cycle and then creates prebuilds one at a time. When a create takes about as long as a failing build, every snapshot sees a build in flight and the preset is never hard-limited.

The query now ranks only finished builds (succeeded, failed, canceled). An in-flight build neither hides earlier failures nor counts as one. A canceled or succeeded build still breaks a failure streak, as before, and once the in-flight build finishes it is ranked like any other.

Tests

  • TestGetPresetsAtFailureLimit gains two table cases with a pending build newer than the rest: three failures before it hit the limit, and a success before it does not.
  • TestHardLimitedPresetWithBuildInFlight, modelled on TestSkippingHardLimitedPresets: with failure_hard_limit = 1, a failed prebuild and a newer pending one, reconciling marks the preset hard-limited and creates nothing.
  • The querier test helper createPrebuiltWorkspace left "successful" jobs pending, since it never set CompletedAt. It now completes them when succeededJob is set, and the existing success cases use it, so they test what their names say.

Without the query change (the new tests only):

--- FAIL: TestGetPresetsAtFailureLimit
    --- FAIL: TestGetPresetsAtFailureLimit/last_3_builds_are_failed_and_a_newer_one_is_in_flight_-_hard_limit_is_reached
            	Error:      	"[]" should have 1 item(s), but has 0
FAIL	github.com/coder/coder/v2/coderd/database	3.276s
--- FAIL: TestHardLimitedPresetWithBuildInFlight
        	Error:      	"[...3 workspaces...]" should have 2 item(s), but has 3
FAIL	github.com/coder/coder/v2/enterprise/coderd/prebuilds	2.541s

With it:

    --- PASS: TestGetPresetsAtFailureLimit/last_build_is_successful_and_a_newer_one_is_in_flight_-_hard_limit_is_NOT_reached
    --- PASS: TestGetPresetsAtFailureLimit/last_3_builds_are_failed_and_a_newer_one_is_in_flight_-_hard_limit_is_reached
ok  	github.com/coder/coder/v2/coderd/database	2.792s
--- PASS: TestHardLimitedPresetWithBuildInFlight
ok  	github.com/coder/coder/v2/enterprise/coderd/prebuilds	2.320s

coderd/database, coderd/prebuilds and enterprise/coderd/prebuilds pass, and golangci-lint reports no issues.

We have also run this on a live deployment. A schedule-driven preset whose builds fail after 90s (longer than the 60s reconcile interval, so a build was in flight at every snapshot) stopped after 4 failed builds and created nothing more for the rest of the 20-minute observation. Before, the same shape produced 156 failed builds in about 2 hours.

AI disclosure

Written with AI assistance (Claude Code). I reviewed the change, ran the tests above myself, and verified the behaviour on a live deployment.

…d hard limit

GetPresetsAtFailureLimit ranked a preset's builds across every status and
then counted only the failed ones among the newest hard_limit. A pending or
running build took a slot without being counted, so a preset that kept a
build in flight at every reconcile snapshot never reached the limit and
failed indefinitely.

Rank only finished builds. The querier test helper now marks successful jobs
as completed, since the existing "successful" cases were really pending jobs.

Fixes coder#30058
@github-actions github-actions Bot added the community Pull Requests and issues created by the community. label Sep 28, 2026
@PushTheLimit PushTheLimit changed the title fix(coderd/database): keep in-flight builds from blocking the prebuild hard limit fix: keep in-flight builds from blocking the prebuild hard limit Sep 28, 2026
@github-actions github-actions Bot added the stale This issue is like stale bread. label Nov 4, 2026
@github-actions github-actions Bot closed this Nov 8, 2026
@BobbyHo BobbyHo reopened this Nov 9, 2026
@BobbyHo
BobbyHo temporarily deployed to main November 9, 2026 03:28 — with GitHub Actions Inactive
@BobbyHo
BobbyHo temporarily deployed to main November 9, 2026 03:28 — with GitHub Actions Inactive
@BobbyHo

BobbyHo commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Manual test

I ran this change against a local coderd built in a Coder workspace, first on the merge base (80978fa5ef) as a control and then on the PR head (8ca5e1c735), each with a fresh database and a Premium license.

To keep a build in flight at every reconcile cycle, the server ran with one provisioner daemon, a 20 s reconciliation interval, backoff set to 0 and the default hard limit of 3. The template had one preset with two prebuild instances whose build slept 40 s and then exited 1.

What happened:

  • Merge base. A replacement prebuild was created after every failure. After 6.5 minutes there were 9 failed builds and the preset was still healthy with no preset_hard_limited gauge. By 12 minutes it had scheduled 19 builds. With a pending build queued behind the running one, the old ranking counted only one of the three failures.
  • PR head. The limit engaged on the first cycle after the third failure, while the fourth build had been running for about 17 s. preset is hard limited was logged once, the gauge went to 1, every later cycle logged skipping hard limited preset for create operation, and the build count stayed at exactly four for the rest of the observation. That matches the four builds reported in the PR description.
  • In-flight build succeeds after the limit. On a new version with a 60 s build, I let the limit engage and then made the running fourth build succeed. On the next cycle the query no longer returned the preset, the gauge cleared and the reconciler created a fifth prebuild, so creation resumed. The prebuild_status row for the version stayed hard_limited. That row is not exposed anywhere, so the only effect is that the "preset is hard limited" warning would not be logged again for that version. This is pre-existing behaviour that timing alone could not reach before this change, and not something the PR needs to fix. It may be worth a follow-up.

The change behaved as described in every case I tried.

@BobbyHo
BobbyHo self-requested a review November 9, 2026 16:46

@BobbyHo BobbyHo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing this fix to Coder and for the clear write-up in #30058. The change is small and in the right place, the new tests cover the bug, and it behaved as described in my manual run above. Approving.

@BobbyHo
BobbyHo enabled auto-merge (squash) November 9, 2026 18:00
@BobbyHo
BobbyHo merged commit 5563b74 into coder:main Nov 9, 2026
25 checks passed
@BobbyHo
BobbyHo temporarily deployed to main November 9, 2026 18:03 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 9, 2026

This branch was previously deployed

1 inactive deployment
main — 66d35992 Deployed Nov 9, 2026 by BobbyHo via cla #252663
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

community Pull Requests and issues created by the community. stale This issue is like stale bread.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prebuilds: failure hard limit does not engage while the newest build is still in flight

2 participants