TypeScript
fix(server): tolerate the missing directory fsync on Windows (#9569)
Julius Marmingef083f520fbde
diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.tsindex 50ffcedf74b..a0bd6f30c6c 100644--- a/apps/server/src/cloud/bootService.ts+++ b/apps/server/src/cloud/bootService.ts@@ -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 = {diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.tsindex b99f90e0fdf..e912130c893 100644--- a/apps/server/src/serviceLauncher.ts+++ b/apps/server/src/serviceLauncher.ts@@ -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);