lease: free a refused snapshot's pipe; WAL tests wait for close
Fixes #1193. TestDrainReleaseSnapshotEarlyRefusalReleasesTheWAL waited 2 s wall-clock for app.db-wal to close and failed once on a busy CI runner. Cause: the HA fence gate refused a Litestream snapshot write without closing its pipe, so the snapshot goroutine could hold the WAL open (#811); our own snapshot goroutine can also close it late. The gate now closes the pipe body of every write it handles, and both WAL tests wait on the real descriptor count with a 30 s guard that only a genuine leak reaches. Reviewed: Astra READY, no findings; census APPROVE on #280. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from fix-1193-wal-close-test into main.
9 files changed,
+124
−52
docs/superpowers/notes/census-proposal-fix-1193-wal-close-test.md+11 −0internal/lease/claimgeneration_snapshot_linux_test.go+9 −32internal/lease/descriptors_test.go+48 −0internal/lease/export_test.go+3 −0internal/lease/fencegate.go+9 −0internal/lease/fencegate_test.go+32 −0internal/lease/release_snapshot_test.go+8 −20internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-fix-1193-wal-close-test.tsv+3 −0
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-fix-1193-wal-close-test.md b/docs/superpowers/notes/census-proposal-fix-1193-wal-close-test.md| new file mode 100644 |
| index 000000000..5ac7ac68b |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-fix-1193-wal-close-test.md |
| @@ -0,0 +1,11 @@ |
| +# Census approval: fix-1193-wal-close-test |
| + |
| +Ruling `census-ruling-fix-1193-wal-close-test`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +Envelopes re-pinned: `internal/lease`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Seal: `7ca068c5c146a679b4726d74fe43cf9c47f97080f9c08b761a36a9a12a5a3c54` → `7ca068c5c146a679b4726d74fe43cf9c47f97080f9c08b761a36a9a12a5a3c54`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-fix-1193-wal-close-test.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/lease/claimgeneration_snapshot_linux_test.go b/internal/lease/claimgeneration_snapshot_linux_test.go| index b3fea7c10..3d826ad3c 100644 |
| --- a/internal/lease/claimgeneration_snapshot_linux_test.go |
| +++ b/internal/lease/claimgeneration_snapshot_linux_test.go |
| @@ -4,10 +4,8 @@ import ( |
| "context" |
| "errors" |
| "io" |
| - "os" |
| "path/filepath" |
| "testing" |
| - "time" |
| "github.com/benbjohnson/litestream" |
| lsfile "github.com/benbjohnson/litestream/file" |
| @@ -27,44 +25,23 @@ func TestGenerationRefusedSnapshotReleasesWAL(t *testing.T) { |
| if err := db.SyncAndWait(t.Context()); err != nil { |
| t.Fatal(err) |
| } |
| - countWAL := func() int { |
| - t.Helper() |
| - fds, err := os.ReadDir("/proc/self/fd") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - count := 0 |
| - for _, fd := range fds { |
| - name, err := os.Readlink(filepath.Join("/proc/self/fd", fd.Name())) |
| - if err == nil && name == path+"-wal" { |
| - count++ |
| - } |
| - } |
| - return count |
| - } |
| - baseline := countWAL() |
| + wal := path + "-wal" |
| + baseline := openDescriptors(t, wal) |
| pos, reader, err := db.SnapshotReader(t.Context()) |
| if err != nil { |
| t.Fatal(err) |
| } |
| pipe := reader.(*io.PipeReader) |
| t.Cleanup(func() { pipe.Close() }) |
| - deadline := time.Now().Add(time.Second) |
| - for countWAL() == baseline && time.Now().Before(deadline) { |
| - time.Sleep(time.Millisecond) |
| - } |
| - if countWAL() <= baseline { |
| - t.Fatal("snapshot did not hold its WAL descriptor") |
| - } |
| + // The encoder (litestream's goroutine) opens the WAL on its own |
| + // schedule, and later closes it as it returns: the waits are for |
| + // the descriptor itself, and only a real bug meets their guard (#1193). |
| + waitDescriptors(t, wal, "the snapshot did not hold its WAL descriptor", |
| + func(open int) bool { return open > baseline }) |
| client := &generationClient{client: db.Replica.Client, check: func(context.Context) error { return ErrLost }} |
| if _, err := client.WriteLTXFile(t.Context(), litestream.SnapshotLevel, 1, pos.TXID, reader); !errors.Is(err, ErrLost) { |
| t.Fatal(err) |
| } |
| - deadline = time.Now().Add(time.Second) |
| - for countWAL() > baseline && time.Now().Before(deadline) { |
| - time.Sleep(time.Millisecond) |
| - } |
| - if got := countWAL(); got != baseline { |
| - t.Fatalf("refused snapshot retained WAL descriptors: got %d, want %d", got, baseline) |
| - } |
| + waitDescriptors(t, wal, "the refused snapshot retained its WAL descriptor", |
| + func(open int) bool { return open <= baseline }) |
| } |
diff --git a/internal/lease/descriptors_test.go b/internal/lease/descriptors_test.go| new file mode 100644 |
| index 000000000..34afce5c6 |
| --- /dev/null |
| +++ b/internal/lease/descriptors_test.go |
| @@ -0,0 +1,48 @@ |
| +package lease |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// descriptorGuard bounds waitDescriptors. Only a real bug (a descriptor |
| +// that never opens or never closes, such as a snapshot encoder blocked |
| +// on a pipe nobody closed) takes this long. |
| +const descriptorGuard = 30 * time.Second |
| + |
| +// waitDescriptors waits until done accepts the number of this process's |
| +// open descriptors on path (Linux: /proc/self/fd), and returns it. A |
| +// litestream snapshot encoder is a goroutine nothing can join: it opens |
| +// and closes the WAL on its own schedule, which a loaded machine can |
| +// stretch, so its tests wait for the descriptor itself, not a short |
| +// window (#810, #1193). The guard fails the test, naming what. |
| +func waitDescriptors(t testing.TB, path, what string, done func(open int) bool) int { |
| + t.Helper() |
| + for guard := time.Now().Add(descriptorGuard); ; time.Sleep(10 * time.Millisecond) { |
| + n := openDescriptors(t, path) |
| + if done(n) { |
| + return n |
| + } |
| + if time.Now().After(guard) { |
| + t.Fatalf("%s: %d descriptors on %s after %s", what, n, path, descriptorGuard) |
| + } |
| + } |
| +} |
| + |
| +// openDescriptors counts this process's open descriptors on path. |
| +func openDescriptors(t testing.TB, path string) int { |
| + t.Helper() |
| + fds, err := os.ReadDir("/proc/self/fd") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + n := 0 |
| + for _, fd := range fds { |
| + if target, err := os.Readlink(filepath.Join("/proc/self/fd", fd.Name())); err == nil && target == path { |
| + n++ |
| + } |
| + } |
| + return n |
| +} |
diff --git a/internal/lease/export_test.go b/internal/lease/export_test.go| index c0e8613c1..830000043 100644 |
| --- a/internal/lease/export_test.go |
| +++ b/internal/lease/export_test.go |
| @@ -19,6 +19,9 @@ var ( |
| LockHeldForTest = lockHeld |
| LeaseFreeForTest = leaseFree |
| AcquireHATTL = acquireHATTL |
| + // WaitDescriptorsForTest waits on this process's open descriptors on |
| + // a path, with a guard that fires only on a real bug. |
| + WaitDescriptorsForTest = waitDescriptors |
| ) |
| // ResetForTest is a fresh process's lease state: no self, no obligation |
diff --git a/internal/lease/fencegate.go b/internal/lease/fencegate.go| index a3b3daf38..80082bd96 100644 |
| --- a/internal/lease/fencegate.go |
| +++ b/internal/lease/fencegate.go |
| @@ -117,7 +117,16 @@ func (g *fenceGate) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID |
| return &gatedReader{ReadCloser: rd, release: release}, nil |
| } |
| +// WriteLTXFile closes a pipe body on every outcome, as litestream's |
| +// compactor and generationClient do. litestream's DB.Snapshot never |
| +// closes its reader when the write fails (#811): a store monitor's |
| +// snapshot refused here, after the release or the fence closed the gate, |
| +// would leave its encoder blocked on the pipe, holding the WAL open for |
| +// the life of the process (#1193). |
| func (g *fenceGate) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { |
| + if pipe, ok := r.(*io.PipeReader); ok { |
| + defer pipe.Close() |
| + } |
| ctx, release, err := g.admit(ctx) |
| if err != nil { |
| return nil, err |
diff --git a/internal/lease/fencegate_test.go b/internal/lease/fencegate_test.go| index c2e8bd324..42ef7a730 100644 |
| --- a/internal/lease/fencegate_test.go |
| +++ b/internal/lease/fencegate_test.go |
| @@ -141,3 +141,35 @@ func TestFenceGateRefusesAndCancels(t *testing.T) { |
| t.Fatal("finalizing does not read the tag") |
| } |
| } |
| + |
| +// A write the gate refuses, or cancels at its close, releases a pipe |
| +// body it never read: litestream's DB.Snapshot leaves that reader open, |
| +// and its encoder, blocked on the pipe, would hold the WAL open for good |
| +// (#811, #1193). |
| +func TestFenceGateReleasesAnUnreadPipeBody(t *testing.T) { |
| + inner := &gateInner{entered: make(chan struct{}, 1)} |
| + g := newFenceGate(inner) |
| + write := func() <-chan error { |
| + pr, pw := io.Pipe() |
| + encoder := make(chan error, 1) |
| + go func() { _, err := pw.Write([]byte("ltx")); encoder <- err }() |
| + go func() { _, _ = g.WriteLTXFile(context.Background(), litestream.SnapshotLevel, 1, 1, pr) }() |
| + return encoder |
| + } |
| + released := func(what string, encoder <-chan error) { |
| + t.Helper() |
| + select { |
| + case err := <-encoder: |
| + if !errors.Is(err, io.ErrClosedPipe) { |
| + t.Fatalf("%s: the encoder's write ended with %v, want io.ErrClosedPipe", what, err) |
| + } |
| + case <-time.After(30 * time.Second): // fires only on a leak |
| + t.Fatalf("%s: the encoder is still blocked on the pipe", what) |
| + } |
| + } |
| + inFlight := write() |
| + <-inner.entered |
| + g.close() |
| + released("cancelled at close", inFlight) |
| + released("refused after close", write()) |
| +} |
diff --git a/internal/lease/release_snapshot_test.go b/internal/lease/release_snapshot_test.go| index 7af076170..49366b074 100644 |
| --- a/internal/lease/release_snapshot_test.go |
| +++ b/internal/lease/release_snapshot_test.go |
| @@ -5,12 +5,10 @@ import ( |
| "database/sql" |
| "errors" |
| "io" |
| - "os" |
| "path/filepath" |
| "runtime" |
| "sync" |
| "testing" |
| - "time" |
| "github.com/benbjohnson/litestream" |
| lsfile "github.com/benbjohnson/litestream/file" |
| @@ -250,24 +248,14 @@ func TestDrainReleaseSnapshotEarlyRefusalReleasesTheWAL(t *testing.T) { |
| if m := lockView(t, fx, "instances/h").Lock.Release; m == nil || !m.Clean || m.TXID != txid { |
| t.Fatalf("marker %+v, want clean at %d", m, txid) |
| } |
| - wal := path + "-wal" |
| - openWAL := func() bool { |
| - ents, err := os.ReadDir("/proc/self/fd") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - for _, e := range ents { |
| - if target, err := os.Readlink(filepath.Join("/proc/self/fd", e.Name())); err == nil && target == wal { |
| - return true |
| - } |
| - } |
| - return false |
| - } |
| - for deadline := time.Now().Add(2 * time.Second); openWAL(); time.Sleep(10 * time.Millisecond) { |
| - if time.Now().After(deadline) { |
| - t.Fatalf("%s is still open after the release: the snapshot encoder was not released", wal) |
| - } |
| - } |
| + // The encoder is litestream's goroutine (DB.SnapshotReader) and |
| + // nothing can join it: once its pipe is closed, its next write fails |
| + // and it closes the WAL as it returns, some time after the release. |
| + // So the check waits for the descriptor itself to close (#1193; a 2 s |
| + // window failed twice, #810). An encoder blocked on a pipe nobody |
| + // closed never closes it, and the helper's guard fails the test. |
| + lease.WaitDescriptorsForTest(t, path+"-wal", "the snapshot encoder was not released", |
| + func(open int) bool { return open == 0 }) |
| out, _ := restoreCounting(t, dir) |
| if got, want := dbRows(t, out), dbRows(t, path); got != want { |
| t.Fatalf("restored %q, want %q", got, want) |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index f4253ce83..542f75862 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -240,3 +240,4 @@ plan14-root-reads-audit-aws-timeouts.tsv c3e74a3069651a7ae638b2ecba5e38c4dbaec28 |
| plan14-root-reads-config-receipts-1.tsv 4abde13ec40d53a479684b7f1e2079fbedb6c40cb1a28819c97853cdab768b2f |
| plan14-root-reads-audit-edge-test-sleeps.tsv 4abde13ec40d53a479684b7f1e2079fbedb6c40cb1a28819c97853cdab768b2f |
| plan14-root-reads-ha-generations-floor.tsv 7ca068c5c146a679b4726d74fe43cf9c47f97080f9c08b761a36a9a12a5a3c54 |
| +plan14-root-reads-fix-1193-wal-close-test.tsv 7ca068c5c146a679b4726d74fe43cf9c47f97080f9c08b761a36a9a12a5a3c54 |
diff --git a/internal/provision/testdata/plan14-root-reads-fix-1193-wal-close-test.tsv b/internal/provision/testdata/plan14-root-reads-fix-1193-wal-close-test.tsv| new file mode 100644 |
| index 000000000..bfb93f6c0 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-fix-1193-wal-close-test.tsv |
| @@ -0,0 +1,3 @@ |
| +# Census overlay fix-1193-wal-close-test, written by census-propose (#369). census-ruling-fix-1193-wal-close-test, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-fix-1193-wal-close-test.md. |
| +# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@coverage internal/lease 39394174608cbf40e565d37ee490ce4dfe3838152cbbb655d99deae4b2e27827 |