Skip to content

Commit 3e1cb74

Browse files
saphidclaude
andcommitted
fix(ssh): keep a failed diagnostics read from hiding the tunnel exit
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2261ed5 commit 3e1cb74

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Fiber from "effect/Fiber";
1111
import * as FileSystem from "effect/FileSystem";
1212
import * as Layer from "effect/Layer";
1313
import * as Option from "effect/Option";
14+
import * as PlatformError from "effect/PlatformError";
1415
import * as Queue from "effect/Queue";
1516
import * as Result from "effect/Result";
1617
import * as Sink from "effect/Sink";
@@ -747,6 +748,61 @@ describe("ssh tunnel scripts", () => {
747748
}),
748749
);
749750

751+
it.effect("reports the real exit when reading tunnel diagnostics fails", () =>
752+
Effect.gen(function* () {
753+
const diagnostic = "Permission denied (publickey)";
754+
const spawner = ChildProcessSpawner.make((command) =>
755+
Effect.succeed(
756+
commandArgs(command).includes("-N")
757+
? {
758+
...makeSuccessfulProcess(""),
759+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(255)),
760+
stderr: Stream.make(new TextEncoder().encode(diagnostic)).pipe(
761+
Stream.concat(
762+
Stream.fail(
763+
PlatformError.systemError({
764+
_tag: "Unknown",
765+
module: "Stream",
766+
method: "read",
767+
description: "stderr read failed",
768+
}),
769+
),
770+
),
771+
),
772+
}
773+
: makeSuccessfulProcess('{"remotePort":3773}\n'),
774+
),
775+
);
776+
const layer = Layer.mergeAll(
777+
NodeServices.layer,
778+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
779+
Layer.succeed(HttpClient.HttpClient, hangingHttpClient),
780+
Layer.succeed(NetService.NetService, testNetService),
781+
SshAuth.SshPasswordPrompt.disabledLayer,
782+
SshTunnel.SshEnvironmentManager.layer({ resolveCliRunner: Effect.succeed(ARCHIVE) }),
783+
);
784+
yield* Effect.gen(function* () {
785+
const manager = yield* SshTunnel.SshEnvironmentManager;
786+
const result = yield* Effect.result(
787+
manager.ensureEnvironment({
788+
alias: "devbox",
789+
hostname: "devbox",
790+
username: null,
791+
port: null,
792+
}),
793+
);
794+
assert.isTrue(Result.isFailure(result));
795+
if (Result.isFailure(result)) {
796+
assert.instanceOf(result.failure, SshCommandError);
797+
if (result.failure instanceof SshCommandError) {
798+
assert.equal(result.failure.exitCode, 255);
799+
assert.equal(result.failure.stderr, diagnostic);
800+
}
801+
}
802+
}).pipe(Effect.provide(layer), Effect.scoped);
803+
}),
804+
);
805+
750806
it.live("drains diagnostics beyond pipe capacity after readiness", () =>
751807
Effect.gen(function* () {
752808
const nodeSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;

‎packages/ssh/src/tunnel.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,8 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
12641264
stderrTail = (stderrTail + chunk).slice(-TUNNEL_STDERR_TAIL_CHARS);
12651265
}),
12661266
),
1267+
// Diagnostics are best-effort: a failed read must not hide the real exit.
1268+
Effect.ignore,
12671269
Effect.forkIn(scope),
12681270
);
12691271
const exitFailure = Effect.all(

0 commit comments

Comments
 (0)