diff --git a/apps/server/src/cloud/pinnedRuntime.ts b/apps/server/src/cloud/pinnedRuntime.ts index 7cf6b296cc45b..e7fba4c8cfc8c 100644 --- a/apps/server/src/cloud/pinnedRuntime.ts +++ b/apps/server/src/cloud/pinnedRuntime.ts @@ -5,6 +5,7 @@ import * as Effect from "effect/Effect"; import * as Hex from "effect/encoding/Hex"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; +import type * as PlatformError from "effect/PlatformError"; import * as Schema from "effect/Schema"; import * as Option from "effect/Option"; import * as Schedule from "effect/Schedule"; @@ -508,6 +509,46 @@ export const isUnpublishedRelease = (error: unknown): error is PinnedRuntimeInst HttpClientError.isHttpClientError(error.cause) && error.cause.response?.status === 404)); +// Windows only flushes handles opened for writing and refuses to flush directories (NTFS journals +// renames itself), so directory flushes there are allowed to fail with EPERM. +const syncPath = ( + fs: FileSystem.FileSystem, + platform: NodeJS.Platform, + target: string, + isDirectory: boolean, +) => + fs.open(target, { flag: platform === "win32" && !isDirectory ? "r+" : "r" }).pipe( + Effect.flatMap((file) => file.sync), + Effect.scoped, + Effect.catchIf( + (error) => isDirectory && error.reason._tag === "PermissionDenied", + () => Effect.void, + ), + ); + +/** Tar leaves the extracted runtime in the page cache; a crash before writeback would publish a truncated binary. */ +const syncTree = ( + fs: FileSystem.FileSystem, + path: Path.Path, + platform: NodeJS.Platform, + directory: string, +): Effect.Effect => + fs.readDirectoryEntries(directory).pipe( + Effect.flatMap((entries) => + Effect.forEach( + entries, + (entry) => { + const target = path.join(directory, entry.name); + if (entry.type === "Directory") return syncTree(fs, path, platform, target); + if (entry.type === "File") return syncPath(fs, platform, target, false); + return Effect.void; + }, + { concurrency: 16, discard: true }, + ), + ), + Effect.andThen(syncPath(fs, platform, directory, true)), + ); + const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(function* ( input: PinnedRuntimeInstallInput, ) { @@ -596,6 +637,12 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")( new PinnedRuntimeInstallError({ step: "recording the completed install", cause }), ), ); + yield* syncTree(fs, input.path, input.platform, stagingDir).pipe( + Effect.mapError( + (cause) => + new PinnedRuntimeInstallError({ step: "flushing the pinned runtime to disk", cause }), + ), + ); const published = yield* fs.rename(stagingDir, paths.versionDir).pipe( Effect.as(true), Effect.catch((cause) => @@ -626,6 +673,12 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")( ), ); if (!published) yield* input.validate(paths); + yield* syncPath(fs, input.platform, versionsDir, true).pipe( + Effect.mapError( + (cause) => + new PinnedRuntimeInstallError({ step: "flushing the pinned runtime to disk", cause }), + ), + ); return paths; }).pipe( Effect.ensuring(fs.remove(stagingDir, { recursive: true, force: true }).pipe(Effect.ignore)),