Skip to content

Commit 0fd6d59

Browse files
fix(safeWriteJson): synchronise the stage on the descriptor that wrote it
Two review rows on PR 1405 asked for executable lines, and both live in the staging path of this file: - The descriptor that wrote the stage never being synchronised. The write stream is now created with autoClose disabled, its finish handler calls fsyncSync on the staging descriptor before closing it, and the error path closes that descriptor instead of leaving it open. The reopen with r+ in _flushFileToDisk stays in place as a second flush, but publication no longer depends on it being the only synchronisation, and a failing write stream no longer leaks the descriptor - which is what the error-stream doubles in the spec could not show, because a doubled Writable owns no descriptor. - The inspection failure falling through a bare catch into the creation default. The fallback to 0o644 now happens only for ENOENT, the code the same file already names explicitly at its other inspection sites; a permission or I/O error propagates instead of silently deciding the mode a file the caller can see will be published with. Three new rows in src/utils/__tests__/safeWriteJson.test.ts each measured red on the pull head before the change and green after it. No suppression was added: the per-file eslint count for both files stays at zero.
1 parent 4e538dc commit 0fd6d59

2 files changed

Lines changed: 121 additions & 4 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,85 @@ describe("safeWriteJson guarded-publish requirements carried by the guarded-writ
10781078
consoleErrorSpy.mockRestore()
10791079
})
10801080

1081+
// The flush row asked that the stage be synchronised through the descriptor that wrote it
1082+
// rather than only through a later reopen. This row measures exactly that hand: the fsync
1083+
// call carries the descriptor the staging open returned, and it happens before that
1084+
// descriptor is closed. Until the change the only synchronisation in the module was
1085+
// _flushFileToDisk reopening the file with r+, which leaves this assertion unmatched.
1086+
test("synchronises the stage on the descriptor that opened it before closing that descriptor", async () => {
1087+
spyOnStageOpens(false)
1088+
const fsyncSpy = vi.spyOn(fsSyncActual, "fsyncSync")
1089+
const closeSpy = vi.spyOn(fsSyncActual, "closeSync")
1090+
1091+
await safeWriteJson(currentTestFilePath, { message: "Synced" })
1092+
1093+
const streamCall = vi.mocked(fsSyncActual.createWriteStream).mock.calls.at(-1)
1094+
const stagedFd = Number((streamCall?.[1] as { fd?: number } | undefined)?.fd)
1095+
expect(Number.isInteger(stagedFd)).toBe(true)
1096+
expect(fsyncSpy.mock.calls.map((call) => Number(call[0]))).toContain(stagedFd)
1097+
const syncOrder = fsyncSpy.mock.invocationCallOrder.at(0)
1098+
const lastClose = closeSpy.mock.invocationCallOrder.at(-1)
1099+
expect(syncOrder).toBeDefined()
1100+
expect(lastClose).toBeDefined()
1101+
expect(Number(syncOrder)).toBeLessThan(Number(lastClose))
1102+
expect(await readFileContent(currentTestFilePath)).toEqual({ message: "Synced" })
1103+
})
1104+
1105+
// The row about the error-stream doubles asked that a failing stream not leave the staging
1106+
// descriptor open. The doubles hand back a Writable that owns no descriptor, so the cleanup
1107+
// has to come from the writer: the descriptor it opened for the stream is closed when the
1108+
// stream fails, which is what the assertion below reads.
1109+
test("closes the staging descriptor when the write stream fails", async () => {
1110+
const stageOpens = spyOnStageOpens(false)
1111+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
1112+
const closeSpy = vi.spyOn(fsSyncActual, "closeSync")
1113+
const openedFds: number[] = []
1114+
const streamSpy = vi.mocked(fsSyncActual.createWriteStream)
1115+
streamSpy.mockImplementationOnce((_streamPath, options) => {
1116+
const fd = (options as { fd?: number } | undefined)?.fd
1117+
if (typeof fd === "number") {
1118+
openedFds.push(fd)
1119+
}
1120+
return makeErrorWriteStream("Stream failure before commit")
1121+
})
1122+
1123+
await expect(safeWriteJson(currentTestFilePath, { message: "Failed" })).rejects.toThrow(
1124+
"Stream failure before commit",
1125+
)
1126+
1127+
expect(openedFds).toHaveLength(1)
1128+
expect(closeSpy.mock.calls.map((call) => Number(call[0]))).toContain(openedFds[0])
1129+
expect(stageOpens.length).toBeGreaterThan(0)
1130+
expect(await readFileContent(currentTestFilePath)).toEqual({ initial: "content" })
1131+
streamSpy.mockRestore()
1132+
consoleErrorSpy.mockRestore()
1133+
})
1134+
1135+
// The catch around the staging-mode inspection has to fall back to the creation default only
1136+
// for a target that is genuinely missing. The same file names ENOENT explicitly at every
1137+
// other inspection site (the ancestor walk and the publication re-checks), and a permission
1138+
// or I/O error says nothing about the target's mode, so taking the default would silently
1139+
// widen or narrow it: the error has to propagate instead.
1140+
test("propagates a staging-mode inspection error instead of taking the creation default", async () => {
1141+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
1142+
const realStatSync = fsSyncActual.statSync
1143+
// The double must reach the real stat for paths other than the target, and statSync's
1144+
// overloads are not expressible through a delegate arrow, so the cast is the last resort.
1145+
const statSpy = vi.spyOn(fsSyncActual, "statSync")
1146+
statSpy.mockImplementation(((statPath: unknown) => {
1147+
if (String(statPath) === currentTestFilePath) {
1148+
throw Object.assign(new Error("EACCES: permission denied, stat"), { code: "EACCES" })
1149+
}
1150+
return realStatSync(String(statPath))
1151+
}) as unknown as typeof fsSyncActual.statSync)
1152+
1153+
await expect(safeWriteJson(currentTestFilePath, { message: "New" })).rejects.toThrow("EACCES")
1154+
1155+
statSpy.mockRestore()
1156+
expect(await readFileContent(currentTestFilePath)).toEqual({ initial: "content" })
1157+
consoleErrorSpy.mockRestore()
1158+
})
1159+
10811160
test("stages the payload at the mode of the file it replaces", async () => {
10821161
const targetMode = fsSyncActual.statSync(currentTestFilePath).mode & 0o777
10831162
const stageOpens = spyOnStageOpens(false)

‎src/utils/safeWriteJson.ts‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,15 @@ async function _streamDataToFile(
480480
let targetExisted = true
481481
try {
482482
stageMode = fsSync.statSync(preserveModeOf).mode & 0o777
483-
} catch {
483+
} catch (modeError) {
484+
// Only a genuinely missing target may fall back to the creation default. Every other
485+
// inspection in this file names ENOENT explicitly - the ancestor walk, the destination
486+
// re-checks and the publication hook - and a permission or I/O error says nothing about
487+
// the target's mode, so taking 0o644 for it could widen a 0o600 file or narrow a shared
488+
// one through the commit rename. Such an error propagates to the caller instead.
489+
if (getErrorCode(modeError) !== "ENOENT") {
490+
throw modeError
491+
}
484492
// No target yet: the default below leaves the process umask in charge, as before.
485493
targetExisted = false
486494
}
@@ -497,13 +505,43 @@ async function _streamDataToFile(
497505
}
498506
}
499507
let fileWriteStream: fsSync.WriteStream
508+
let stagingFdClosed = false
509+
const closeStagingFd = () => {
510+
if (stagingFdClosed) {
511+
return
512+
}
513+
stagingFdClosed = true
514+
try {
515+
fsSync.closeSync(stagedFd)
516+
} catch (closeError) {
517+
console.error(`Failed to close the staging descriptor for ${targetPath}:`, closeError)
518+
}
519+
}
500520
try {
501-
// The stream owns the descriptor: it closes it on finish and on error.
502-
fileWriteStream = fsSync.createWriteStream(targetPath, { fd: stagedFd, encoding: "utf8" })
521+
// The stream owns the descriptor but must not close it before the payload that carries
522+
// the credentials is synchronised on that same descriptor, so autoClose is turned off
523+
// and the finish handler does the hand: fsync first, then close. The flush below still
524+
// reopens the stage with r+ as well, but publication no longer depends on that reopen
525+
// being the only place the stage reaches storage.
526+
fileWriteStream = fsSync.createWriteStream(targetPath, {
527+
fd: stagedFd,
528+
encoding: "utf8",
529+
autoClose: false,
530+
})
503531
} catch (streamError) {
504-
fsSync.closeSync(stagedFd)
532+
closeStagingFd()
505533
throw streamError
506534
}
535+
fileWriteStream.on("finish", () => {
536+
try {
537+
fsSync.fsyncSync(stagedFd)
538+
} catch (syncError) {
539+
// A filesystem that refuses fsync must not block the write: same rule as the flush.
540+
console.error(`Failed to synchronise ${targetPath} on its staging descriptor:`, syncError)
541+
}
542+
closeStagingFd()
543+
})
544+
fileWriteStream.on("error", closeStagingFd)
507545

508546
// JsonStreamStringify traverses the object and streams tokens directly
509547
// The 'spaces' parameter adds indentation during streaming, not via a separate pass

0 commit comments

Comments
 (0)