docs: review brief for flake-lease-teardown
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+157
−0
docs/superpowers/reviews/REVIEW-BRIEF-flake-lease-teardown.md+157 −0
CI cancelled — superseded by a newer push — run details
diff --git a/docs/superpowers/reviews/REVIEW-BRIEF-flake-lease-teardown.md b/docs/superpowers/reviews/REVIEW-BRIEF-flake-lease-teardown.md| new file mode 100644 |
| index 000000000..3ebe07062 |
| --- /dev/null |
| +++ b/docs/superpowers/reviews/REVIEW-BRIEF-flake-lease-teardown.md |
| @@ -0,0 +1,157 @@ |
| +# Review brief: flake-lease-teardown |
| + |
| +You are the independent reviewer (Astra). Review branch `flake-lease-teardown` |
| +on origin (amadan.net) against `origin/main`. The fix is commits `c297d61b` |
| +(code and tests) and `057e43b0` (census re-pin); the merge of main after them |
| +brings in nothing under `internal/lease`. |
| + |
| +## The failure |
| + |
| +`internal/lease` `TestTeardownIsTerminal/abandon_then_close_proven` failed once |
| +on main CI at `0acb2767` (2026-11-19, shard `rest/internal_lease`) and passed on |
| +rerun: |
| + |
| +``` |
| +teardown_linux_test.go:80: the first teardown left 1 descriptors on the database open |
| +``` |
| + |
| +The test tears a non-HA replicator down (Close or Abandon), counts this |
| +process's descriptors on `app.db`, `app.db-wal` and `app.db-shm` through |
| +`/proc/self/fd`, and requires zero. It then calls Close or CloseProven again |
| +and requires ErrSpent, no bucket requests and still zero descriptors. |
| + |
| +## Root cause, with the evidence |
| + |
| +Nothing else opens the counted paths. Each subtest uses its own `t.TempDir()` |
| +database, and the package's tests cannot run in parallel (the lock fixture |
| +uses `t.Setenv`). The descriptor is the WAL, held by litestream v0.5.14's |
| +snapshot encoder: the goroutine `DB.SnapshotReader` starts (db.go:2368). It |
| +opens the WAL read-only, writes the snapshot into an `io.Pipe`, and closes the |
| +WAL in a `defer`. Nothing can join that goroutine. The store's snapshot |
| +monitor runs a pass at `Store.Open`. In the fixture's request log every run |
| +shows a `PUT .../0009/...` (the snapshot), interleaved with the test's own |
| +sync, so that pass is often still running when the test tears down. |
| + |
| +There are two outcomes. I reproduced both. |
| + |
| +1. **A delayed close (test only).** The encoder closes its pipe (success, |
| + or `CloseWithError` once `DB.Close` has nil'd `db.f`: logs show |
| + `write snapshot ltx: read database page 2: invalid argument` 739 times |
| + across the stress runs). The upload returns, `Store.Close`'s `wg.Wait` |
| + returns, and only after that does the encoder's deferred |
| + `walFile.Close()` run. The race window is between the pipe close and |
| + that `defer`. |
| + - Unchanged test under stress: 2 failures, both `app.db-wal`, both |
| + closed by the next scan ("immediately after 0, 200ms later 0"). |
| + - Deterministic: a scratch copy of litestream (via `-modfile` replace) |
| + that sleeps 100 ms before that `defer`'s close made the unchanged test |
| + fail 20/20 with the exact CI message. The fixed test passes 80/80 |
| + against the same copy. |
| + |
| +2. **A permanent leak (a production bug, #811 on non-HA stores).** If |
| + `Store.Close` cancels the monitor after litestream's |
| + `s3.ReplicaClient.WriteLTXFile` has peeked the LTX header |
| + (`ltx.PeekHeader`) but before the AWS uploader reads the body, the |
| + uploader's `partPool.Get(ctx)` (feature/s3/manager v1.20.18, |
| + upload.go:504) refuses the cancelled context. The upload then returns |
| + without reading the rest. litestream never closes the pipe, so the |
| + encoder blocks in `io.(*pipe).write` (inside `ltx.(*Encoder).EncodePage`) |
| + and holds the WAL descriptor and its goroutine for the life of the |
| + process. |
| + - Stress with the test-side fix only: 1 failure at the 30 s guard ("1 |
| + descriptors on .../app.db-wal after 30s"). |
| + - Deterministic: a scratch test cancelled the upload context once the |
| + header had been read, going through the real S3 client and the lock |
| + fixture. The WAL stayed open 3/3, and a goroutine dump showed the |
| + encoder blocked as described. |
| + |
| + The HA path was already guarded: `fenceGate.WriteLTXFile` closes the |
| + pipe, and so do `generationClient` and `snapshotWithin`. A plain non-HA |
| + store had no guard. |
| + |
| +## The fix |
| + |
| +- `internal/lease/snapshotpipe.go` (new): `pipeClosingClient` embeds |
| + `litestream.ReplicaClient`. Its `WriteLTXFile` closes an `*io.PipeReader` |
| + body on every outcome. |
| +- `internal/lease/lease.go:481` (`newStore`): every store's replica client |
| + is wrapped in `pipeClosingClient`. The HA fence gate still wraps the |
| + result in `Replicate`, and a double close of a `PipeReader` is harmless. |
| +- `internal/lease/teardown_linux_test.go`: |
| + - `tornDownFiles`: the database file and `-shm` must be closed as soon as |
| + the teardown returns. Any open SQLite connection, and litestream's own |
| + `db.f`, holds a descriptor on the database file, so these are checked |
| + at once. Only the WAL is waited on, through `WaitDescriptorsForTest` |
| + (the package's #1193 helper, 30 s guard). |
| + - The second-call check in `TestTeardownIsTerminal` is unchanged and |
| + still immediate. By then the encoder has closed its WAL, and a reopen |
| + (DB.init) holds descriptors that do not go away. |
| + - `TestTeardownReleasesACancelledSnapshot` (new): wraps the real S3 |
| + client (`lease.ReadReplicaClient` against the lock fixture) so that the |
| + snapshot upload's context is cancelled once the header has been read. |
| + It asserts the upload really was cancelled (`context.Canceled`), then |
| + Abandons and calls `tornDownFiles`. With `newStore` reverted to the bare |
| + client it fails at the guard ("the teardown left the WAL open: 1 |
| + descriptors ... after 30s"). With the fix it passes. |
| +- Census: envelope `internal/lease` re-pinned; no unit changed |
| + (`docs/superpowers/notes/census-proposal-flake-lease-teardown.md`). |
| + |
| +## Verification run (docs-t1, load average about 55 on 16 cores) |
| + |
| +| Run | Result | |
| +|---|---| |
| +| Before: `go test ./internal/lease -run TestTeardownIsTerminal -count=200` | ok (0/800 subtests) | |
| +| Before: test binary, `-test.count=300`, GOMAXPROCS 1/2/4 at once | 1/3600 subtests failed (WAL, delayed close) | |
| +| Before: 4 processes at GOMAXPROCS=4, `-test.count=300` | 1/4800 failed (WAL, delayed close) | |
| +| Test-side fix only: 7 processes, `-test.count=300` | 1/8400 failed (WAL still open after 30 s: the leak) | |
| +| Both fixes: 8 processes (GOMAXPROCS 1, 2, 4×6), `-test.count=300`, both tests | 0/9600 + 0/2400 | |
| +| Both fixes: `go test ./internal/lease -count=3` (whole package) | ok, 466.9 s | |
| +| Mutation: never close the encoder's WAL (scratch litestream) | fixed test fails at the 30 s guard | |
| +| Mutation: drop the `tornDown` guards in `beginClose` and `guardedSync` | all 4 subtests fail at the second check (requests, 5 descriptors) | |
| +| Mutation: Abandon skips `store.Close` | abandon subtests fail at once ("4 descriptors on the database or its -shm") | |
| +| `go test ./internal/provision -run Census` | failed (envelope), re-pinned, then ok | |
| +| `make code-health` | PASS | |
| + |
| +`-race` on these tests fails on main and on this branch alike: 5 and 2 |
| +reports per 100 runs. The race is `DB.Close` writing `db.f` (db.go:802) |
| +while the encoder reads it (db.go:2099). This is the litestream-internal |
| +race that `.amadan/ci.d/70-race` already gives as the reason |
| +`internal/lease` is left out of the race step (fixed upstream in v0.5.18). |
| +This branch neither causes nor fixes it. |
| + |
| +## What to check |
| + |
| +1. Is the root cause right? Is there any other way the counted paths could |
| + have a descriptor open after a teardown that the fix does not cover? |
| +2. `tornDownFiles`: does checking the database file and `-shm` at once still |
| + catch every real teardown leak, and is waiting on the WAL alone sound? |
| + Could an immediate count of the database file ever see a descriptor that |
| + closes asynchronously too? |
| +3. `pipeClosingClient`: is closing the pipe after a successful |
| + `WriteLTXFile` always safe for every client in use (S3, `lsfile`, |
| + `generationClient`, `fenceGate`, the compactor's own pipes)? It hides the |
| + inner client's optional `ReplicaClientV3`. Confirm nothing restores |
| + through a store's `db.Replica`, and that no other optional interface or |
| + concrete-type assertion on `db.Replica.Client` is lost. |
| +4. Under v0.5.18, which stops the monitors before closing the databases, |
| + the cancellation path in item 2 of the root cause becomes more likely, |
| + not less. Is the wrapper still the right layer for that? |
| +5. Does `TestTeardownReleasesACancelledSnapshot` go through production |
| + wiring (`Held.Replicate`, `newStore`, the real S3 client and uploader), |
| + and is it impossible for it to pass vacuously? It asserts the snapshot |
| + upload was cancelled before tearing down. |
| +6. Census: is the `internal/lease` envelope re-pin acceptable (no root-read |
| + change)? |
| + |
| +## Severity |
| + |
| +- **P1**: unsafe, won't compile, a test fails, or it won't work. |
| +- **P2**: wrong or missing; can be fixed without reopening the design. |
| +- **P3**: polish. |
| + |
| +## Output contract |
| + |
| +List findings ranked P1, then P2, then P3, each with `file:line` and a |
| +one-line fix. End with exactly one of: **READY**, **READY AFTER FIXES**, |
| +**NOT READY**. Do not edit files. Do not run full suites; targeted |
| +`go test ./internal/lease -run '<name>'` runs are fine. |