fix(server): tolerate the missing directory fsync on Windows (#9569)
Co-authored-by: Claude Code <[email protected]>
This commit is contained in:
1 parent
c251e41b5e
commit
f083f520fb
2 files changed
+17
-9
No files matched your search
@@ -555,9 +555,16 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
|
||||
yield* fs.makeDirectory(directory, { recursive: true });
|
||||
const tempPath = yield* fs.makeTempFileScoped({ directory, prefix: ".service-write-" });
|
||||
yield* fs.writeFileString(tempPath, contents, { mode: 0o600 });
|
||||
yield* (yield* fs.open(tempPath, { flag: "r" })).sync;
|
||||
// Opened read-write: Windows refuses to flush a handle without write access.
|
||||
yield* (yield* fs.open(tempPath, { flag: "r+" })).sync;
|
||||
yield* fs.rename(tempPath, filePath);
|
||||
yield* (yield* fs.open(directory, { flag: "r" })).sync;
|
||||
// Windows has no directory fsync (EPERM); NTFS journals the rename.
|
||||
yield* (yield* fs.open(directory, { flag: "r" })).sync.pipe(
|
||||
Effect.catchIf(
|
||||
(error) => (error.reason.cause as NodeJS.ErrnoException | undefined)?.code === "EPERM",
|
||||
() => Effect.void,
|
||||
),
|
||||
);
|
||||
}),
|
||||
).pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
|
||||
const plan: BootServicePlan = {
|
||||
|
||||
@@ -70,8 +70,9 @@ async function pathExists(target: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
// Opened read-write: Windows refuses to flush a handle without write access.
|
||||
async function syncFile(filePath: string): Promise<void> {
|
||||
const handle = await NodeFSP.open(filePath, "r");
|
||||
const handle = await NodeFSP.open(filePath, "r+");
|
||||
try {
|
||||
await handle.sync();
|
||||
} finally {
|
||||
@@ -79,10 +80,15 @@ async function syncFile(filePath: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
// Flushes a directory entry so a rename into it survives power loss. Windows
|
||||
// has no directory fsync: the handle opens but sync fails with EPERM, and
|
||||
// NTFS journals the rename on its own.
|
||||
async function syncDirectory(directory: string): Promise<void> {
|
||||
const handle = await NodeFSP.open(directory, "r");
|
||||
try {
|
||||
await handle.sync();
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "EPERM") throw error;
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
@@ -188,12 +194,7 @@ export async function writeServiceState(filePath: string, state: ServiceState):
|
||||
await handle.close();
|
||||
handle = undefined;
|
||||
await NodeFSP.rename(tempPath, filePath);
|
||||
const directoryHandle = await NodeFSP.open(directory, "r");
|
||||
try {
|
||||
await directoryHandle.sync();
|
||||
} finally {
|
||||
await directoryHandle.close();
|
||||
}
|
||||
await syncDirectory(directory);
|
||||
} finally {
|
||||
await handle?.close().catch(() => undefined);
|
||||
await NodeFSP.rm(tempPath, { force: true }).catch(() => undefined);
|
||||
|
||||
Reference in new issue
Block a user