Skip to content

Commit 65d2ac6

Browse files
ScottN-PVclaude
andcommitted
fix(shared): relay client install waits out a brief Windows file lock
On Windows the installer renames cloudflared.exe right after running it for validation. A process that still holds the file without delete sharing, such as a virus scanner, makes that rename fail with EBUSY, and the install stops with "Could not stage the relay client." Each of the two renames now retries up to 40 times, 250 ms apart, on Windows when it fails with EBUSY, EPERM, or EACCES. Fixes #16945 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 95ccbf4 commit 65d2ac6

2 files changed

Lines changed: 182 additions & 11 deletions

File tree

‎packages/shared/src/relayClient.test.ts‎

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { sha256 } from "@noble/hashes/sha2";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { describe, expect, it } from "@effect/vitest";
44
import * as ConfigProvider from "effect/ConfigProvider";
5+
import * as Deferred from "effect/Deferred";
56
import * as Effect from "effect/Effect";
67
import * as Hex from "effect/encoding/Hex";
8+
import * as Fiber from "effect/Fiber";
79
import * as FileSystem from "effect/FileSystem";
810
import * as Layer from "effect/Layer";
11+
import * as PlatformError from "effect/PlatformError";
912
import * as Sink from "effect/Sink";
1013
import * as Stream from "effect/Stream";
14+
import * as TestClock from "effect/testing/TestClock";
1115
import { HttpClient, HttpClientResponse } from "effect/http";
1216
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1317
import { HostProcessArchitecture, HostProcessPlatform } from "./hostProcess.ts";
@@ -18,9 +22,9 @@ import * as RelayClient from "./relayClient.ts";
1822
// POSIX exec bits that NTFS never reports; the win32 branch skips that check.
1923
const windowsHost = HostProcessPlatform.defaultValue() === "win32";
2024

21-
const layerHostRuntime = (env: Record<string, string> = {}) =>
25+
const layerHostRuntime = (env: Record<string, string> = {}, platform: NodeJS.Platform = "linux") =>
2226
Layer.mergeAll(
23-
Layer.succeed(HostProcessPlatform, "linux"),
27+
Layer.succeed(HostProcessPlatform, platform),
2428
Layer.succeed(HostProcessArchitecture, "x64"),
2529
ConfigProvider.layer(ConfigProvider.fromEnv({ env })),
2630
);
@@ -65,6 +69,64 @@ const layerSpawner = (commands: Array<string>) =>
6569
),
6670
);
6771

72+
const renameError = (code: string, path: string) =>
73+
PlatformError.systemError({
74+
_tag: code === "EBUSY" ? "Busy" : code === "EACCES" ? "PermissionDenied" : "Unknown",
75+
module: "FileSystem",
76+
method: "rename",
77+
pathOrDescriptor: path,
78+
cause: Object.assign(new Error(code), { code }),
79+
});
80+
81+
type InstallRename = "staging" | "activation";
82+
83+
/**
84+
* Fails the first `failures` staging or activation renames with `code`, as Windows does while
85+
* another process holds the file. Staging moves the binary to a `.tmp` name beside its final path.
86+
*/
87+
const makeLockedRenames = (
88+
fileSystem: FileSystem.FileSystem,
89+
input: { readonly rename: InstallRename; readonly failures: number; readonly code: string },
90+
) =>
91+
Effect.gen(function* () {
92+
const firstAttempt = yield* Deferred.make<void>();
93+
const renames = { attempts: 0 };
94+
const locked = FileSystem.make({
95+
...fileSystem,
96+
rename: (from, to) =>
97+
Effect.suspend(() => {
98+
if ((to.endsWith(".tmp") ? "staging" : "activation") !== input.rename) {
99+
return fileSystem.rename(from, to);
100+
}
101+
renames.attempts += 1;
102+
return renames.attempts <= input.failures
103+
? Deferred.succeed(firstAttempt, undefined).pipe(
104+
Effect.andThen(Effect.fail(renameError(input.code, to))),
105+
)
106+
: fileSystem.rename(from, to);
107+
}),
108+
});
109+
return { locked, renames, firstAttempt };
110+
});
111+
112+
const testBinary = new TextEncoder().encode("test-cloudflared-binary");
113+
const testReleaseAsset = {
114+
url: "https://example.test/cloudflared",
115+
sha256: Hex.encode(sha256(testBinary)),
116+
archive: "binary",
117+
} as const;
118+
119+
const layerInstallRuntime = (platform: NodeJS.Platform) =>
120+
Layer.mergeAll(
121+
NodeServices.layer,
122+
layerHttpClient(testBinary),
123+
layerSpawner([]),
124+
layerHostRuntime({ PATH: "" }, platform),
125+
);
126+
127+
const managedDirectory = (baseDir: string, platform: NodeJS.Platform) =>
128+
`${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/${platform}-x64`;
129+
68130
describe("RelayClient", () => {
69131
it.effect.skipIf(windowsHost)(
70132
"resolves explicit overrides before managed and PATH executables",
@@ -278,4 +340,95 @@ describe("RelayClient", () => {
278340
);
279341
},
280342
);
343+
344+
it.effect.each([
345+
["staging", "EBUSY"],
346+
["activation", "EACCES"],
347+
] as const)("retries %s on Windows while another process holds the binary", ([rename, code]) =>
348+
Effect.gen(function* () {
349+
const fileSystem = yield* FileSystem.FileSystem;
350+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
351+
prefix: "t3-cloudflared-test-",
352+
});
353+
const { locked, renames, firstAttempt } = yield* makeLockedRenames(fileSystem, {
354+
rename,
355+
failures: 2,
356+
code,
357+
});
358+
const manager = yield* RelayClient.makeCloudflaredRelayClient({
359+
baseDir,
360+
releaseAsset: testReleaseAsset,
361+
}).pipe(Effect.provideService(FileSystem.FileSystem, locked));
362+
363+
const installing = yield* manager.install.pipe(Effect.forkChild);
364+
yield* Deferred.await(firstAttempt);
365+
yield* TestClock.adjust("1 second");
366+
const installed = yield* Fiber.join(installing);
367+
368+
expect(renames.attempts).toBe(3);
369+
expect(new TextDecoder().decode(yield* fileSystem.readFile(installed.executablePath))).toBe(
370+
"test-cloudflared-binary",
371+
);
372+
expect(yield* fileSystem.readDirectory(managedDirectory(baseDir, "win32"))).toEqual([
373+
"cloudflared.exe",
374+
]);
375+
}).pipe(Effect.scoped, Effect.provide(layerInstallRuntime("win32"))),
376+
);
377+
378+
it.effect.each([
379+
["staging", "EPERM", "Could not stage the relay client."],
380+
["activation", "EBUSY", "Could not activate the relay client."],
381+
] as const)("gives up on a Windows %s lock that does not clear", ([rename, code, message]) =>
382+
Effect.gen(function* () {
383+
const fileSystem = yield* FileSystem.FileSystem;
384+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
385+
prefix: "t3-cloudflared-test-",
386+
});
387+
const { locked, renames, firstAttempt } = yield* makeLockedRenames(fileSystem, {
388+
rename,
389+
failures: Infinity,
390+
code,
391+
});
392+
const manager = yield* RelayClient.makeCloudflaredRelayClient({
393+
baseDir,
394+
releaseAsset: testReleaseAsset,
395+
}).pipe(Effect.provideService(FileSystem.FileSystem, locked));
396+
397+
const installing = yield* manager.install.pipe(Effect.flip, Effect.forkChild);
398+
yield* Deferred.await(firstAttempt);
399+
yield* TestClock.adjust("1 minute");
400+
const error = yield* Fiber.join(installing);
401+
402+
expect(error.message).toBe(message);
403+
expect(renames.attempts).toBe(41);
404+
// Nothing is installed, and the staged copy, download folder, and install lock are gone.
405+
expect(yield* fileSystem.readDirectory(managedDirectory(baseDir, "win32"))).toEqual([]);
406+
}).pipe(Effect.scoped, Effect.provide(layerInstallRuntime("win32"))),
407+
);
408+
409+
it.effect.each([
410+
["linux", "EBUSY"],
411+
["win32", "EXDEV"],
412+
] as const)("fails staging at once on %s for %s", ([platform, code]) =>
413+
Effect.gen(function* () {
414+
const fileSystem = yield* FileSystem.FileSystem;
415+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
416+
prefix: "t3-cloudflared-test-",
417+
});
418+
const { locked, renames } = yield* makeLockedRenames(fileSystem, {
419+
rename: "staging",
420+
failures: Infinity,
421+
code,
422+
});
423+
const manager = yield* RelayClient.makeCloudflaredRelayClient({
424+
baseDir,
425+
releaseAsset: testReleaseAsset,
426+
}).pipe(Effect.provideService(FileSystem.FileSystem, locked));
427+
428+
const error = yield* manager.install.pipe(Effect.flip);
429+
430+
expect(error.message).toBe("Could not stage the relay client.");
431+
expect(renames.attempts).toBe(1);
432+
}).pipe(Effect.scoped, Effect.provide(layerInstallRuntime(platform))),
433+
);
281434
});

‎packages/shared/src/relayClient.ts‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as Layer from "effect/Layer";
1414
import * as Option from "effect/Option";
1515
import * as Path from "effect/Path";
1616
import * as PlatformError from "effect/PlatformError";
17+
import * as Schedule from "effect/Schedule";
1718
import * as Semaphore from "effect/Semaphore";
1819
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
1920
import { ChildProcess, ChildProcessSpawner } from "effect/process";
@@ -101,6 +102,8 @@ const CLOUDFLARED_RELEASE_ASSETS: Readonly<
101102
const INSTALL_LOCK_RETRY_COUNT = 100;
102103
const INSTALL_LOCK_RETRY_DELAY = "100 millis";
103104
const INSTALL_LOCK_STALE_MS = 5 * 60 * 1_000;
105+
const ACTIVATE_RETRY_COUNT = 40;
106+
const ACTIVATE_RETRY_DELAY = "250 millis";
104107

105108
const trimmedString = (name: string) =>
106109
Config.String(name).pipe(
@@ -150,6 +153,13 @@ function isAlreadyExists(error: PlatformError.PlatformError): boolean {
150153
return error.reason._tag === "AlreadyExists";
151154
}
152155

156+
// Windows refuses to rename a file another process briefly holds open, such as a virus
157+
// scanner reading the binary that just ran. These codes clear once it lets go.
158+
function isTransientWindowsLock(error: PlatformError.PlatformError): boolean {
159+
const code = (error.reason.cause as NodeJS.ErrnoException | undefined)?.code;
160+
return code === "EBUSY" || code === "EPERM" || code === "EACCES";
161+
}
162+
153163
const wrapInstallFailure =
154164
(
155165
reason: RelayClientInstallError["reason"],
@@ -260,6 +270,16 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
260270
};
261271
});
262272

273+
/** Moves the new binary toward its final path, waiting out a brief Windows lock. */
274+
const renameWhenUnlocked = (from: string, to: string) =>
275+
fileSystem.rename(from, to).pipe(
276+
Effect.retry({
277+
times: ACTIVATE_RETRY_COUNT,
278+
schedule: Schedule.spaced(ACTIVATE_RETRY_DELAY),
279+
while: (error) => platform === "win32" && isTransientWindowsLock(error),
280+
}),
281+
);
282+
263283
const runCommand = Effect.fn("cloudflared.runCommand")(function* (
264284
command: string,
265285
args: ReadonlyArray<string>,
@@ -426,15 +446,13 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
426446

427447
const stagedPath = `${managedPath}.${yield* crypto.randomUUIDv4}.tmp`;
428448
yield* report("activating");
429-
yield* fileSystem
430-
.rename(executablePath, stagedPath)
431-
.pipe(wrapInstallFailure("write_failed", "Could not stage the relay client."));
432-
yield* fileSystem
433-
.rename(stagedPath, managedPath)
434-
.pipe(
435-
wrapInstallFailure("write_failed", "Could not activate the relay client."),
436-
Effect.ensuring(fileSystem.remove(stagedPath, { force: true }).pipe(Effect.ignore)),
437-
);
449+
yield* renameWhenUnlocked(executablePath, stagedPath).pipe(
450+
wrapInstallFailure("write_failed", "Could not stage the relay client."),
451+
);
452+
yield* renameWhenUnlocked(stagedPath, managedPath).pipe(
453+
wrapInstallFailure("write_failed", "Could not activate the relay client."),
454+
Effect.ensuring(fileSystem.remove(stagedPath, { force: true }).pipe(Effect.ignore)),
455+
);
438456
return {
439457
status: "available",
440458
executablePath: managedPath,

0 commit comments

Comments
 (0)