| index ea39c2469..3be3481c6 100644 |
| --- a/internal/lease/teardown_linux_test.go |
| +++ b/internal/lease/teardown_linux_test.go |
| @@ -3,10 +3,17 @@ |
| package lease_test |
| |
| import ( |
| + "context" |
| "errors" |
| + "io" |
| "os" |
| "path/filepath" |
| + "slices" |
| "testing" |
| + "time" |
| + |
| + "github.com/benbjohnson/litestream" |
| + "github.com/superfly/ltx" |
| |
| "github.com/carlosframework/platform/internal/lease" |
| leasetest "github.com/carlosframework/platform/internal/lease/locktest" |
| @@ -16,6 +23,12 @@ import ( |
| // at path and its -wal and -shm files: litestream's DB.init opens the |
| // database (sql.DB and a long-running descriptor), its Close closes them. |
| func openDBFiles(t *testing.T, path string) int { |
| + t.Helper() |
| + return openFiles(t, path, path+"-wal", path+"-shm") |
| +} |
| + |
| +// openFiles counts this process's open file descriptors on any of paths. |
| +func openFiles(t *testing.T, paths ...string) int { |
| t.Helper() |
| fds, err := os.ReadDir("/proc/self/fd") |
| if err != nil { |
| @@ -27,13 +40,35 @@ func openDBFiles(t *testing.T, path string) int { |
| if err != nil { |
| continue // closed since the listing |
| } |
| - if target == path || target == path+"-wal" || target == path+"-shm" { |
| + if slices.Contains(paths, target) { |
| n++ |
| } |
| } |
| return n |
| } |
| |
| +// tornDownFiles fails t unless the teardown that just returned closed |
| +// every descriptor on the database at path. The database file and its |
| +// -shm must be closed already: a SQLite connection holds a descriptor on |
| +// the database file for as long as it is open, and so does litestream's |
| +// own. The WAL alone may close a moment later. The store's snapshot |
| +// monitor runs a pass at Open, concurrently with the test's first sync, |
| +// and litestream's DB.SnapshotReader encodes in a goroutine nothing can |
| +// join: it opens the WAL read-only and closes it in a defer that runs |
| +// after it has closed its pipe. The store's Close waits for the upload |
| +// reading that pipe, not for the encoder, so it can return in between. |
| +// The WAL check therefore waits for the descriptor itself (#1193); an |
| +// encoder that never lets go (#811, pipeClosingClient) fails it at the |
| +// guard. |
| +func tornDownFiles(t *testing.T, path string) { |
| + t.Helper() |
| + if n := openFiles(t, path, path+"-shm"); n != 0 { |
| + t.Fatalf("the teardown left %d descriptors on the database or its -shm open", n) |
| + } |
| + lease.WaitDescriptorsForTest(t, path+"-wal", "the teardown left the WAL open", |
| + func(open int) bool { return open == 0 }) |
| +} |
| + |
| // replicating is a non-HA replicator of a fresh database on prefix, its |
| // first sync shipped. |
| func replicating(t *testing.T, fx *leasetest.LockFixture, prefix string) (*lease.Replicator, string) { |
| @@ -76,9 +111,7 @@ func TestTeardownIsTerminal(t *testing.T) { |
| if err := tc.first(r); err != nil { |
| t.Fatalf("first teardown: %v", err) |
| } |
| - if n := openDBFiles(t, dbPath); n != 0 { |
| - t.Fatalf("the first teardown left %d descriptors on the database open", n) |
| - } |
| + tornDownFiles(t, dbPath) |
| before := len(fx.Requests()) |
| err := tc.again(r) |
| reqs, open := fx.Requests()[before:], openDBFiles(t, dbPath) |
| @@ -88,3 +121,82 @@ func TestTeardownIsTerminal(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// cancelAfterHeader cancels its upload's context once litestream has |
| +// read the snapshot's LTX header from it (ltx.PeekHeader): a store's |
| +// Close cancelling its snapshot monitor at that point. |
| +type cancelAfterHeader struct { |
| + r io.Reader |
| + n int |
| + cancel context.CancelFunc |
| +} |
| + |
| +func (c *cancelAfterHeader) Read(p []byte) (int, error) { |
| + n, err := c.r.Read(p) |
| + if c.n += n; c.n >= ltx.HeaderSize { |
| + c.cancel() |
| + } |
| + return n, err |
| +} |
| + |
| +// cancelledSnapshot is the real S3 replica client, except that each |
| +// snapshot upload is cancelled once its header has been read. done |
| +// receives the upload's answer. |
| +type cancelledSnapshot struct { |
| + litestream.ReplicaClient |
| + done chan error |
| +} |
| + |
| +func (c cancelledSnapshot) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { |
| + if level != litestream.SnapshotLevel { |
| + return c.ReplicaClient.WriteLTXFile(ctx, level, minTXID, maxTXID, r) |
| + } |
| + ctx, cancel := context.WithCancel(ctx) |
| + defer cancel() |
| + info, err := c.ReplicaClient.WriteLTXFile(ctx, level, minTXID, maxTXID, &cancelAfterHeader{r: r, cancel: cancel}) |
| + select { |
| + case c.done <- err: |
| + default: |
| + } |
| + return info, err |
| +} |
| + |
| +// #811 on a non-HA replicator: the store's snapshot monitor runs a pass at |
| +// Open, and a teardown that cancels it after litestream has peeked the LTX |
| +// header makes the S3 uploader return before it reads the body. litestream |
| +// never closes that pipe, so its encoder would hold the WAL open for the |
| +// life of the process; the store's replica client closes it |
| +// (pipeClosingClient), and the teardown leaves nothing open. |
| +func TestTeardownReleasesACancelledSnapshot(t *testing.T) { |
| + fx := leasetest.NewLockFixture(t) |
| + const prefix = "instances/cancelled.example.com" |
| + h, err := lease.Acquire(ctx, fx.Bucket, prefix, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + s3, err := lease.ReadReplicaClient(ctx, fx.Bucket, prefix, "us-east-1") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + snap := cancelledSnapshot{ReplicaClient: s3, done: make(chan error, 1)} |
| + lease.SetReplicaClientForTest(h, snap) |
| + dbPath := tempDB(t) |
| + r, err := h.Replicate(ctx, dbPath, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { r.Abandon(ctx) }) |
| + if err := r.SyncAndWait(ctx); err != nil { |
| + t.Fatalf("first sync: %v", err) |
| + } |
| + select { |
| + case err := <-snap.done: |
| + if !errors.Is(err, context.Canceled) { |
| + t.Fatalf("the snapshot upload = %v, want it cancelled", err) |
| + } |
| + case <-time.After(30 * time.Second): |
| + t.Fatal("the store's snapshot monitor made no snapshot") |
| + } |
| + r.Abandon(ctx) |
| + tornDownFiles(t, dbPath) |
| +} |