From 43dc964c25cc280551a203451c01ad775870e25c Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Mon, 24 Aug 2026 14:29:57 +0200 Subject: [PATCH] direct: remove the WAL Open created when its header cannot be written O_EXCL means the file is ours and a WAL without a header carries nothing to recover, but leaving it made every later open fail: on the header parse with recovery, and on the unexpected WAL file without it. Co-authored-by: Isaac --- bundle/direct/dstate/state.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bundle/direct/dstate/state.go b/bundle/direct/dstate/state.go index af2e2638eb..3ea376b071 100644 --- a/bundle/direct/dstate/state.go +++ b/bundle/direct/dstate/state.go @@ -316,7 +316,16 @@ func (db *DeploymentState) unlockedOpen(ctx context.Context, path string, withRe StateVersion: currentStateVersion, CLIVersion: build.GetInfo().Version, } - return appendJSONLine(db.walFile, walHead) + if err := appendJSONLine(db.walFile, walHead); err != nil { + // Remove the WAL created just above: O_EXCL means it is ours, and + // without a header it carries nothing to recover. Leaving it makes + // every later open fail, with recovery on the header parse and + // without it on the unexpected WAL file. + db.walFile.Close() + db.walFile = nil + os.Remove(walPath) + return fmt.Errorf("failed to write WAL header to %s: %w", walPath, err) + } } return nil