test(workspace): reservation persist hook and RED for renewal behind a held lifecycle write (#205)

Add DeletionReservationStore.SetPersistHookForTest, an instance-local
test seam that installs the existing write-fault hook under the store
mutex (nil by default).

r5: while the deletion-reservation write of an unrelated volume is held
in that hook, a renewal through the services layer must return within
2 s. On main it waits on the reservation store's mutex.

r4 and r5 now release held writes first and settle every worker with
bounded waits before clearing hooks or closing fixtures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-18 04:42:15 -08:00
1 parent c5192fc4c1
commit 1193cb751b
3 files changed
+93 -3

No files matched your search

@@ -154,12 +154,13 @@ func TestWorkspaceRenewalDoesNotWaitForUnrelatedHeldWrite(t *testing.T) {
release := func() { releaseOnce.Do(func() { close(held.release) }) }
revoked := make(chan error, 1)
go func() { revoked <- p.BeginWorkspaceRevocation(context.Background(), "a", "r", 1) }()
// Bounded cleanup: release the held write first, then settle the worker.
t.Cleanup(func() {
release()
select {
case <-revoked:
case <-time.After(5 * time.Second):
t.Error("held revocation of a did not settle")
t.Error("held revocation of a did not settle within 5 s after release")
}
})
select {
@@ -180,8 +181,13 @@ func TestWorkspaceRenewalDoesNotWaitForUnrelatedHeldWrite(t *testing.T) {
t.Fatalf("renewal of b: %v", err)
}
case <-time.After(2 * time.Second):
blocked := time.Since(start).Round(time.Millisecond)
release()
err := <-renewed
t.Fatalf("renewal of b was still blocked %s behind the held store write of a (it returned only after a was released, err=%v)", time.Since(start).Round(time.Millisecond), err)
select {
case err := <-renewed:
t.Fatalf("renewal of b was still blocked %s behind the held store write of a (it returned only after a was released, err=%v)", blocked, err)
case <-time.After(5 * time.Second):
t.Fatalf("renewal of b was still blocked %s behind the held store write of a, and did not settle within 5 s after release", blocked)
}
}
}
@@ -116,3 +116,75 @@ func TestWorkspaceLeaseGuardSurvivesSlowAuthorityStore(t *testing.T) {
time.Sleep(50 * time.Millisecond)
}
}
// A durable deletion-reservation write of an unrelated volume must not block
// a lease renewal: the renewal RPC checks Deletions.Reserved and the volume
// record, and both lifecycle stores hold their mutex across file I/O
// (issue #205).
func TestWorkspaceRenewalRPCDoesNotWaitForHeldReservationWrite(t *testing.T) {
h, c, ctx := reviewBindingHost(t)
reviewLegacyBind(t, h, c, ctx, "vol-b")
line := waitAuthorityLine(t, h.Publisher(), "vol-b")
svc := &services{host: h}
id := &control.WorkspaceIdentity{VolumeId: "vol-b", ReplicaId: line.ReplicaID, Epoch: line.Epoch}
if _, err := svc.RenewWorkspaceLease(ctx, id); err != nil {
t.Fatal(err) // first grant
}
deletions := h.Lifecycle().Deletions
entered, release := make(chan struct{}), make(chan struct{})
var enterOnce, releaseOnce sync.Once
releaseHook := func() { releaseOnce.Do(func() { close(release) }) }
deletions.SetPersistHookForTest(func() error {
enterOnce.Do(func() { close(entered) })
<-release
return nil
})
admitted := make(chan error, 1)
go func() {
_, err := deletions.Admit("vol-x", "tok") // an unrelated volume, a real durable write
admitted <- err
}()
renewed := make(chan error, 1)
renewStarted := false
// Bounded cleanup: release the hook first, settle BOTH workers, and only
// then clear the hook. Never call the setter after a timed-out join: the
// store mutex may still be held.
t.Cleanup(func() {
releaseHook()
settled := true
select {
case <-admitted:
case <-time.After(5 * time.Second):
settled = false
t.Error("held reservation write did not settle within 5 s after release")
}
if renewStarted {
select {
case <-renewed:
case <-time.After(5 * time.Second):
settled = false
t.Error("renewal did not settle within 5 s after release")
}
}
if settled {
deletions.SetPersistHookForTest(nil)
}
})
select {
case <-entered:
case <-time.After(3 * time.Second):
t.Fatal("reservation write never reached the store")
}
start := time.Now()
renewStarted = true
go func() { _, err := svc.RenewWorkspaceLease(ctx, id); renewed <- err }()
select {
case err := <-renewed:
renewed <- err // keep it for the cleanup join
if err != nil {
t.Fatalf("renewal of vol-b: %v", err)
}
case <-time.After(2 * time.Second):
t.Fatalf("renewal RPC of vol-b was still blocked %s behind the held reservation write of vol-x", time.Since(start).Round(time.Millisecond))
}
}
@@ -56,6 +56,18 @@ type DeletionReservationStore struct {
failWrite, failRename, failDirSync func() error
}
// SetPersistHookForTest installs (nil clears) a callback that each later
// reservation write of this store runs under the store mutex, after its
// temporary file is created and before the write. A non-nil error fails that
// write with nothing replaced. Tests only: install it before any concurrent
// writer starts; the callback must not call back into the store or into this
// setter. Never call it while a write may still be held by the callback.
func (s *DeletionReservationStore) SetPersistHookForTest(hook func() error) {
s.mu.Lock()
defer s.mu.Unlock()
s.failWrite = hook
}
// OpenDeletionReservationStore opens or creates the store in dir.
func OpenDeletionReservationStore(dir string) (*DeletionReservationStore, error) {
return openDeletionReservationStore(dir, syncBindingParent)