Skip to content

Commit 6bc3726

Browse files
fix(ssh): a slow reconnect no longer replaces a healthy port forward (pingdotgg#35874)
1 parent e726a08 commit 6bc3726

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,22 +681,31 @@ describe("ssh tunnel scripts", () => {
681681
}),
682682
);
683683

684-
it.effect("connects through a tunnel whose responses take longer than a second", () =>
684+
it.effect("connects and reuses a tunnel whose responses take over a second", () =>
685685
Effect.gen(function* () {
686686
// Over a ~1s RTT link each request through the forward takes a few round trips.
687687
const RESPONSE_MS = 2_500;
688688
const tunnelSpawned = yield* Deferred.make<void>();
689+
const reconnectProbed = yield* Deferred.make<void>();
690+
const forwardReplaced = yield* Deferred.make<void>();
691+
let forwards = 0;
692+
let connected = false;
689693
const spawner = ChildProcessSpawner.make((command) =>
690694
commandArgs(command).includes("-N")
691-
? Deferred.succeed(tunnelSpawned, undefined).pipe(
695+
? Effect.sync(() => ++forwards).pipe(
696+
Effect.flatMap((count) =>
697+
Deferred.succeed(count === 1 ? tunnelSpawned : forwardReplaced, undefined),
698+
),
692699
Effect.as(makeRunningProcess(() => undefined)),
693700
)
694701
: Effect.succeed(makeSuccessfulProcess('{"remotePort":3773}\n')),
695702
);
696703
const httpClient = HttpClient.make((request) =>
697-
Effect.sleep(Duration.millis(RESPONSE_MS)).pipe(
698-
Effect.as(HttpClientResponse.fromWeb(request, new Response("", { status: 200 }))),
699-
),
704+
Effect.gen(function* () {
705+
if (connected) yield* Deferred.succeed(reconnectProbed, undefined);
706+
yield* Effect.sleep(Duration.millis(RESPONSE_MS));
707+
return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
708+
}),
700709
);
701710
const layer = Layer.mergeAll(
702711
NodeServices.layer,
@@ -726,6 +735,17 @@ describe("ssh tunnel scripts", () => {
726735
Result.isSuccess(result),
727736
Result.isFailure(result) ? result.failure.message : undefined,
728737
);
738+
if (Result.isFailure(result)) return;
739+
740+
connected = true;
741+
const reconnect = yield* Effect.forkChild(manager.ensureEnvironment(result.success.target));
742+
yield* Deferred.await(reconnectProbed);
743+
yield* TestClock.adjust(Duration.seconds(60));
744+
const outcome = yield* Effect.raceFirst(
745+
Fiber.join(reconnect).pipe(Effect.map((environment) => environment.httpBaseUrl)),
746+
Deferred.await(forwardReplaced).pipe(Effect.as("replaced the forward")),
747+
);
748+
assert.equal(outcome, result.success.httpBaseUrl);
729749
}).pipe(Effect.provide(layer), Effect.scoped);
730750
}),
731751
);

‎packages/ssh/src/tunnel.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,10 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
18591859
remotePort: entry.remotePort,
18601860
});
18611861
const readinessExit = yield* Effect.exit(
1862-
waitForHttpReady({ baseUrl: entry.httpBaseUrl, timeoutMs: 2_000 }),
1862+
waitForHttpReady({
1863+
baseUrl: entry.httpBaseUrl,
1864+
timeoutMs: SSH_TUNNEL_READY_PROBE_TIMEOUT_MS,
1865+
}),
18631866
);
18641867
if (Exit.isSuccess(readinessExit)) {
18651868
yield* Effect.logDebug("ssh.environment.tunnel.reused", {

0 commit comments

Comments
 (0)