From c662bec716f1c601d89b28d290648924662eb962 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 15:01:24 -0600 Subject: [PATCH] fix(journal): close the region in Stop when the final flush or MarkClean fails Stop returned early on those paths without calling region.Close. Close does nothing today, since the caller owns the device, so nothing leaked; Stop now always closes and joins the errors so a future Close cannot be skipped. A new test covers the failed-flush path of Stop. Co-Authored-By: Claude Opus 5.5 --- internal/journal/journal.go | 21 +++++----- internal/journal/journal_test.go | 69 ++++++++++++++++++++++++++------ tests/test-manifest.yaml | 7 ++++ 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/internal/journal/journal.go b/internal/journal/journal.go index ca2ec981..e81fba88 100644 --- a/internal/journal/journal.go +++ b/internal/journal/journal.go @@ -195,24 +195,21 @@ func (j *Journal) Stop() error { close(j.checkpointStop) j.checkpointWg.Wait() - // Mark journal clean on orderly shutdown j.mu.Lock() defer j.mu.Unlock() - // Leave the journal dirty so the next mount replays what may not have synced. - if flushErr != nil { - stopErr = fmt.Errorf("failed to flush batched commits: %w", flushErr) - return - } - - if len(j.activeTxns) == 0 && len(j.pendingIntents) == 0 { + var cleanErr error + switch { + case flushErr != nil: + // Leave the journal dirty so the next mount replays what may not have synced. + cleanErr = fmt.Errorf("failed to flush batched commits: %w", flushErr) + case len(j.activeTxns) == 0 && len(j.pendingIntents) == 0: + // Mark journal clean on orderly shutdown if err := j.region.MarkClean(); err != nil { - stopErr = fmt.Errorf("failed to mark journal clean: %w", err) - return + cleanErr = fmt.Errorf("failed to mark journal clean: %w", err) } } - - stopErr = j.region.Close() + stopErr = errors.Join(cleanErr, j.region.Close()) }) return stopErr } diff --git a/internal/journal/journal_test.go b/internal/journal/journal_test.go index 4805da22..1020ff8b 100644 --- a/internal/journal/journal_test.go +++ b/internal/journal/journal_test.go @@ -1,6 +1,7 @@ package journal import ( + "errors" "fmt" "os" "sync" @@ -679,19 +680,7 @@ func TestBatchCommitReturnsSyncError(t *testing.T) { result <- err }() - deadline := time.Now().Add(5 * time.Second) - for { - j.batchMu.Lock() - pending := j.batchPendingCount - j.batchMu.Unlock() - if pending == 1 { - break - } - if time.Now().After(deadline) { - t.Fatal("commit never joined a batch") - } - time.Sleep(time.Millisecond) - } + waitForPendingBatch(t, j) if err := dev.Close(); err != nil { t.Fatal(err) } @@ -734,3 +723,57 @@ func TestCheckpointAdvancesHeadNow(t *testing.T) { t.Fatalf("head %d != tail %d after Checkpoint", head, tail) } } + +func waitForPendingBatch(t *testing.T, j *Journal) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for { + j.batchMu.Lock() + pending := j.batchPendingCount + j.batchMu.Unlock() + if pending > 0 { + return + } + if time.Now().After(deadline) { + t.Fatal("commit never joined a batch") + } + time.Sleep(time.Millisecond) + } +} + +// Stop reports a final batch flush that failed, and a second Stop does nothing. +func TestStopReportsFailedBatchFlush(t *testing.T) { + dev, cleanup := createTestDevice(t, 10*1024*1024) + defer cleanup() + config := DefaultConfig() + config.CommitMode = CommitBatch + config.BatchConfig = BatchConfig{MaxBatchTransactions: 100, MaxBatchAge: time.Minute} + j, err := Init(dev, 0, DefaultJournalSlotSize, [16]byte{1}, 1, config) + if err != nil { + t.Fatalf("Init failed: %v", err) + } + + txn := j.Begin() + if err := txn.LogInodeCreate(testInodePayload(100)); err != nil { + t.Fatal(err) + } + result := make(chan error, 1) + go func() { + _, err := j.Commit(txn) + result <- err + }() + waitForPendingBatch(t, j) + if err := dev.Close(); err != nil { + t.Fatal(err) + } + + if err := j.Stop(); !errors.Is(err, os.ErrInvalid) { + t.Fatalf("Stop = %v, want the failed sync's error", err) + } + if err := <-result; err == nil { + t.Fatal("Commit returned nil after its batch sync failed") + } + if err := j.Stop(); err != nil { + t.Fatalf("second Stop = %v, want nil", err) + } +} diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index 7f7a874d..d79c8cb1 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -764,6 +764,13 @@ scenarios: environments: go: internal/journal/journal_test.go::TestBatchCommitReturnsSyncError + - id: journal-stop-failed-flush + name: Stop reports a failed final flush + description: Journal Stop returns the error of a final batch flush that failed, the waiting commit gets it too, and a second Stop does nothing + priority: go_only + environments: + go: internal/journal/journal_test.go::TestStopReportsFailedBatchFlush + - id: checkpoint-keeps-incomplete-intents name: Checkpoints never pass an intent that has not completed description: A transaction committed but not yet applied stays in the journal through a checkpoint and an unmount, and the next mount replays it; completed intents are checkpointed away