Skip to content

Commit a75f05c

Browse files
fix(file-safety): open the copied target with write access before fsync
Windows FlushFileBuffers requires GENERIC_WRITE on the handle, so the copy-fallback flush opened with "r" can fail the flush and report a false PostCommitDurabilityError on bytes that are already published - a later create attempt then sees the published target as a conflict. Open "r+", matching the existing backup fsync path; the invocation-order pin asserts the write-access flag (NC: reverting to "r" reddens exactly that test). 68/68 green.
1 parent 0c70f6d commit a75f05c

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎src/services/file-safety/__tests__/safeWriteText.spec.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,8 +1188,11 @@ describe("safeWriteText", () => {
11881188

11891189
await safeWriteText(targetPath, "new data", { failIfExist: true, platform: "linux" })
11901190

1191-
// The copied target itself was opened and fsynced AFTER the copy landed...
1192-
expect(fsSync.openSync).toHaveBeenCalledWith(targetPath, "r")
1191+
// The copied target itself was opened and fsynced AFTER the copy landed,
1192+
// with write access: Windows FlushFileBuffers requires GENERIC_WRITE, so a
1193+
// read-only handle would fail the flush and report a false durability error
1194+
// on bytes that are already published.
1195+
expect(fsSync.openSync).toHaveBeenCalledWith(targetPath, "r+")
11931196
const copyOrder = vi.mocked(fs.copyFile).mock.invocationCallOrder[0]
11941197
const openCalls = vi.mocked(fsSync.openSync).mock.calls
11951198
const targetOpenIndex = openCalls.findIndex((call) => String(call[0]) === targetPath)

‎src/services/file-safety/safeWriteText.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,12 @@ export async function safeWriteText(
663663
// damaged bytes. Flush the copied target here; a failure is reported
664664
// like the directory fsync failure, as a durability verdict on bytes
665665
// that are already at the target.
666+
// Open with write access: on Windows FlushFileBuffers requires
667+
// GENERIC_WRITE on the handle, so a read-only handle can fail the flush
668+
// even though the bytes are at the target - the same reason the backup
669+
// fsync path opens "r+".
666670
try {
667-
const copiedFd = fsSync.openSync(targetPath, "r")
671+
const copiedFd = fsSync.openSync(targetPath, "r+")
668672
try {
669673
_fsyncFile(copiedFd)
670674
} finally {

0 commit comments

Comments
 (0)