carlos / platform Public

lease: the early-refusal fake refuses only the drain's snapshot (#810)

Arming the refusal after the store opened was not enough: on CI the
store monitor's Open-time snapshot can still land after it, be refused,
and leak litestream's reader and the WAL (DB.Snapshot does not close
its reader when an upload fails; #811). The test now marks the drain
release's context, and the fake refuses only level-9 writes carrying
that mark. The monitor runs on the store's own context, so its
snapshots are never refused. 30 targeted and 3 full-package runs pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev ccb9cc68c7b6d348661ca3a13e35867644e3aaa2 parent 1e4d397
1 file changed, +14 −11
  • internal/lease/release_snapshot_test.go +14 −11

CI passed — run details

Intent

Human

<paseo-system version="1" kind="agent-message" source-agent-id="8a3d302a-eac7-407c-b61c-f5ccc66b5544" message-id="6da31648-2208-429f-8f67-489aba94c4ce"> Agent manager 8a3d302: excellent. Merge on green and send the SHA. Also file the side note (a failed monitor snapshot can leak a goroutine and the WAL fd until restart when the replica client isn&apos;t the generation-bound wrapper) as a discussion with the fix idea, so it isn&apos;t lost. </paseo-system>

Attributed to this commit: 75.6 g CO₂e (9,911,154 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on fix-wal-test-810, 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/release_snapshot_test.go b/internal/lease/release_snapshot_test.go
index 4c6e431d9..3b2cafd61 100644
--- a/internal/lease/release_snapshot_test.go
+++ b/internal/lease/release_snapshot_test.go
@@ -29,6 +29,10 @@ import (
// countingReplica is a replica client that counts the LTX files opened
// (a restore's reads, one GET each on S3) and records the levels written,
// and can refuse snapshot writes.
+// drainSnapshotOnly marks the context of the drain release's own snapshot,
+// so a refusing fake can refuse that snapshot and no other.
+type drainSnapshotOnly struct{}
+
type countingReplica struct {
litestream.ReplicaClient
mu sync.Mutex
@@ -52,7 +56,12 @@ 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
+ // Only the drain's own snapshot is refused: the test marks its context
+ // (drainSnapshotOnly). The store monitor's snapshots run on the store's
+ // context and are never refused, so litestream's DB.Snapshot, which
+ // leaves its reader and the WAL open when an upload fails (#810, #811),
+ // never runs into this fake's refusal.
+ early := c.refuseEarly && level == litestream.SnapshotLevel && ctx.Value(drainSnapshotOnly{}) != nil
if level == litestream.SnapshotLevel {
c.snapshotTXID = maxTXID
}
@@ -228,17 +237,11 @@ func TestDrainReleaseSnapshotEarlyRefusalReleasesTheWAL(t *testing.T) {
haBox(t)
fx := leasetest.NewLockFixture(t)
dir := t.TempDir()
- rc := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(dir)}
+ rc := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(dir), refuseEarly: true}
r, path := manyL0(t, fx, "instances/h", rc, 5)
- // Armed only now, for the drain's own snapshot (#810). Armed from the
- // start, it also refused the store monitor's Open-time snapshot, and
- // litestream's DB.Snapshot leaves its reader, and with it the WAL, open
- // when an upload fails and the client does not close the body (this
- // fake does not). That leak was the fixture's, not the drain release's.
- rc.mu.Lock()
- rc.refuseEarly = true
- rc.mu.Unlock()
- txid, snapErr, err := r.FinishSnapshot(ctx)
+ // The refusal applies to the drain's own snapshot only (#810): its
+ // context carries drainSnapshotOnly, the store monitor's does not.
+ txid, snapErr, err := r.FinishSnapshot(context.WithValue(ctx, drainSnapshotOnly{}, true))
if err != nil || snapErr == nil {
t.Fatalf("FinishSnapshot: txid %d, snapshot %v, %v; want the snapshot's failure alone", txid, snapErr, err)
}