Skip to content

coord: stealLease's 50 retries are 50 copies of the same certain loss when the round trip exceeds RenewInterval #390

Closed

Description

@scttfrdmn

internal/coord's TestDoCancelsTheActionWhenTheLeaseIsLost failed on main at ca27fbe:

--- FAIL: TestDoCancelsTheActionWhenTheLeaseIsLost (3.38s)
    lease_test.go:658: could not steal the lease at "leases/resource" in 50 attempts;
      last: [s3-backend:PutObjectIf] PRECONDITION_FAILED: S3 declined the write: the precondition did not hold

It passed on re-run, so it reads as an ordinary flake. It is not, and the distinction matters because
the retry loop it failed in was already added to fix a flake in this same test.

Why 50 retries did not help

stealLease reads the lease object's ETag, then conditionally writes against it. Inside a
Lease.Do action it races that action's renewal ticker for the same ETag.

A HEAD+PUT round trip against the in-process testaws endpoint measures ~4ms mean, ~6.4ms worst
on an idle laptop (measured with a throwaway probe, not estimated). The test sets
RenewInterval = 10ms.

Once the round trip exceeds RenewInterval, a tick lands between the HEAD and the PUT on every
attempt. So the 50 attempts are not 50 independent trials whose failure probability compounds
downward — they are 50 instances of the same certain loss. On a loaded runner where the round trip
crosses 10ms, this is a deterministic failure wearing a flake's clothes.

That is the general lesson: a retry count only helps when attempts are independent. Here the
condition that makes an attempt fail is the same condition that persists across all of them, so
raising the count to 500 would change nothing.

Fix

The steal takes the holder's own claimMu for its read-then-write instead of trying to outrun the
ticker. claimMu already exists and is already held across the holder's entire read-assert-store
round trip — it is what stops a holder's two concurrent renewals from defeating each other — so
taking it in the test helper makes the steal's HEAD and PUT atomic with respect to renewals for
exactly the same reason.

No production code changes. The mechanism was already there; the test was reaching past it.

It is also the more faithful model. A real contender takes over a lease precisely when the holder is
not mid-renewal, because a renewal in flight is what makes the lease un-takeable. Racing a live
renewal is the one case where a takeover is supposed to lose.

Verified by mutation

CI's timing is not reproducible on demand, so the reproduction is constructed: setting
RenewInterval to 1ms puts the ticker inside the measured round trip, which is CI's condition
expressed locally.

RenewInterval = 1ms
claimMu hold removed all three steal-inside-an-action tests fail, with CI's exact message, deterministically
claimMu hold present 20 consecutive runs pass

A local run at the committed intervals proves nothing either way, which is why the helper's doc
comment records the hostile setting rather than a passing local run.

Also verified: 25 consecutive -count=25 -race runs of the whole internal/coord suite pass.

Activity

  1. added
    type: testing
    priority: medium
    area: distributed
    area: tests
    on Aug 8, 2026
  2. scttfrdmn commented on Aug 8, 2026

    @scttfrdmn
    OwnerAuthor

    Fixed in 1c8a90f.

    The steal now takes the holder's own claimMu for its read-then-write rather than trying to outrun the renewal ticker — that lock already serializes the holder's whole read-assert-store round trip, so taking it makes the test's HEAD and PUT atomic with respect to renewals for the same reason. No production code changed; the mechanism was already there and the test was reaching past it.

    Verified by mutation, not by argument: setting RenewInterval to 1ms puts the ticker inside the measured ~4ms round trip, which is CI's condition expressed locally. With the claimMu hold removed, all three steal-inside-an-action tests fail deterministically with the same "could not steal … in 50 attempts" message CI produced. With it restored at the same 1ms interval, 20 consecutive runs pass. The hostile setting is recorded in the doc comment, because a local pass at the committed intervals is evidence of nothing either way.

  3. added a commit that references this issue on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: distributedarea: testspriority: mediumtype: testing

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions