diff --git a/seaweed-block/core/authority/workspace_lease_refusal_race_test.go b/seaweed-block/core/authority/workspace_lease_refusal_race_test.go new file mode 100644 index 0000000000..295989d9cd --- /dev/null +++ b/seaweed-block/core/authority/workspace_lease_refusal_race_test.go @@ -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: + } +} diff --git a/seaweed-block/core/authority/workspace_lease_view.go b/seaweed-block/core/authority/workspace_lease_view.go index bed676e893..6cfc82d8a5 100644 --- a/seaweed-block/core/authority/workspace_lease_view.go +++ b/seaweed-block/core/authority/workspace_lease_view.go @@ -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