Skip to content

Commit 004f7a7

Browse files
fix(server): interrupted SSH device bootstrap no longer leaks its forward (pingdotgg#35928)
1 parent ed5afae commit 004f7a7

2 files changed

Lines changed: 105 additions & 4 deletions

File tree

‎apps/server/src/device/SshDeviceHost.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { expect, it } from "@effect/vitest";
22
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
44
import * as Net from "@t3tools/shared/Net";
5+
import * as Deferred from "effect/Deferred";
56
import * as Effect from "effect/Effect";
7+
import * as Fiber from "effect/Fiber";
68
import * as FileSystem from "effect/FileSystem";
79
import * as Layer from "effect/Layer";
810
import * as PlatformError from "effect/PlatformError";
@@ -196,6 +198,97 @@ it.effect("preserves installed status after probes and cleans failed agent activ
196198
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
197199
);
198200

201+
it.effect("releases the forward of a bootstrap interrupted before its hub answered", () =>
202+
Effect.gen(function* () {
203+
const fs = yield* FileSystem.FileSystem;
204+
const home = yield* fs.makeTempDirectoryScoped();
205+
const probing = yield* Deferred.make<void>();
206+
let holdReadiness = true;
207+
let forwards = 0;
208+
const spawner = ChildProcessSpawner.make((command) =>
209+
Effect.gen(function* () {
210+
const forwarding = command._tag === "StandardCommand" && command.args.includes("-L");
211+
if (forwarding) {
212+
forwards++;
213+
yield* Effect.addFinalizer(() =>
214+
Effect.sync(() => {
215+
forwards--;
216+
}),
217+
);
218+
}
219+
const output = forwarding
220+
? ""
221+
: JSON.stringify({
222+
nodePath: "/node",
223+
platforms: [{ platform: "ios", available: true }],
224+
hubPort: 1234,
225+
helpers: { serveSimAxSettings: null, serveSimCli: null },
226+
});
227+
return ChildProcessSpawner.makeHandle({
228+
pid: ChildProcessSpawner.ProcessId(123),
229+
stdout: Stream.make(new TextEncoder().encode(output)),
230+
stderr: Stream.empty,
231+
all: Stream.empty,
232+
exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)),
233+
isRunning: Effect.succeed(forwarding),
234+
kill: () => Effect.void,
235+
stdin: Sink.drain,
236+
getInputFd: () => Sink.drain,
237+
getOutputFd: () => Stream.empty,
238+
unref: Effect.succeed(Effect.void),
239+
});
240+
}),
241+
);
242+
const publishing = yield* Deferred.make<void>();
243+
let holdReadyStatus = false;
244+
const host = yield* SshDeviceHost.make(
245+
{
246+
kind: "ssh",
247+
id: "test",
248+
label: "Test",
249+
target: "test.example",
250+
},
251+
undefined,
252+
(status) =>
253+
status === "ready" && holdReadyStatus
254+
? Deferred.succeed(publishing, undefined).pipe(Effect.andThen(Effect.never))
255+
: Effect.void,
256+
).pipe(
257+
Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
258+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
259+
Effect.provideService(
260+
HttpClient.HttpClient,
261+
HttpClient.make((request) =>
262+
Effect.gen(function* () {
263+
if (holdReadiness) {
264+
yield* Deferred.succeed(probing, undefined);
265+
return yield* Effect.never;
266+
}
267+
return HttpClientResponse.fromWeb(request, new Response("ok"));
268+
}),
269+
),
270+
),
271+
);
272+
const first = yield* host.ensureReady(() => Effect.void).pipe(Effect.forkChild);
273+
yield* Deferred.await(probing);
274+
yield* Fiber.interrupt(first);
275+
expect(forwards).toBe(0);
276+
holdReadiness = false;
277+
yield* host.ensureReady(() => Effect.void);
278+
yield* host.stop;
279+
expect(forwards).toBe(0);
280+
holdReadyStatus = true;
281+
const second = yield* host.ensureReady(() => Effect.void).pipe(Effect.forkChild);
282+
yield* Deferred.await(publishing);
283+
yield* Fiber.interrupt(second);
284+
holdReadyStatus = false;
285+
yield* host.ensureReady(() => Effect.void);
286+
expect(forwards).toBe(1);
287+
yield* host.stop;
288+
expect(forwards).toBe(0);
289+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
290+
);
291+
199292
it.effect("lists Android devices without probing the host again once it reports Android", () =>
200293
Effect.gen(function* () {
201294
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/device/SshDeviceHost.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,21 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
473473
return next;
474474
});
475475

476+
const closeConnection = Effect.suspend(() => {
477+
const failedScope = connectionScope;
478+
connectionScope = null;
479+
ready = null;
480+
return failedScope ? Scope.close(failedScope, Exit.void) : Effect.void;
481+
});
482+
476483
const connect = Effect.fn("SshDeviceHost.connect")(function* () {
477484
for (let attempt = 0; ; attempt++) {
478-
const result = yield* connectOnce().pipe(Effect.result);
485+
const result = yield* connectOnce().pipe(
486+
Effect.onInterrupt(() => closeConnection),
487+
Effect.result,
488+
);
479489
if (result._tag === "Success") return result.success;
480-
const failedScope = connectionScope;
481-
connectionScope = null;
482-
if (failedScope) yield* Scope.close(failedScope, Exit.void);
490+
yield* closeConnection;
483491
// SSH binds after the reservation is released, so a competing bind needs fresh ports.
484492
if (
485493
attempt >= 2 ||

0 commit comments

Comments
 (0)