Guard the listener's Running phase write with resourceVersion - #4648
Open
nikola-jokic wants to merge 1 commit into
Open
Guard the listener's Running phase write with resourceVersion#4648nikola-jokic wants to merge 1 commit into
nikola-jokic wants to merge 1 commit into
Conversation
nikola-jokic
requested review from
a team,
Steve-Glass,
mumoshu,
rentziass and
toast-gear
as code owners
September 10, 2026 16:33
nikola-jokic
force-pushed
the
nikola-jokic/guard-job-started-phase-write
branch
from
September 10, 2026 20:56
3aabeeb to
f4c21f8
Compare
nikola-jokic
changed the base branch from
nikola-jokic/workqueue-predicates
to
nikola-jokic/ars-outdated-phase
September 10, 2026 21:03
nikola-jokic
force-pushed
the
nikola-jokic/guard-job-started-phase-write
branch
from
September 10, 2026 21:03
f4c21f8 to
e412566
Compare
nikola-jokic
added this pull request to stack #4645
September 10, 2026 21:11
HandleJobStarted reads the ephemeral runner to decide whether the runner is still eligible for the Running phase, then applies that decision with a separate merge patch. The patch carried no precondition, so nothing tied it to the state the decision was made on: if the ephemeral runner controller wrote a terminal phase in between, the listener silently overwrote it and resurrected a runner that had already finished. Attach the observed resourceVersion to the patch whenever the phase is transitioned, and retry on conflict so the decision is re-made against the fresh state. The job detail fields stay unguarded: they are write-once metadata that the runner set only consults for runners that are neither done nor being deleted, so patching them cannot change a scaling decision. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
nikola-jokic
force-pushed
the
nikola-jokic/guard-job-started-phase-write
branch
from
September 11, 2026 09:52
e412566 to
9c9cc51
Compare
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The reviewed changes address stale writes with comprehensive conflict and API-server coverage, with no unresolved blocking issues.
Review tier: Lite
Findings: None
What changed in this PR
Prevents HandleJobStarted from resurrecting terminal runners by guarding phase writes with resourceVersion and retrying conflicts.
Changes:
- Adds optimistic concurrency protection and conflict retries.
- Expands fake API tests for terminal-state races.
- Adds envtest coverage against a real API server.
| File | Description |
|---|---|
cmd/ghalistener/scaler/scaler.go |
Guards phase updates and retries stale writes. |
cmd/ghalistener/scaler/scaler_test.go |
Tests preconditions, conflicts, and retries. |
cmd/ghalistener/scaler/scaler_apiserver_test.go |
Validates behavior against kube-apiserver. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Stacked on #4652 (which is stacked on #4647).
The problem
HandleJobStartedis a read-modify-write across two API calls:EphemeralRunnerto decide whether it is still eligible forRunning(notFailed/Succeeded/Outdated, not being deleted)./statusapplying that decision.The patch carried no precondition, so nothing tied it to the state the decision was made on. If the ephemeral runner controller wrote a terminal phase between the two calls —
markAsFailed,markAsSucceeded,markAsOutdated, all non-optimistic merge patches themselves — the listener's patch won last and resurrected a runner that had already finished.The existing guard is not wrong, it just only covers the lag case it was written for (a short job that completes before the listener drains the
JobStartedmessage). The narrow interleaving was unprotected.This is reproducible against a real API server:
TestHandleJobStartedAgainstAPIServer/does_not_resurrect_a_runner_that_failed_after_the_readfails on the parent commit withexpected: "Failed", actual: "Running".The fix
Attach the observed
metadata.resourceVersionto the merge patch whenever the phase is transitioned, and wrap the read-modify-write inretry.RetryOnConflict. The API server then rejects a patch built on a stale read with a 409, and the runner is re-read so the eligibility decision is made again against fresh state.The job detail fields (
jobId,jobRequestId,jobRepositoryName,workflowRunId,jobWorkflowRef,jobDisplayName) are deliberately left unguarded. They are write-once metadata, and the runner set only consults them (HasJob()) for runners that are neither done nor being deleted — both call sites are behind!isDone &&, and deletion is classified before phase. Patching them onto a terminal runner cannot change a scaling decision, so putting them behind a precondition would only add avoidable conflict retries on a path that has no correctness requirement.Tests
scaler_test.go: the fake API server now tracksresourceVersionand emulates the optimistic concurrency check, with a hook that mutates the runner between the read and the patch. New cases cover the precondition being sent (and correctly not sent for terminal runners), no resurrection for each ofFailed/Succeeded/Outdated, and conflict-retry exhaustion.scaler_apiserver_test.go(new): runsHandleJobStartedagainst envtest so the fake's emulation is pinned to real kube-apiserver behaviour. The race is made deterministic by writing the terminal phase from inside the client transport, immediately before the scaler's patch is forwarded. Skips whenKUBEBUILDER_ASSETSis unset.Verified that every new test fails on the parent commit and passes here.