Skip to content

Commit c8e346b

Browse files
yashranawayclaude
authored andcommitted
fix(shared): release relay install locks on cancellation
The install lock is now acquired with acquireRelease inside the install scope, so cancelling an install at any point, including right after the lock write lands, removes the lock this installer created. A contended install that is cancelled leaves the other installer's lock alone. Rebased onto main after #17275 (the two original commits are squashed). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2a93885 commit c8e346b

2 files changed

Lines changed: 139 additions & 3 deletions

File tree

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ 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";
7+
import * as Fiber from "effect/Fiber";
68
import * as Hex from "effect/encoding/Hex";
79
import * as FileSystem from "effect/FileSystem";
810
import * as Layer from "effect/Layer";
@@ -92,6 +94,133 @@ const managedPathFor = (baseDir: string, version: string) =>
9294
`${baseDir}/tools/cloudflared/${version}/linux-x64/cloudflared`;
9395

9496
describe("RelayClient", () => {
97+
it.effect("cancels a contended install without removing the other installer's lock", () =>
98+
Effect.gen(function* () {
99+
const fileSystem = yield* FileSystem.FileSystem;
100+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
101+
const directory = `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64`;
102+
const lockPath = `${directory}/cloudflared.lock`;
103+
yield* fileSystem.makeDirectory(directory, { recursive: true });
104+
yield* fileSystem.writeFileString(lockPath, "other-installer");
105+
const contended = yield* Deferred.make<void>();
106+
const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir }).pipe(
107+
Effect.provideService(FileSystem.FileSystem, {
108+
...fileSystem,
109+
writeFileString: (path, contents, options) =>
110+
fileSystem
111+
.writeFileString(path, contents, options)
112+
.pipe(
113+
Effect.tapError(() =>
114+
path === lockPath ? Deferred.succeed(contended, undefined) : Effect.void,
115+
),
116+
),
117+
}),
118+
);
119+
const installing = yield* manager.install.pipe(Effect.forkChild);
120+
yield* Deferred.await(contended);
121+
yield* Fiber.interrupt(installing);
122+
expect(yield* fileSystem.readFileString(lockPath)).toBe("other-installer");
123+
}).pipe(
124+
Effect.scoped,
125+
Effect.provide(
126+
Layer.mergeAll(
127+
NodeServices.layer,
128+
layerHttpClient(new Uint8Array()),
129+
layerSpawner([]),
130+
layerHostRuntime({ PATH: "" }),
131+
),
132+
),
133+
),
134+
);
135+
136+
it.effect("releases the install lock when a download is cancelled", () =>
137+
Effect.gen(function* () {
138+
const fileSystem = yield* FileSystem.FileSystem;
139+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
140+
const lockPath = `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`;
141+
const downloading = yield* Deferred.make<void>();
142+
const manager = yield* RelayClient.makeCloudflaredRelayClient({ baseDir });
143+
const installing = yield* manager
144+
.installWithProgress((event) =>
145+
event.type === "progress" && event.stage === "downloading"
146+
? Deferred.succeed(downloading, undefined).pipe(Effect.andThen(Effect.never))
147+
: Effect.void,
148+
)
149+
.pipe(Effect.forkChild);
150+
yield* Deferred.await(downloading);
151+
expect(yield* fileSystem.exists(lockPath)).toBe(true);
152+
yield* Fiber.interrupt(installing);
153+
expect(yield* fileSystem.exists(lockPath)).toBe(false);
154+
}).pipe(
155+
Effect.scoped,
156+
Effect.provide(
157+
Layer.mergeAll(
158+
NodeServices.layer,
159+
layerHttpClient(new Uint8Array()),
160+
layerSpawner([]),
161+
layerHostRuntime({ PATH: "" }),
162+
),
163+
),
164+
),
165+
);
166+
167+
it.effect.skipIf(windowsHost)("releases a lock acquired while installation is cancelled", () =>
168+
Effect.gen(function* () {
169+
const fileSystem = yield* FileSystem.FileSystem;
170+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
171+
const lockPath = `${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`;
172+
const acquired = yield* Deferred.make<void>();
173+
const completeWrite = yield* Deferred.make<void>();
174+
let pauseWrite = true;
175+
const manager = yield* RelayClient.makeCloudflaredRelayClient({
176+
baseDir,
177+
releaseAsset: {
178+
url: "https://example.test/cloudflared",
179+
sha256: Hex.encode(sha256(new TextEncoder().encode("test-binary"))),
180+
archive: "binary",
181+
},
182+
}).pipe(
183+
Effect.provideService(FileSystem.FileSystem, {
184+
...fileSystem,
185+
writeFileString: (path, contents, options) =>
186+
fileSystem
187+
.writeFileString(path, contents, options)
188+
.pipe(
189+
Effect.tap(() =>
190+
path === lockPath && pauseWrite
191+
? Deferred.succeed(acquired, undefined).pipe(
192+
Effect.andThen(Deferred.await(completeWrite)),
193+
)
194+
: Effect.void,
195+
),
196+
),
197+
}),
198+
);
199+
200+
const installing = yield* manager.install.pipe(Effect.forkChild);
201+
yield* Deferred.await(acquired);
202+
const cancelling = yield* Fiber.interrupt(installing).pipe(
203+
Effect.forkChild({ startImmediately: true }),
204+
);
205+
yield* Deferred.succeed(completeWrite, undefined);
206+
yield* Fiber.join(cancelling);
207+
expect(yield* fileSystem.exists(lockPath)).toBe(false);
208+
209+
pauseWrite = false;
210+
expect(yield* manager.install).toMatchObject({ status: "available" });
211+
expect(yield* fileSystem.exists(lockPath)).toBe(false);
212+
}).pipe(
213+
Effect.scoped,
214+
Effect.provide(
215+
Layer.mergeAll(
216+
NodeServices.layer,
217+
layerHttpClient(new TextEncoder().encode("test-binary")),
218+
layerSpawner([]),
219+
layerHostRuntime({ PATH: "" }),
220+
),
221+
),
222+
),
223+
);
95224
it.effect.skipIf(windowsHost)(
96225
"resolves explicit overrides before managed and PATH executables",
97226
() =>
@@ -212,6 +341,11 @@ describe("RelayClient", () => {
212341
const error = yield* manager.install.pipe(Effect.flip);
213342
expect(error).toBeInstanceOf(RelayClient.RelayClientInstallError);
214343
expect(error.reason).toBe("invalid_checksum");
344+
expect(
345+
yield* fileSystem.exists(
346+
`${baseDir}/tools/cloudflared/${RelayClient.CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`,
347+
),
348+
).toBe(false);
215349
}).pipe(
216350
Effect.scoped,
217351
Effect.provide(

‎packages/shared/src/relayClient.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
466466
lockPath: string,
467467
) {
468468
for (let attempt = 0; attempt < INSTALL_LOCK_RETRY_COUNT; attempt += 1) {
469-
const acquired = yield* fileSystem.writeFileString(lockPath, "", { flag: "wx" }).pipe(
469+
const acquired = yield* Effect.acquireRelease(
470+
fileSystem.writeFileString(lockPath, "", { flag: "wx" }),
471+
() => fileSystem.remove(lockPath, { force: true }).pipe(Effect.ignore),
472+
).pipe(
470473
Effect.as(true),
471474
Effect.catchIf(isAlreadyExists, () => Effect.succeed(false)),
472475
);
@@ -591,7 +594,6 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
591594
} satisfies AvailableRelayClient;
592595
}).pipe(
593596
Effect.scoped,
594-
Effect.ensuring(fileSystem.remove(lockPath, { force: true }).pipe(Effect.ignore)),
595597
Effect.catchIf(
596598
(cause) => !(cause instanceof RelayClientInstallError),
597599
(cause) =>
@@ -612,7 +614,7 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
612614
type: "progress",
613615
stage,
614616
}),
615-
),
617+
).pipe(Effect.scoped),
616618
);
617619
const install = installWithProgress(() => Effect.void);
618620

0 commit comments

Comments
 (0)