roster: signal a finished refresh only after releasing the cache lock
The refresh flight closed f.done while still holding c.mu and unlocked after, so a waiter woken by the close (Refresh, or a Check waiting on the flight) that asked Lease.Valid or Remaining at once could meet the lock; both use TryLock and read contention as not valid, so a lease the roster still confirmed was reported invalid and an app answered a spurious retryable 503. It surfaced as TestLeaseSurvivesAnotherMembersChange failing once under -race on hub CI. The flight now unlocks, then closes; f.err is written before the close, so the receive still sees it. A test-only signalled seam parks the closer just after the close: TestRefreshWakesItsWaiterWithTheCacheUnlocked passes on the fix and fails every run with the old order; the first decision in the flaky test is now asserted Member. Reviewed by Claude Fable 5.1 (claude-fable-5-1), oficina/home #104, APPROVED at 12b19ea (diagnosis made causal by a sleep mutant reproducing the CI line); CI green at 7db3832. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from roster-flight-unlock into main.
2 files changed,
+37
−1
roster/cache.go+13 −1roster/lease_test.go+24 −0
CI passed — run details
diff --git a/roster/cache.go b/roster/cache.go| index 33659b7..2f52e9f 100644 |
| --- a/roster/cache.go |
| +++ b/roster/cache.go |
| @@ -174,6 +174,10 @@ type Cache struct { |
| flight *flight |
| attempted bool |
| lastAttempt time.Duration |
| + // signalled, when set (tests only), runs in the flight goroutine just |
| + // after done is closed: a test parks the closer there to prove the lock |
| + // is already free when a waiter wakes. Read under mu with the result. |
| + signalled func() |
| } |
| func New(ctx context.Context, cfg Config) (*Cache, error) { |
| @@ -413,8 +417,16 @@ func (c *Cache) startRefresh(now time.Duration) *flight { |
| c.mu.Lock() |
| f.err = err |
| c.flight = nil |
| - close(f.done) |
| + signalled := c.signalled |
| c.mu.Unlock() |
| + // Signal only once the lock is free: a waiter that wakes on done and |
| + // asks Lease.Valid at once must not meet this goroutine's lock, which |
| + // TryLock reports as contention (Unknown). f.err is written before |
| + // the close, so the receive still sees it. |
| + close(f.done) |
| + if signalled != nil { |
| + signalled() |
| + } |
| }() |
| return f |
| } |
diff --git a/roster/lease_test.go b/roster/lease_test.go| index 57c9984..5c1e1e0 100644 |
| --- a/roster/lease_test.go |
| +++ b/roster/lease_test.go |
| @@ -300,6 +300,7 @@ func TestLeaseSurvivesAnotherMembersChange(t *testing.T) { |
| }) |
| c := f.cache() |
| d := f.check(c, 1) |
| + state(t, d, Member) |
| f.clock.step(30 * time.Second) |
| f.refreshTo(c, 2, []MemberEntry{{person(1), "owner"}, {person(3), "owner"}}) |
| if !d.Lease.Valid() { |
| @@ -307,6 +308,29 @@ func TestLeaseSurvivesAnotherMembersChange(t *testing.T) { |
| } |
| } |
| +// A waiter woken by a finished refresh finds the cache unlocked: the flight |
| +// releases c.mu before it closes done. The closer is parked just after the |
| +// close (the signalled seam), so a Lease.Valid asked by the woken waiter |
| +// meets the lock if, and only if, the close still happens under it, which |
| +// TryLock reads as contention and a valid lease as invalid (#104). |
| +func TestRefreshWakesItsWaiterWithTheCacheUnlocked(t *testing.T) { |
| + f := setup(t) |
| + c := f.cache() |
| + d := f.check(c, 1) |
| + state(t, d, Member) |
| + parked, release := make(chan struct{}), make(chan struct{}) |
| + c.mu.Lock() |
| + c.signalled = func() { close(parked); <-release } |
| + c.mu.Unlock() |
| + defer close(release) |
| + f.clock.step(30 * time.Second) |
| + f.refreshTo(c, 2, []MemberEntry{{person(1), "member"}}) |
| + <-parked |
| + if !d.Lease.Valid() { |
| + t.Fatal("the refresh woke its waiter with the cache still locked") |
| + } |
| +} |
| + |
| // A fence ends every earlier lease even when a later publication lists the |
| // subject unchanged: the deadline was measured on a clock the fence distrusts. |
| func TestLeaseEndedByFenceStaysEndedAfterPublication(t *testing.T) { |