authority: read the lease view under leaseMu in the renewal refusal (#242)

The no-host-validity refusal in RenewWorkspaceLease formatted its error
from lv.d.horizon after releasing leaseMu, while the extender
(refreshLeaseViewLocked) rewrites the view under leaseMu: a data race.
The horizon is now read before the unlock.

Test: TestWorkspaceLeaseRefusalReadsTheViewUnderTheLock drives the
refusal while the view is republished concurrently; under -race it fails
on main (workspace_lease_view.go:174) and passes here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-18 23:00:20 -08:00
1 parent 367617c14b
commit 01a118036f
2 files changed
+63 -1

No files matched your search

@@ -0,0 +1,61 @@
package authority
import (
"context"
"strings"
"sync"
"testing"
"time"
)
// #242: a renewal refused for lack of host validity formats its error from
// the lease view; that read must happen under leaseMu, because the extender
// (refreshLeaseViewLocked) rewrites the view under leaseMu at any time.
// Run with -race: renewals that take the refusal run while another goroutine
// rewrites the view, as the extender does.
func TestWorkspaceLeaseRefusalReadsTheViewUnderTheLock(t *testing.T) {
disk, _ := newTempStore(t)
c := newTestClock()
p := newLeasePublisher(t, disk, 3*time.Second, c)
bindAndGrant(t, p, "v", "r")
// Force the no-host-validity refusal: the horizon just ahead, a skew
// bound far larger than what is left of it; the granted deadline later.
p.leaseMu.Lock()
lv := p.leaseViews["v"]
lv.d.horizon = c.now().Add(time.Millisecond).UnixNano()
lv.d.maxSkewNS = int64(time.Hour)
lv.deadline = c.now().Add(time.Hour)
p.leaseMu.Unlock()
const rounds = 200
var wg sync.WaitGroup
wg.Add(2)
go func() { // the extender's republish: a write of the view under leaseMu
defer wg.Done()
for i := 0; i < rounds; i++ {
p.leaseMu.Lock()
d := lv.d
lv.d = d
p.leaseMu.Unlock()
}
}()
errs := make(chan error, 1)
go func() {
defer wg.Done()
for i := 0; i < rounds; i++ {
if _, err := p.RenewWorkspaceLease(context.Background(), "v", "r", 1); err == nil || !strings.Contains(err.Error(), "leaves no host validity") {
select {
case errs <- err:
default:
}
return
}
}
}()
wg.Wait()
select {
case err := <-errs:
t.Fatalf("renewal with no host validity: %v; want the refusal", err)
default:
}
}
@@ -170,8 +170,9 @@ func (p *Publisher) RenewWorkspaceLease(ctx context.Context, volume, holder stri
// bound, leaseFact) would make the host fence on an invalid reply and lose
// its remaining validity; refuse with an error instead.
if grant-lv.d.maxSkewNS/2 <= now.UnixNano() {
horizon := lv.d.horizon // the view is leaseMu's: read it before the unlock (#242)
p.leaseMu.Unlock()
return WorkspaceLease{}, fmt.Errorf("workspace lease: durable horizon %s leaves no host validity", time.Unix(0, lv.d.horizon).UTC().Format(time.RFC3339Nano))
return WorkspaceLease{}, fmt.Errorf("workspace lease: durable horizon %s leaves no host validity", time.Unix(0, horizon).UTC().Format(time.RFC3339Nano))
}
if g := time.Unix(0, grant); g.After(lv.deadline) {
lv.deadline = g