Skip to content

Commit 032678d

Browse files
fix(safe-write-json): keep the published target across a post-commit durability error
The 08:21Z Persistence Integrity row asked for commit-state tracking across the safeWriteText failure boundary. The observable behavior at this head was already correct - the catch cleanup unlinks the staging path, which the commit rename had consumed, and the ENOENT-tolerant handler swallows the miss - but the target survived only by accident: the unlink still fires at a path this write no longer owns, so a concurrent writer that reuses the staging name between the rename and the cleanup loses its temp file to this write's catch. safeWriteJson now clears the staged path when safeWriteText reports PostCommitDurabilityError: the rename landed, the staged path is the published target's former name, and the durability error rethrows so the write is never observed as a confirmed-durable success. Regression test: the parent-directory fsync fails after the commit rename; the target stays present with the new JSON and no unlink fires at the consumed staging path. Negative control: removing the guard reddens exactly that test. 114/119 with the file-safety suites (5 pre-existing skips).
1 parent cc84906 commit 032678d

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎src/utils/__tests__/safeWriteJson.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Writable } from "stream"
33
import * as path from "path"
44
import * as os from "os"
55

6-
import { StagingPathError } from "../../services/file-safety/safeWriteText"
6+
import { PostCommitDurabilityError, StagingPathError } from "../../services/file-safety/safeWriteText"
77
import { ConfinedPathEscapeError, safeWriteJson } from "../safeWriteJson"
88
import * as lockfile from "proper-lockfile"
99
import * as fileLockModule from "../fileLock"
@@ -1102,4 +1102,44 @@ describe("safeWriteJson", () => {
11021102
{ inside: true },
11031103
)
11041104
})
1105+
1106+
// ── post-commit durability: the published target must survive (PI regression) ──
1107+
1108+
it("keeps the published target with its new content when the post-commit directory fsync fails", async () => {
1109+
const fsSyncMod = await import("fs")
1110+
const realOpen = fsSyncMod.openSync
1111+
const realFsync = fsSyncMod.fsyncSync
1112+
// A sentinel fd identifies the parent-directory open that safeWriteText
1113+
// performs after the commit rename; only that fsync fails.
1114+
const sentinel = 987654
1115+
vi.spyOn(process, "platform", "get").mockReturnValue("linux")
1116+
vi.spyOn(fsSyncMod, "openSync").mockImplementation(((...args: unknown[]) => {
1117+
if (String(args[0]) === tempDir) {
1118+
return sentinel
1119+
}
1120+
return (realOpen as (...a: unknown[]) => unknown)(...args)
1121+
}) as never)
1122+
vi.spyOn(fsSyncMod, "fsyncSync").mockImplementation(((fd: number) => {
1123+
if (fd === sentinel) {
1124+
throw new Error("EIO fsync")
1125+
}
1126+
return (realFsync as (f: number) => unknown)(fd)
1127+
}) as never)
1128+
1129+
// Earlier tests in this file leave unlink history on the shared mock.
1130+
vi.mocked(fs.unlink).mockClear()
1131+
1132+
await expect(safeWriteJson(currentTestFilePath, { fresh: "content" })).rejects.toBeInstanceOf(
1133+
PostCommitDurabilityError,
1134+
)
1135+
1136+
// The rename committed: the target must still hold the new JSON, not be
1137+
// unlinked away by the catch cleanup of the consumed staging path.
1138+
const content = await readFileContent(currentTestFilePath)
1139+
expect(content).toEqual({ fresh: "content" })
1140+
// The cleanup must not unlink the consumed staging path: after the commit
1141+
// rename it is no longer this write's file to remove.
1142+
const stagingUnlinks = vi.mocked(fs.unlink).mock.calls.filter((call) => /\.new_.*\.tmp$/.test(String(call[0])))
1143+
expect(stagingUnlinks).toHaveLength(0)
1144+
})
11051145
})

‎src/utils/safeWriteJson.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { JsonStreamStringify } from "json-stream-stringify"
55

66
import { acquireFileLock } from "./fileLock"
77
import {
8+
PostCommitDurabilityError,
89
resolveLockKey,
910
resolvePublishTarget,
1011
safeWriteText,
@@ -278,6 +279,16 @@ async function safeWriteJson(filePath: string, data: any, options?: SafeWriteJso
278279
originalError,
279280
)
280281

282+
// PostCommitDurabilityError means the commit rename landed: the staged
283+
// path was renamed onto the target, so it is no longer a leftover temp
284+
// file. Unlinking it here would at best race a concurrent writer that has
285+
// already reused the staging name, and the published target must survive
286+
// the durability failure with its new content. The error rethrows below,
287+
// so the write is never observed as a confirmed-durable success.
288+
if (originalError instanceof PostCommitDurabilityError) {
289+
actualTempNewFilePath = null
290+
}
291+
281292
const newFileToCleanupWithinCatch = actualTempNewFilePath
282293

283294
// A failed safeWriteText already deleted its backup copy (the target is

0 commit comments

Comments
 (0)