@devries-cameron20 / t3code-1 / commits / 004f7a768f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): interrupted SSH device bootstrap no longer leaks its forward (#35928)

Julius Marminge committed Nov 12, 2026 at 20:01 UTC 004f7a768f3f6bba8a4c435b076b7c826448d7ae
2 files changed +105 -4
apps/server/src/device/SshDeviceHost.test.ts
+93
@@ -2,7 +2,9 @@ import { expect, it } from "@effect/vitest";
2 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3 import * as NodeServices from "@effect/platform-node/NodeServices";
4 import * as Net from "@t3tools/shared/Net";
5 +import * as Deferred from "effect/Deferred";
6 import * as Effect from "effect/Effect";
7 +import * as Fiber from "effect/Fiber";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Layer from "effect/Layer";
10 import * as PlatformError from "effect/PlatformError";
@@ -196,6 +198,97 @@ it.effect("preserves installed status after probes and cleans failed agent activ
198 }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
199 );
200
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 +
292 it.effect("lists Android devices without probing the host again once it reports Android", () =>
293 Effect.gen(function* () {
294 const fs = yield* FileSystem.FileSystem;
apps/server/src/device/SshDeviceHost.ts
+12 -4
@@ -473,13 +473,21 @@ export const make = Effect.fn("SshDeviceHost.make")(function* (
473 return next;
474 });
475
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 +
483 const connect = Effect.fn("SshDeviceHost.connect")(function* () {
484 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 + );
489 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;
491 // SSH binds after the reservation is released, so a competing bind needs fresh ports.
492 if (
493 attempt >= 2 ||