carlos / platform Public

lease: gate frees a refused snapshot's pipe; WAL test waits for close

Fixes the flake in TestDrainReleaseSnapshotEarlyRefusalReleasesTheWAL
(#1193). The WAL descriptor after a release is held by litestream's
snapshot encoder goroutine (DB.SnapshotReader), which nothing can join:
it closes the WAL in a defer after its last pipe write. Two ways it
outlives the release:

- The drain's own snapshot: snapshotWithin closes the pipe, and the
  encoder frees the WAL some time later. A loaded CI worker can stretch
  that past the test's old 2 s window (as in #810).
- A store monitor snapshot that straddles the release: once finalize
  closes the fence gate, the gate refused the monitor's WriteLTXFile
  without touching the body, and litestream's DB.Snapshot never closes
  its reader on a failed write (#811). That encoder blocks on the pipe
  and holds the WAL for the life of the process.

The gate's WriteLTXFile now closes a pipe body on every outcome, as
litestream's compactor and generationClient already do. The test waits
for the descriptor itself to close, with a 30 s guard that fires only
on a leak, instead of a 2 s deadline. A new gate test checks a refused
and a cancelled write both release the pipe's writer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 96aee3fb45522e87eedc4c1a5470dce153b91594 parent 03ff5d1
3 files changed, +51 −3
  • internal/lease/fencegate.go +9 −0
  • internal/lease/fencegate_test.go +32 −0
  • internal/lease/release_snapshot_test.go +10 −3

Intent

Attributed to this commit: 5.02 g CO₂e (656,045 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on fix-1193-wal-close-test, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

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..8483da60c 100644
--- a/internal/lease/release_snapshot_test.go
+++ b/internal/lease/release_snapshot_test.go
@@ -263,9 +263,16 @@ func TestDrainReleaseSnapshotEarlyRefusalReleasesTheWAL(t *testing.T) {
}
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, however
+ // long a loaded machine takes to run the encoder (#1193; a 2 s window
+ // failed twice, #810). An encoder still blocked on a pipe nobody
+ // closed never gets there: the 30 s guard fires only on that leak.
+ for guard := time.Now().Add(30 * time.Second); openWAL(); time.Sleep(10 * time.Millisecond) {
+ if time.Now().After(guard) {
+ t.Fatalf("%s is still open 30 s after the release: the snapshot encoder was not released", wal)
}
}
out, _ := restoreCounting(t, dir)