carlos / platform Public

lease: close the snapshot reader on every outcome

Astra round 1 on held-move-speed, F1.3 (P2). litestream's DB.Snapshot
leaves its SnapshotReader pipe open when the upload fails without
draining it; the encoder, blocked writing to the pipe, then keeps the
source database's WAL open for good.

The release's snapshot now calls SnapshotReader and WriteLTXFile itself
and closes the pipe reader on every outcome, as generationClient's
WriteLTXFile does (claimgeneration.go). The only thing skipped from
DB.Snapshot is its cache of the level-9 file info, unused since the
store closes right after.

Witness: a replica that refuses the snapshot at once, reading none of
it; the release is clean, the restore matches, and no descriptor for
the WAL is left in /proc/self/fd (linux only). With DB.Snapshot back it
fails: the WAL stays open.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev f9fd712d83009f94f01ccbfeb2576a089c1e6e38 parent f425885
2 files changed, +74 −1
  • internal/lease/release.go +18 −1
  • internal/lease/release_snapshot_test.go +56 −0
diff --git a/internal/lease/release.go b/internal/lease/release.go
index 59a2735f4..0ed3af44f 100644
--- a/internal/lease/release.go
+++ b/internal/lease/release.go
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
+ "io"
"net/http"
"time"
@@ -20,13 +21,29 @@ import (
// snapshotWithin writes the release's snapshot within half of what is left
// of ctx's budget, so a slow one never leaves the store's close (its own
// final sync, which decides the release's clean flag) a spent context.
+//
+// It is litestream's DB.Snapshot (v0.5.14 db.go) with one difference: it
+// owns the snapshot reader and closes it on every outcome. The reader is
+// a pipe whose encoder holds the WAL open; DB.Snapshot leaves it open when
+// the upload fails without draining it, and the encoder, blocked on the
+// pipe, then keeps the WAL descriptor for good (generationClient's
+// WriteLTXFile closes it the same way, claimgeneration.go). Skipping
+// DB.Snapshot's update of the DB's cached level-9 info is harmless: the
+// store closes right after.
func (r *Replicator) snapshotWithin(ctx context.Context) (*ltx.FileInfo, error) {
if d, ok := ctx.Deadline(); ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Until(d)/2)
defer cancel()
}
- return r.db.Snapshot(ctx)
+ pos, rd, err := r.db.SnapshotReader(ctx)
+ if err != nil {
+ return nil, err
+ }
+ if c, ok := rd.(io.Closer); ok {
+ defer c.Close()
+ }
+ return r.db.Replica.Client.WriteLTXFile(ctx, litestream.SnapshotLevel, 1, pos.TXID, rd)
}
// releaseWithMarker is the one HA release. lease never deletes an HA lock:
diff --git a/internal/lease/release_snapshot_test.go b/internal/lease/release_snapshot_test.go
index 55eae69ef..a0228bc3c 100644
--- a/internal/lease/release_snapshot_test.go
+++ b/internal/lease/release_snapshot_test.go
@@ -5,9 +5,12 @@ import (
"database/sql"
"errors"
"io"
+ "os"
"path/filepath"
+ "runtime"
"sync"
"testing"
+ "time"
"github.com/benbjohnson/litestream"
lsfile "github.com/benbjohnson/litestream/file"
@@ -33,6 +36,9 @@ type countingReplica struct {
writes []int
snapshotTXID ltx.TXID
failSnapshot bool
+ // refuseEarly refuses a snapshot without reading any of it, as an
+ // upload that fails before it consumes the body.
+ refuseEarly bool
}
func (c *countingReplica) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) {
@@ -46,10 +52,14 @@ func (c *countingReplica) WriteLTXFile(ctx context.Context, level int, minTXID,
c.mu.Lock()
c.writes = append(c.writes, level)
fail := c.failSnapshot && level == litestream.SnapshotLevel
+ early := c.refuseEarly && level == litestream.SnapshotLevel
if level == litestream.SnapshotLevel {
c.snapshotTXID = maxTXID
}
c.mu.Unlock()
+ if early {
+ return nil, errors.New("the store refused the snapshot at once")
+ }
if fail {
io.Copy(io.Discard, r)
return nil, errors.New("the store refused the snapshot")
@@ -207,3 +217,49 @@ func TestDrainReleaseSnapshotFailureStaysClean(t *testing.T) {
t.Fatalf("restored %q, want %q", got, want)
}
}
+
+// F1.3 (Astra r1): a snapshot refused before any of it is read still
+// releases the snapshot encoder and its WAL descriptor; the release is
+// clean and the restore correct, as for any failed snapshot.
+func TestDrainReleaseSnapshotEarlyRefusalReleasesTheWAL(t *testing.T) {
+ if runtime.GOOS != "linux" {
+ t.Skip("reads /proc/self/fd")
+ }
+ haBox(t)
+ fx := leasetest.NewLockFixture(t)
+ dir := t.TempDir()
+ rc := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(dir), refuseEarly: true}
+ r, path := manyL0(t, fx, "instances/h", rc, 5)
+ txid, snapErr, err := r.FinishSnapshot(ctx)
+ if err != nil || snapErr == nil {
+ t.Fatalf("FinishSnapshot: txid %d, snapshot %v, %v; want the snapshot's failure alone", txid, snapErr, err)
+ }
+ if err := r.ReleaseMarker(ctx, hafresh.Marker{V: 1, Clean: true}); err != nil {
+ t.Fatal(err)
+ }
+ 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)
+ }
+ }
+ out, _ := restoreCounting(t, dir)
+ if got, want := dbRows(t, out), dbRows(t, path); got != want {
+ t.Fatalf("restored %q, want %q", got, want)
+ }
+}