roster: pin that every lock release publishes the view
Review on oficina/home #158 (Claude Fable) showed that replacing c.unlock() with a bare c.mu.Unlock() at any of the ten release sites left the suite green, and two of those are real holes: UpdateRecord's (a contended reader revives a lease after a record fence returned) and the uncontended lease read's (the same after a clock fence that read made). - TestLeaseContentionSeesRecordFence: fence by UpdateRecord, hold c.mu, a contended Valid is false. Fails with UpdateRecord's release bare. - TestLeaseContentionSeesClockFenceFromALeaseRead: a lease read fences on a backward wall step, the wall steps forward again so the contended clock check sees no net discrepancy, and only the published fence can refuse. Fails with the lease read's release bare. - TestEveryLockReleasePublishes: cache.go holds exactly one bare c.mu.Unlock(), inside unlock(). Fails with any of the other eight sites made bare.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+62
−0
roster/lease_test.go+62 −0
diff --git a/roster/lease_test.go b/roster/lease_test.go| index e34eaad..e8d5e50 100644 |
| --- a/roster/lease_test.go |
| +++ b/roster/lease_test.go |
| @@ -4,7 +4,9 @@ import ( |
| "context" |
| "database/sql" |
| "net/http" |
| + "os" |
| "path/filepath" |
| + "strings" |
| "sync" |
| "testing" |
| "time" |
| @@ -208,6 +210,66 @@ func TestLeaseContentionSeesPublishedRemoval(t *testing.T) { |
| } |
| } |
| +// A record update's fence is published when UpdateRecord returns: a reader |
| +// that then finds the lock held must not answer from the view before it. |
| +func TestLeaseContentionSeesRecordFence(t *testing.T) { |
| + f := setup(t) |
| + c := f.cache() |
| + d := f.check(c, 1) |
| + n := f.record |
| + n.Sequence = 2 |
| + n.Operation = "op2" |
| + if e := c.UpdateRecord(context.Background(), f.verify(n)); e != nil { |
| + t.Fatal(e) |
| + } |
| + c.mu.Lock() |
| + v, left := contended(t, c, d.Lease) |
| + c.mu.Unlock() |
| + if v || left != 0 { |
| + t.Fatal("contended reader revived a lease the record fence ended") |
| + } |
| +} |
| + |
| +// A clock fence made by a lease read is published too. The wall clock steps |
| +// back past the tolerance (the read fences), then forward again, so the |
| +// contended reader's own clock check against the last published observation |
| +// sees no net discrepancy and only the published fence can refuse it. |
| +func TestLeaseContentionSeesClockFenceFromALeaseRead(t *testing.T) { |
| + f := setup(t) |
| + c := f.cache() |
| + d := f.check(c, 1) |
| + f.clock.wallStep(-ClockTolerance - time.Nanosecond) |
| + if d.Lease.Valid() { |
| + t.Fatal("fence missed") |
| + } |
| + f.clock.wallStep(ClockTolerance + time.Nanosecond) |
| + c.mu.Lock() |
| + v, left := contended(t, c, d.Lease) |
| + c.mu.Unlock() |
| + if v || left != 0 { |
| + t.Fatal("contended reader revived a lease a clock fence ended") |
| + } |
| +} |
| + |
| +// Every release of c.mu must publish the view (unlock); a bare Unlock leaves |
| +// contended readers on a stale view. Two sites have tests above; this keeps |
| +// the rest honest: the one bare Unlock in cache.go is unlock's own. |
| +func TestEveryLockReleasePublishes(t *testing.T) { |
| + raw, e := os.ReadFile("cache.go") |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + src := string(raw) |
| + if n := strings.Count(src, "c.mu.Unlock()"); n != 1 { |
| + t.Fatalf("cache.go has %d bare c.mu.Unlock() calls; release the lock with c.unlock()", n) |
| + } |
| + start := strings.Index(src, "func (c *Cache) unlock() {") |
| + at := strings.Index(src, "c.mu.Unlock()") |
| + if start < 0 || at < start || strings.Contains(src[start:at], "\n}") { |
| + t.Fatal("the bare c.mu.Unlock() is not inside unlock()") |
| + } |
| +} |
| + |
| // The production shape of #175: many requests' Checks take the lock briefly |
| // while a stream's guard asks its lease. A live lease must never read as 0. |
| func TestLeaseNeverReadsZeroUnderConcurrentChecks(t *testing.T) { |