Skip to content

Commit 03235d8

Browse files
committed
fix(shared): release relay install locks on cancellation
1 parent 0d34579 commit 03235d8

2 files changed

Lines changed: 140 additions & 3 deletions

File tree

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

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ 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 Encoding from "effect/Encoding";
78
import * as FileSystem from "effect/FileSystem";
9+
import * as Fiber from "effect/Fiber";
810
import * as Layer from "effect/Layer";
911
import * as Sink from "effect/Sink";
1012
import * as Stream from "effect/Stream";
@@ -70,6 +72,134 @@ const makeSpawnerLayer = (commands: Array<string>) =>
7072
);
7173

7274
describe("RelayClient", () => {
75+
it.effect("cancels a contended install without removing the other installer's lock", () =>
76+
Effect.gen(function* () {
77+
const fileSystem = yield* FileSystem.FileSystem;
78+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
79+
const directory = `${baseDir}/tools/cloudflared/${CLOUDFLARED_VERSION}/linux-x64`;
80+
const lockPath = `${directory}/cloudflared.lock`;
81+
yield* fileSystem.makeDirectory(directory, { recursive: true });
82+
yield* fileSystem.writeFileString(lockPath, "other-installer");
83+
const contended = yield* Deferred.make<void>();
84+
const manager = yield* makeCloudflaredRelayClient({ baseDir }).pipe(
85+
Effect.provideService(FileSystem.FileSystem, {
86+
...fileSystem,
87+
writeFileString: (path, contents, options) =>
88+
fileSystem
89+
.writeFileString(path, contents, options)
90+
.pipe(
91+
Effect.tapError(() =>
92+
path === lockPath ? Deferred.succeed(contended, undefined) : Effect.void,
93+
),
94+
),
95+
}),
96+
);
97+
const installing = yield* manager.install.pipe(Effect.forkChild);
98+
yield* Deferred.await(contended);
99+
yield* Fiber.interrupt(installing);
100+
expect(yield* fileSystem.readFileString(lockPath)).toBe("other-installer");
101+
}).pipe(
102+
Effect.scoped,
103+
Effect.provide(
104+
Layer.mergeAll(
105+
NodeServices.layer,
106+
makeHttpClientLayer(new Uint8Array()),
107+
makeSpawnerLayer([]),
108+
hostRuntimeLayer(),
109+
),
110+
),
111+
),
112+
);
113+
114+
it.effect("releases the install lock when a download is cancelled", () =>
115+
Effect.gen(function* () {
116+
const fileSystem = yield* FileSystem.FileSystem;
117+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
118+
const lockPath = `${baseDir}/tools/cloudflared/${CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`;
119+
const downloading = yield* Deferred.make<void>();
120+
const manager = yield* makeCloudflaredRelayClient({ baseDir });
121+
const installing = yield* manager
122+
.installWithProgress((event) =>
123+
event.type === "progress" && event.stage === "downloading"
124+
? Deferred.succeed(downloading, undefined).pipe(Effect.andThen(Effect.never))
125+
: Effect.void,
126+
)
127+
.pipe(Effect.forkChild);
128+
yield* Deferred.await(downloading);
129+
expect(yield* fileSystem.exists(lockPath)).toBe(true);
130+
yield* Fiber.interrupt(installing);
131+
expect(yield* fileSystem.exists(lockPath)).toBe(false);
132+
}).pipe(
133+
Effect.scoped,
134+
Effect.provide(
135+
Layer.mergeAll(
136+
NodeServices.layer,
137+
makeHttpClientLayer(new Uint8Array()),
138+
makeSpawnerLayer([]),
139+
hostRuntimeLayer(),
140+
),
141+
),
142+
),
143+
);
144+
145+
it.effect.skipIf(windowsHost)("releases a lock acquired while installation is cancelled", () =>
146+
Effect.gen(function* () {
147+
const fileSystem = yield* FileSystem.FileSystem;
148+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cloudflared-test-" });
149+
const lockPath = `${baseDir}/tools/cloudflared/${CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`;
150+
const acquired = yield* Deferred.make<void>();
151+
const completeWrite = yield* Deferred.make<void>();
152+
let pauseWrite = true;
153+
const manager = yield* makeCloudflaredRelayClient({
154+
baseDir,
155+
releaseAsset: {
156+
url: "https://example.test/cloudflared",
157+
sha256: Encoding.encodeHex(sha256(new TextEncoder().encode("test-binary"))),
158+
archive: "binary",
159+
},
160+
}).pipe(
161+
Effect.provideService(FileSystem.FileSystem, {
162+
...fileSystem,
163+
writeFileString: (path, contents, options) =>
164+
fileSystem
165+
.writeFileString(path, contents, options)
166+
.pipe(
167+
Effect.tap(() =>
168+
path === lockPath && pauseWrite
169+
? Deferred.succeed(acquired, undefined).pipe(
170+
Effect.andThen(Deferred.await(completeWrite)),
171+
)
172+
: Effect.void,
173+
),
174+
),
175+
}),
176+
);
177+
178+
const installing = yield* manager.install.pipe(Effect.forkChild);
179+
yield* Deferred.await(acquired);
180+
const cancelling = yield* Fiber.interrupt(installing).pipe(
181+
Effect.forkChild({ startImmediately: true }),
182+
);
183+
yield* Deferred.succeed(completeWrite, undefined);
184+
yield* Fiber.join(cancelling);
185+
expect(yield* fileSystem.exists(lockPath)).toBe(false);
186+
187+
pauseWrite = false;
188+
expect(yield* manager.install).toMatchObject({ status: "available" });
189+
expect(yield* fileSystem.exists(lockPath)).toBe(false);
190+
}).pipe(
191+
Effect.scoped,
192+
Effect.provide(
193+
Layer.mergeAll(
194+
NodeServices.layer,
195+
makeHttpClientLayer(new TextEncoder().encode("test-binary")),
196+
makeSpawnerLayer([]),
197+
hostRuntimeLayer(),
198+
),
199+
),
200+
),
201+
);
202+
73203
it.effect.skipIf(windowsHost)(
74204
"resolves explicit overrides before managed and PATH executables",
75205
() =>
@@ -190,6 +320,11 @@ describe("RelayClient", () => {
190320
const error = yield* manager.install.pipe(Effect.flip);
191321
expect(error).toBeInstanceOf(RelayClientInstallError);
192322
expect(error.reason).toBe("invalid_checksum");
323+
expect(
324+
yield* fileSystem.exists(
325+
`${baseDir}/tools/cloudflared/${CLOUDFLARED_VERSION}/linux-x64/cloudflared.lock`,
326+
),
327+
).toBe(false);
193328
}).pipe(
194329
Effect.scoped,
195330
Effect.provide(

‎packages/shared/src/relayClient.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
329329
lockPath: string,
330330
) {
331331
for (let attempt = 0; attempt < INSTALL_LOCK_RETRY_COUNT; attempt += 1) {
332-
const acquired = yield* fileSystem.writeFileString(lockPath, "", { flag: "wx" }).pipe(
332+
const acquired = yield* Effect.acquireRelease(
333+
fileSystem.writeFileString(lockPath, "", { flag: "wx" }),
334+
() => fileSystem.remove(lockPath, { force: true }).pipe(Effect.ignore),
335+
).pipe(
333336
Effect.as(true),
334337
Effect.catch((error) =>
335338
isAlreadyExists(error) ? Effect.succeed(false) : Effect.fail(error),
@@ -444,7 +447,6 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
444447
} satisfies AvailableRelayClient;
445448
}).pipe(
446449
Effect.scoped,
447-
Effect.ensuring(fileSystem.remove(lockPath, { force: true }).pipe(Effect.ignore)),
448450
Effect.catch((cause) =>
449451
cause instanceof RelayClientInstallError
450452
? Effect.fail(cause)
@@ -465,7 +467,7 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
465467
type: "progress",
466468
stage,
467469
}),
468-
),
470+
).pipe(Effect.scoped),
469471
);
470472
const install = installWithProgress(() => Effect.void);
471473

0 commit comments

Comments
 (0)