From 9f16e152403c6c22fbbf7d21c715ce9d4c38dad4 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 7 Jun 2026 16:10:57 +0000 Subject: [PATCH] Use Effect durations in SSH timeouts Co-authored-by: Julius Marminge --- packages/ssh/src/command.test.ts | 2 +- packages/ssh/src/command.ts | 18 ++++++--- packages/ssh/src/tunnel.test.ts | 6 +-- packages/ssh/src/tunnel.ts | 66 ++++++++++++++++++-------------- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/packages/ssh/src/command.test.ts b/packages/ssh/src/command.test.ts index e5b621f87a..3f3afab198 100644 --- a/packages/ssh/src/command.test.ts +++ b/packages/ssh/src/command.test.ts @@ -219,7 +219,7 @@ describe("ssh command", () => { username: "julius", port: 2222, }, - { timeoutMs: 1 }, + { timeout: Duration.millis(1) }, ), ), ); diff --git a/packages/ssh/src/command.ts b/packages/ssh/src/command.ts index 44cc047aae..7a447a2f37 100644 --- a/packages/ssh/src/command.ts +++ b/packages/ssh/src/command.ts @@ -14,7 +14,7 @@ import { buildSshChildEnvironment, type SshAuthOptions } from "./auth.ts"; import { SshCommandError, SshInvalidTargetError } from "./errors.ts"; const PUBLISHABLE_T3_VERSION_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/u; -const DEFAULT_SSH_COMMAND_TIMEOUT_MS = 60_000; +const DEFAULT_SSH_COMMAND_TIMEOUT = Duration.minutes(1); const MAX_SSH_ERROR_OUTPUT_LENGTH = 4_000; export const SSH_COMMAND = process.platform === "win32" ? "ssh.exe" : "ssh"; @@ -29,7 +29,7 @@ export interface RunSshCommandOptions extends SshAuthOptions { readonly preHostArgs?: ReadonlyArray; readonly remoteCommandArgs?: ReadonlyArray; readonly stdin?: string; - readonly timeoutMs?: number; + readonly timeout?: Duration.Input; } export function parseSshResolveOutput(alias: string, stdout: string): DesktopSshEnvironmentTarget { @@ -157,6 +157,10 @@ function stdinStream(input: string | undefined) { return input === undefined ? Stream.empty : Stream.make(encoder.encode(input)); } +function resolveSshCommandTimeout(input: RunSshCommandOptions): Duration.Duration { + return Duration.fromInputUnsafe(input.timeout ?? DEFAULT_SSH_COMMAND_TIMEOUT); +} + const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(function* ( target: DesktopSshEnvironmentTarget, input: RunSshCommandOptions, @@ -195,7 +199,7 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func ...sshTargetLogFields(target), command: [SSH_COMMAND, ...args], hasStdin: input.stdin !== undefined, - timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS, + timeoutMs: Duration.toMillis(resolveSshCommandTimeout(input)), }); const child = yield* spawner .spawn( @@ -282,10 +286,12 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* ( SshCommandError | SshInvalidTargetError, ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path > { + const timeout = resolveSshCommandTimeout(input); + const timeoutMs = Duration.toMillis(timeout); return yield* Effect.scopedWith((commandScope) => runSshCommandInScope(target, input, commandScope), ).pipe( - Effect.timeoutOption(Duration.millis(input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS)), + Effect.timeoutOption(timeout), Effect.flatMap((result) => Option.match(result, { onSome: Effect.succeed, @@ -293,7 +299,7 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* ( Effect.gen(function* () { yield* Effect.logWarning("ssh.command.timedOut", { ...sshTargetLogFields(target), - timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS, + timeoutMs, remoteCommandArgs: input.remoteCommandArgs ?? [], preHostArgs: input.preHostArgs ?? [], hasStdin: input.stdin !== undefined, @@ -302,7 +308,7 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* ( command: ["ssh"], exitCode: null, stderr: "", - message: `SSH command timed out after ${input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS}ms.`, + message: `SSH command timed out after ${timeoutMs}ms.`, }); }), }), diff --git a/packages/ssh/src/tunnel.test.ts b/packages/ssh/src/tunnel.test.ts index 2e5c1a6990..ca44594143 100644 --- a/packages/ssh/src/tunnel.test.ts +++ b/packages/ssh/src/tunnel.test.ts @@ -242,9 +242,9 @@ describe("ssh tunnel scripts", () => { Effect.result( waitForHttpReady({ baseUrl: "http://127.0.0.1:41773/", - timeoutMs: 1_000, - intervalMs: 100, - probeTimeoutMs: 250, + timeout: { seconds: 1 }, + interval: Duration.millis(100), + probeTimeout: { milliseconds: 250 }, }), ), ); diff --git a/packages/ssh/src/tunnel.ts b/packages/ssh/src/tunnel.ts index 029b764489..0ec309af23 100644 --- a/packages/ssh/src/tunnel.ts +++ b/packages/ssh/src/tunnel.ts @@ -51,11 +51,13 @@ import { export const DEFAULT_REMOTE_PORT = 3773; const REMOTE_PORT_SCAN_WINDOW = 200; -const SSH_READY_TIMEOUT_MS = 20_000; -const SSH_READY_PROBE_TIMEOUT_MS = 1_000; -const TUNNEL_SHUTDOWN_TIMEOUT_MS = 2_000; -const REMOTE_READY_TIMEOUT_MS = 15_000; -const REMOTE_REUSE_READY_TIMEOUT_MS = 2_000; +const SSH_READY_TIMEOUT = Duration.seconds(20); +const SSH_READY_PROBE_TIMEOUT = Duration.seconds(1); +const TUNNEL_SHUTDOWN_TIMEOUT = Duration.seconds(2); +const REMOTE_READY_TIMEOUT = Duration.seconds(15); +const REMOTE_REUSE_READY_TIMEOUT = Duration.seconds(2); +const DEFAULT_HTTP_READY_TIMEOUT = Duration.seconds(30); +const DEFAULT_HTTP_READY_INTERVAL = Duration.millis(100); export interface RemoteT3RunnerOptions { readonly packageSpec?: string; @@ -682,9 +684,9 @@ export function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string { T3_WAIT_READY_SCRIPT: stripTrailingNewlines(REMOTE_WAIT_READY_SCRIPT), T3_DEFAULT_REMOTE_PORT: String(DEFAULT_REMOTE_PORT), T3_REMOTE_PORT_SCAN_WINDOW: String(REMOTE_PORT_SCAN_WINDOW), - T3_READY_TIMEOUT_MS: String(REMOTE_READY_TIMEOUT_MS), - T3_REUSE_READY_TIMEOUT_MS: String(REMOTE_REUSE_READY_TIMEOUT_MS), - T3_READY_PROBE_TIMEOUT_MS: String(SSH_READY_PROBE_TIMEOUT_MS), + T3_READY_TIMEOUT_MS: String(Duration.toMillis(REMOTE_READY_TIMEOUT)), + T3_REUSE_READY_TIMEOUT_MS: String(Duration.toMillis(REMOTE_REUSE_READY_TIMEOUT)), + T3_READY_PROBE_TIMEOUT_MS: String(Duration.toMillis(SSH_READY_PROBE_TIMEOUT)), }); } @@ -856,7 +858,7 @@ const readRemoteServerLogTail = Effect.fn("ssh/tunnel.readRemoteServerLogTail")( const result = yield* runSshCommand(target, { remoteCommandArgs: ["sh", "-s"], stdin: buildRemoteLogTailScript(target), - timeoutMs: 10_000, + timeout: Duration.seconds(10), ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }), ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }), ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }), @@ -866,20 +868,23 @@ const readRemoteServerLogTail = Effect.fn("ssh/tunnel.readRemoteServerLogTail")( export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(function* (input: { readonly baseUrl: string; - readonly timeoutMs?: number; - readonly intervalMs?: number; - readonly probeTimeoutMs?: number; + readonly timeout?: Duration.Input; + readonly interval?: Duration.Input; + readonly probeTimeout?: Duration.Input; readonly path?: string; }): Effect.fn.Return { - const timeoutMs = input.timeoutMs ?? 30_000; - const intervalMs = input.intervalMs ?? 100; - const probeTimeoutMs = input.probeTimeoutMs ?? SSH_READY_PROBE_TIMEOUT_MS; - const retryPolicy = Schedule.spaced(Duration.millis(intervalMs)).pipe( + const timeout = Duration.fromInputUnsafe(input.timeout ?? DEFAULT_HTTP_READY_TIMEOUT); + const interval = Duration.fromInputUnsafe(input.interval ?? DEFAULT_HTTP_READY_INTERVAL); + const probeTimeout = Duration.fromInputUnsafe(input.probeTimeout ?? SSH_READY_PROBE_TIMEOUT); + const timeoutMs = Duration.toMillis(timeout); + const intervalMs = Duration.toMillis(interval); + const probeTimeoutMs = Duration.toMillis(probeTimeout); + const retryPolicy = Schedule.spaced(interval).pipe( Schedule.take(Math.max(0, Math.ceil(timeoutMs / intervalMs))), ); const requestUrl = new URL(input.path ?? "/", input.baseUrl).toString(); const client = yield* HttpClient.HttpClient; - const lastProbeFailure = yield* Ref.make(null); + const lastProbeFailure = yield* Ref.make>(Option.none()); let attempt = 0; yield* Effect.logDebug("ssh.tunnel.httpReady.start", { @@ -896,7 +901,7 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio Effect.gen(function* () { attempt += 1; const responseOption = yield* effect.pipe( - Effect.timeoutOption(Duration.millis(probeTimeoutMs)), + Effect.timeoutOption(probeTimeout), Effect.mapError( (cause) => new SshReadinessError({ @@ -929,10 +934,13 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio }), ), Effect.tapError((cause) => - Ref.set(lastProbeFailure, { - attempt, - cause: describeReadinessCause(cause), - }), + Ref.set( + lastProbeFailure, + Option.some({ + attempt, + cause: describeReadinessCause(cause), + }), + ), ), ), ), @@ -949,7 +957,7 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio cause, }), ), - Effect.timeoutOption(Duration.millis(timeoutMs)), + Effect.timeoutOption(timeout), ); return yield* Option.match(result, { @@ -969,11 +977,11 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio intervalMs, probeTimeoutMs, attempts: attempt, - lastFailure, + ...(Option.isSome(lastFailure) ? { lastFailure: lastFailure.value } : {}), }); return yield* new SshReadinessError({ message: `Timed out waiting ${timeoutMs}ms for backend readiness at ${input.baseUrl}.`, - cause: lastFailure, + cause: Option.getOrUndefined(lastFailure), }); }), }); @@ -1166,7 +1174,7 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input: yield* Effect.raceFirst( waitForHttpReady({ baseUrl: input.httpBaseUrl, - timeoutMs: SSH_READY_TIMEOUT_MS, + timeout: SSH_READY_TIMEOUT, }), exitFailure, ).pipe( @@ -1226,7 +1234,7 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input: : child .kill({ killSignal: "SIGTERM", - forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT_MS, + forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT, }) .pipe(Effect.ignore), ), @@ -1470,7 +1478,7 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma [ tunnelEntry.process.kill({ killSignal: "SIGTERM", - forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT_MS, + forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT, }), stopRemoteServer( tunnelEntry.target, @@ -1524,7 +1532,7 @@ const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.ma remotePort: entry.remotePort, }); const readinessExit = yield* Effect.exit( - waitForHttpReady({ baseUrl: entry.httpBaseUrl, timeoutMs: 2_000 }), + waitForHttpReady({ baseUrl: entry.httpBaseUrl, timeout: Duration.seconds(2) }), ); if (Exit.isSuccess(readinessExit)) { yield* Effect.logDebug("ssh.environment.tunnel.reused", {