Skip to content

Reject status patches computed from a stale AppWrapper - #2

Merged
mike-ainsel merged 1 commit into
milabfrom
fix/status-patch-optimistic-lock
Nov 25, 2026
Merged

mike-ainsel merged 1 commit into
milabfrom
fix/status-patch-optimistic-lock

Conversation

@DenKoren

Copy link
Copy Markdown
Member

Stacked on #1 (base: feat/v1.2.3-concurrency-quay-ci).

Problem

Each reconcile reads the AppWrapper from the informer cache and writes status with a merge patch that has no resourceVersion check (client.MergeFrom(orig)). If the key is requeued while a reconcile runs (for example by a Pod event), the next reconcile can start before the cache shows the previous status patch. It then decides from the old phase and writes over the newer status.

Upstream has this problem with one worker too. With maxConcurrentReconciles > 1 from #1, a requeued AppWrapper goes to a free worker at once instead of waiting behind other AppWrappers, so stale reads happen more often.

Change

  • All Status().Patch calls use statusPatch(orig) = client.MergeFromWithOptions(orig, client.MergeFromWithOptimisticLock{}). The patch carries the resourceVersion of the copy the decision was made from. If the AppWrapper changed on the API server, the patch is rejected with 409 Conflict. copyForStatusPatch already copies ObjectMeta, and each successful patch writes the server response back into aw, so sequential patches in one reconcile (createComponent) keep a current resourceVersion.
  • Reconcile is a thin wrapper: on a Conflict it requeues after 1s without an error (no "Reconciler error" log, no error backoff). The state machine moved unchanged to reconcile.
  • In the Resuming phase, a Conflict from createComponents is returned as is. It is not counted as a create failure, so it cannot lead to CreateFailed / resetOrFail.
  • MILAB.md: new row in the list of changes on top of upstream.

Not covered: Create / Delete calls that a stale reconcile makes before its status patch is rejected. They are idempotent here (AlreadyExists with our controller reference is accepted, NotFound on delete counts as deleted). To remove stale reads fully, the AppWrapper would have to be read with the uncached reader at the start of each reconcile, at the cost of one API GET per reconcile.

Tests

  • New envtest case "A status patch that uses a stale AppWrapper is rejected and requeued": a client returns an old copy of the AppWrapper, a component is deleted, and the reconcile tries Running -> Failed. The patch is rejected, the phase on the server stays Running, and Reconcile returns no error with RequeueAfter > 0. The next reconcile with a current copy moves the AppWrapper to Failed.
  • Without the optimistic lock, this test fails: the stale reconcile writes Failed.
  • go test -race ./internal/controller/... ./pkg/... passes locally (envtest 1.37.0).

The milab workflow runs only for PRs into milab, so CI does not run on this PR until #1 is merged and the base changes to milab.

@greptile-apps

greptile-apps Bot commented Nov 25, 2026

Copy link
Copy Markdown

Automatic reviews are disabled for this repository. Configure in Code Review Settings.

@DenKoren
DenKoren added this pull request to stack #3 November 25, 2026 10:41
Base automatically changed from feat/v1.2.3-concurrency-quay-ci to milab November 25, 2026 12:47
Status patches now carry the resourceVersion of the AppWrapper they were
computed from (MergeFromWithOptimisticLock). A reconcile that read a stale
copy from the cache gets a Conflict from the API server and does not write
over a newer status.

Reconcile treats a Conflict as a requeue, not as an error. The Resuming
phase no longer counts a Conflict as a component create failure.

With maxConcurrentReconciles > 1, a requeued AppWrapper goes to a free
worker at once, often before the informer has delivered the previous
status patch. Stale reads are then more frequent than with one worker.
@mike-ainsel
mike-ainsel force-pushed the fix/status-patch-optimistic-lock branch from f15ebc1 to b2710dc Compare November 25, 2026 12:47
@mike-ainsel
mike-ainsel merged commit 01b1e30 into milab Nov 25, 2026
3 checks passed
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