Skip to content

Commit 4d9caf1

Browse files
fix(ssh): tunnel readiness no longer counts ssh's connection time (#33554)
1 parent 4168c14 commit 4d9caf1

2 files changed

Lines changed: 169 additions & 7 deletions

File tree

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { assert, describe, 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 NetService from "@t3tools/shared/Net";
5+
import * as Clock from "effect/Clock";
56
import * as Deferred from "effect/Deferred";
67
import * as Duration from "effect/Duration";
78
import * as Effect from "effect/Effect";
@@ -13,11 +14,11 @@ import * as Result from "effect/Result";
1314
import * as Sink from "effect/Sink";
1415
import * as Stream from "effect/Stream";
1516
import { TestClock } from "effect/testing";
16-
import { HttpClient, HttpClientResponse } from "effect/http";
17+
import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
1718
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1819

1920
import * as SshAuth from "./auth.ts";
20-
import { SshCommandError, SshPasswordPromptError } from "./errors.ts";
21+
import { SshCommandError, SshPasswordPromptError, SshReadinessError } from "./errors.ts";
2122
import * as SshTunnel from "./tunnel.ts";
2223

2324
const isSshCommandError = Schema.is(SshCommandError);
@@ -74,7 +75,7 @@ const hangingHttpClient = HttpClient.make(() => Effect.never);
7475
const testNetService = NetService.NetService.of({
7576
canListenOnHost: () => Effect.succeed(true),
7677
isPortAvailableOnLoopback: () => Effect.succeed(true),
77-
hasListenerOnHost: () => Effect.succeed(false),
78+
hasListenerOnHost: () => Effect.succeed(true),
7879
reserveLoopbackPort: () => Effect.succeed(41_773),
7980
findAvailablePort: (preferred) => Effect.succeed(preferred),
8081
});
@@ -272,6 +273,135 @@ describe("ssh tunnel scripts", () => {
272273
}),
273274
);
274275

276+
it.effect("waits for a tunnel whose ssh connection outlasts the readiness timeout", () =>
277+
Effect.gen(function* () {
278+
const LOCAL_PORT = 41_773;
279+
// ssh spends ConnectTimeout on each unreachable address before the forward listens.
280+
const SSH_CONNECT_MS = 25_000;
281+
let forwardListensAt = Number.POSITIVE_INFINITY;
282+
const tunnelSpawned = yield* Deferred.make<void>();
283+
const forwardListening = (port: number) =>
284+
Clock.currentTimeMillis.pipe(
285+
Effect.map((now) => port === LOCAL_PORT && now >= forwardListensAt),
286+
);
287+
const spawner = ChildProcessSpawner.make((command) =>
288+
commandArgs(command).includes("-N")
289+
? Effect.gen(function* () {
290+
forwardListensAt = (yield* Clock.currentTimeMillis) + SSH_CONNECT_MS;
291+
yield* Deferred.succeed(tunnelSpawned, undefined);
292+
return makeRunningProcess(() => undefined);
293+
})
294+
: Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
295+
);
296+
const httpClient = HttpClient.make((request) =>
297+
forwardListening(LOCAL_PORT).pipe(
298+
Effect.flatMap((listening) =>
299+
listening
300+
? Effect.succeed(
301+
HttpClientResponse.fromWeb(request, new Response("", { status: 200 })),
302+
)
303+
: Effect.fail(
304+
new HttpClientError.HttpClientError({
305+
reason: new HttpClientError.TransportError({
306+
request,
307+
description: `connect ECONNREFUSED 127.0.0.1:${LOCAL_PORT}`,
308+
}),
309+
}),
310+
),
311+
),
312+
),
313+
);
314+
const net = NetService.NetService.of({
315+
...testNetService,
316+
reserveLoopbackPort: () => Effect.succeed(LOCAL_PORT),
317+
canListenOnHost: (port) =>
318+
forwardListening(port).pipe(Effect.map((listening) => !listening)),
319+
isPortAvailableOnLoopback: (port) =>
320+
forwardListening(port).pipe(Effect.map((listening) => !listening)),
321+
hasListenerOnHost: (port) => forwardListening(port),
322+
});
323+
const layer = Layer.mergeAll(
324+
NodeServices.layer,
325+
NodeCrypto.layer,
326+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
327+
Layer.succeed(HttpClient.HttpClient, httpClient),
328+
Layer.succeed(NetService.NetService, net),
329+
SshAuth.SshPasswordPrompt.disabledLayer,
330+
SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
331+
);
332+
yield* Effect.gen(function* () {
333+
const manager = yield* SshTunnel.SshEnvironmentManager;
334+
const ensure = yield* Effect.forkChild(
335+
manager.ensureEnvironment({
336+
alias: "devbox",
337+
hostname: "devbox",
338+
username: null,
339+
port: null,
340+
}),
341+
);
342+
yield* Deferred.await(tunnelSpawned);
343+
yield* TestClock.adjust(Duration.seconds(60));
344+
const environment = yield* Fiber.join(ensure);
345+
assert.equal(environment.httpBaseUrl, `http://127.0.0.1:${LOCAL_PORT}/`);
346+
}).pipe(Effect.provide(layer), Effect.scoped);
347+
}),
348+
);
349+
350+
it.effect("fails with a readiness error when ssh never connects", () =>
351+
Effect.gen(function* () {
352+
const tunnelSpawned = yield* Deferred.make<void>();
353+
let tunnelKilled = false;
354+
const spawner = ChildProcessSpawner.make((command) =>
355+
commandArgs(command).includes("-N")
356+
? Deferred.succeed(tunnelSpawned, undefined).pipe(
357+
Effect.as(
358+
makeRunningProcess(() => {
359+
tunnelKilled = true;
360+
}),
361+
),
362+
)
363+
: Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
364+
);
365+
const layer = Layer.mergeAll(
366+
NodeServices.layer,
367+
NodeCrypto.layer,
368+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
369+
Layer.succeed(HttpClient.HttpClient, hangingHttpClient),
370+
Layer.succeed(
371+
NetService.NetService,
372+
NetService.NetService.of({
373+
...testNetService,
374+
hasListenerOnHost: () => Effect.succeed(false),
375+
}),
376+
),
377+
SshAuth.SshPasswordPrompt.disabledLayer,
378+
SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
379+
);
380+
yield* Effect.gen(function* () {
381+
const manager = yield* SshTunnel.SshEnvironmentManager;
382+
const ensure = yield* Effect.forkChild(
383+
Effect.result(
384+
manager.ensureEnvironment({
385+
alias: "devbox",
386+
hostname: "devbox",
387+
username: null,
388+
port: null,
389+
}),
390+
),
391+
);
392+
yield* Deferred.await(tunnelSpawned);
393+
yield* TestClock.adjust(Duration.minutes(2));
394+
const result = yield* Fiber.join(ensure);
395+
assert.isTrue(Result.isFailure(result));
396+
if (Result.isFailure(result)) {
397+
assert.instanceOf(result.failure, SshReadinessError);
398+
assert.include(result.failure.message, "SSH connection to devbox did not come up");
399+
}
400+
assert.isTrue(tunnelKilled);
401+
}).pipe(Effect.provide(layer), Effect.scoped);
402+
}),
403+
);
404+
275405
it.effect.each([
276406
{
277407
name: "does not ask for a password when the agent cannot sign an accepted key",

‎packages/ssh/src/tunnel.ts‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import { extractJsonObject, fromLenientJson } from "@t3tools/shared/schemaJson";
1010
import { satisfiesSemverRange } from "@t3tools/shared/semver";
1111
import * as Context from "effect/Context";
1212
import * as Digest from "effect/crypto/Digest";
13+
import * as Duration from "effect/Duration";
1314
import * as Effect from "effect/Effect";
1415
import * as Exit from "effect/Exit";
1516
import * as Fiber from "effect/Fiber";
1617
import * as FileSystem from "effect/FileSystem";
1718
import * as Layer from "effect/Layer";
1819
import * as Path from "effect/Path";
20+
import * as Schedule from "effect/Schedule";
1921
import * as Schema from "effect/Schema";
2022
import * as Scope from "effect/Scope";
2123
import * as Stream from "effect/Stream";
@@ -52,6 +54,9 @@ const TUNNEL_SHUTDOWN_TIMEOUT_MS = 2_000;
5254
const TUNNEL_STDERR_TAIL_CHARS = 16_384;
5355
const REMOTE_READY_TIMEOUT_MS = 60_000;
5456
const REMOTE_LAUNCH_TIMEOUT_MS = 90_000;
57+
// Matches the node-script launch command's whole ssh run against the same host.
58+
const SSH_TUNNEL_CONNECT_TIMEOUT_MS = REMOTE_LAUNCH_TIMEOUT_MS;
59+
const SSH_TUNNEL_FORWARD_PROBE_INTERVAL_MS = 100;
5560
// Cold archive launches download and extract ~70 MB and may wait on an install lock.
5661
// Lock waits must outlast both downloads and extraction; SSH must outlast install or wait plus readiness.
5762
// Leave slack for verification and extraction, which have no timeout.
@@ -1096,6 +1101,29 @@ const reserveLocalTunnelPort = Effect.fn("ssh/tunnel.reserveLocalTunnelPort")(fu
10961101
return yield* net.reserveLoopbackPort();
10971102
});
10981103

1104+
// ssh only binds the forward after it connects, which can take ConnectTimeout per dead address.
1105+
const waitForTunnelForward = Effect.fn("ssh/tunnel.waitForTunnelForward")(function* (input: {
1106+
readonly target: DesktopSshEnvironmentTarget;
1107+
readonly localPort: number;
1108+
}) {
1109+
const net = yield* NetService.NetService;
1110+
yield* net.hasListenerOnHost(input.localPort, "127.0.0.1").pipe(
1111+
Effect.repeat({
1112+
schedule: Schedule.spaced(Duration.millis(SSH_TUNNEL_FORWARD_PROBE_INTERVAL_MS)),
1113+
until: (listening) => listening,
1114+
}),
1115+
Effect.timeoutOrElse({
1116+
duration: Duration.millis(SSH_TUNNEL_CONNECT_TIMEOUT_MS),
1117+
orElse: () =>
1118+
Effect.fail(
1119+
new SshReadinessError({
1120+
message: `SSH connection to ${input.target.alias} did not come up within ${SSH_TUNNEL_CONNECT_TIMEOUT_MS}ms.`,
1121+
}),
1122+
),
1123+
}),
1124+
);
1125+
});
1126+
10991127
const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input: {
11001128
readonly key: string;
11011129
readonly resolvedTarget: DesktopSshEnvironmentTarget;
@@ -1271,10 +1299,14 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
12711299
}),
12721300
);
12731301
yield* Effect.raceFirst(
1274-
waitForHttpReady({
1275-
baseUrl: input.httpBaseUrl,
1276-
timeoutMs: SSH_READY_TIMEOUT_MS,
1277-
}),
1302+
waitForTunnelForward({ target: input.resolvedTarget, localPort: input.localPort }).pipe(
1303+
Effect.andThen(
1304+
waitForHttpReady({
1305+
baseUrl: input.httpBaseUrl,
1306+
timeoutMs: SSH_READY_TIMEOUT_MS,
1307+
}),
1308+
),
1309+
),
12781310
exitFailure,
12791311
).pipe(
12801312
Effect.tap(() =>

0 commit comments

Comments
 (0)