The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
fix(ssh): a slow remote server keeps its SSH forward on reconnect (#39802)
Julius Marminge committed
Nov 20, 2026 at 08:50 UTC
d817e614879e59d072667b9144c017f984edb940
2 files changed
+52
-5
packages/ssh/src/tunnel.test.ts
+18
-4
@@ -832,9 +832,15 @@ describe("ssh tunnel scripts", () => {
832
}),
833
);
834
835
- it.effect("connects and reuses a tunnel whose responses take over a second", () =>
835
+ it.effect.each([
836
+ // Over a ~1s RTT link each request through the forward takes a few round trips.
837
+ { condition: "responses take over a second", reconnectResponseMs: 2_500 },
838
+ // An alive server whose event loop stalls (e.g. on a blocked fsync) answers late.
839
+ { condition: "server stalls for 20 seconds", reconnectResponseMs: 20_000 },
840
+ // ssh closes the forwarded connection when nothing listens on the remote port.
841
+ { condition: "server is gone", reconnectResponseMs: 0, serverGone: true },
842
+ ])("connects and reuses a tunnel whose $condition", ({ reconnectResponseMs, serverGone }) =>
843
Effect.gen(function* () {
837
- // Over a ~1s RTT link each request through the forward takes a few round trips.
844
const RESPONSE_MS = 2_500;
845
const tunnelSpawned = yield* Deferred.make<void>();
846
const reconnectProbed = yield* Deferred.make<void>();
@@ -854,7 +860,15 @@ describe("ssh tunnel scripts", () => {
860
const httpClient = HttpClient.make((request) =>
861
Effect.gen(function* () {
862
if (connected) yield* Deferred.succeed(reconnectProbed, undefined);
857
- yield* Effect.sleep(Duration.millis(RESPONSE_MS));
863
+ yield* Effect.sleep(Duration.millis(connected ? reconnectResponseMs : RESPONSE_MS));
864
+ if (connected && serverGone) {
865
+ return yield* new HttpClientError.HttpClientError({
866
+ reason: new HttpClientError.TransportError({
867
+ request,
868
+ description: "socket hang up",
869
+ }),
870
+ });
871
+ }
872
return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
873
}),
874
);
@@ -896,7 +910,7 @@ describe("ssh tunnel scripts", () => {
910
Fiber.join(reconnect).pipe(Effect.map((environment) => environment.httpBaseUrl)),
911
Deferred.await(forwardReplaced).pipe(Effect.as("replaced the forward")),
912
);
899
- assert.equal(outcome, result.success.httpBaseUrl);
913
+ assert.equal(outcome, serverGone ? "replaced the forward" : result.success.httpBaseUrl);
914
}).pipe(Effect.provide(layer), Effect.scoped);
915
}),
916
);
packages/ssh/src/tunnel.ts
+34
-1
@@ -2063,12 +2063,45 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma
2063
localPort: entry.localPort,
2064
remotePort: entry.remotePort,
2065
});
2066
+ let answered = false;
2067
+ const markAnswered = Effect.sync(() => {
2068
+ answered = true;
2069
+ });
2070
+ const client = yield* HttpClient.HttpClient;
2071
const readinessExit = yield* Effect.exit(
2072
waitForHttpReady({
2073
baseUrl: entry.httpBaseUrl,
2074
timeoutMs: SSH_TUNNEL_READY_PROBE_TIMEOUT_MS,
2070
- }),
2075
+ }).pipe(
2076
+ Effect.provideService(
2077
+ HttpClient.HttpClient,
2078
+ client.pipe(
2079
+ HttpClient.transform((request) =>
2080
+ request.pipe(
2081
+ Effect.tap(() => markAnswered),
2082
+ Effect.tapError(() => markAnswered),
2083
+ ),
2084
+ ),
2085
+ ),
2086
+ ),
2087
+ ),
2088
);
2089
+ // ssh closes the channel when nothing listens remotely, so a forward that
2090
+ // is up but got no answer leads to a live server that is only slow.
2091
+ const keepSlowForward =
2092
+ Exit.isFailure(readinessExit) &&
2093
+ !answered &&
2094
+ (yield* entry.process.isRunning.pipe(Effect.orElseSucceed(() => false))) &&
2095
+ (yield* (yield* NetService.NetService).hasListenerOnHost(entry.localPort, "127.0.0.1"));
2096
+ if (keepSlowForward) {
2097
+ yield* Effect.logWarning("ssh.environment.tunnel.existing.slow", {
2098
+ ...sshTargetLogFields(resolvedTarget),
2099
+ key,
2100
+ localPort: entry.localPort,
2101
+ remotePort: entry.remotePort,
2102
+ });
2103
+ return entry;
2104
+ }
2105
if (Exit.isSuccess(readinessExit)) {
2106
yield* Effect.logDebug("ssh.environment.tunnel.reused", {
2107
...sshTargetLogFields(resolvedTarget),