Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/onboard/agent-fixed-forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { bestEffortForwardStopForSandbox } from "./forward-cleanup";
import {
buildDetachedForwardStartSpawn,
buildForwardStartProgressLogger,
runDetachedForwardStartWithPortReleaseRetries,
runDetachedForwardStartWithRetries,
} from "./forward-start";

type CommandResult = { status: number | null };
Expand Down Expand Up @@ -35,7 +35,7 @@ export function ensureAgentFixedForward(
);

stopForwardForSandbox(port);
const { ok, diagnostic } = runDetachedForwardStartWithPortReleaseRetries(
const { ok, diagnostic } = runDetachedForwardStartWithRetries(
buildDetachedForwardStartSpawn(
deps.openshellArgv(["forward", "start", "--background", forwardTarget, sandboxName]),
),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/onboard/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
buildDetachedForwardStartSpawn,
buildForwardStartProgressLogger,
looksLikeForwardPortConflict,
runDetachedForwardStartWithPortReleaseRetries,
runDetachedForwardStartWithRetries,
} from "./forward-start";
import {
ensureMessagingHostForwardForSandbox,
Expand Down Expand Up @@ -319,7 +319,7 @@ export function createOnboardDashboardHelpers(deps: OnboardDashboardDeps): Onboa
parsedUrl.port = String(actualPort);
const actualTarget = getDashboardForwardTarget(parsedUrl.toString());
stopForwardForSandbox(actualPort);
const { ok: fwdOk, diagnostic: fwdDiagnostic } = runDetachedForwardStartWithPortReleaseRetries(
const { ok: fwdOk, diagnostic: fwdDiagnostic } = runDetachedForwardStartWithRetries(
buildDetachedForwardStartSpawn(
deps.openshellArgv(["forward", "start", "--background", actualTarget, sandboxName]),
),
Expand Down
247 changes: 233 additions & 14 deletions src/lib/onboard/forward-start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { describe, expect, it, vi } from "vitest";

import {
buildDetachedForwardStartSpawn,
looksLikeForwardListenerStartFailure,
looksLikeForwardPortConflict,
looksLikeUntrackedForward,
runDetachedForwardStartWithDiagnostics,
runDetachedForwardStartWithPortReleaseRetries,
runDetachedForwardStartWithRetries,
} from "./forward-start";

// Build an `openshell forward list`-shaped output for the given live entries.
Expand Down Expand Up @@ -421,13 +422,11 @@ describe("runDetachedForwardStartWithDiagnostics", () => {
expect(isPortListening).toHaveBeenCalledWith(18789);
});

it("keeps waiting (then times out) when ssh exits under ControlMaster but the port is not live (#6099)", () => {
// A genuinely failed ssh (auth error, refused connection) produces the same
// exit diagnostic without a live listener — the fallback must not confirm.
it("returns immediately when ssh exits and no ControlMaster listener is live (#7266)", () => {
const fetchList = vi.fn().mockReturnValue(forwardListWith([]));
const spawn = vi.fn().mockImplementation(({ stderr }: { stderr: number }) => {
fs.writeSync(stderr, "ssh exited before local forward listener opened on 127.0.0.1:18789\n");
return { pid: 782 };
return {};
});
const sleep = vi.fn();
const isPortListening = vi.fn().mockReturnValue(false);
Expand All @@ -440,8 +439,61 @@ describe("runDetachedForwardStartWithDiagnostics", () => {
);

expect(result.ok).toBe(false);
expect(result.reason).toBe("timeout");
expect(isPortListening).toHaveBeenCalled();
expect(result.reason).toBe("listener-start-failure");
expect(isPortListening).toHaveBeenCalledOnce();
expect(sleep).not.toHaveBeenCalled();
});

it("returns immediately for openshell's listener timeout when the port remains closed (#7266)", () => {
const fetchList = vi.fn().mockReturnValue(forwardListWith([]));
const spawn = vi.fn().mockImplementation(({ stderr }: { stderr: number }) => {
fs.writeSync(
stderr,
"Error: ssh process started but local forward listener was not reachable\n" +
"local forward listener did not open on 127.0.0.1:18789 within 10000ms\n",
);
return {};
});
const sleep = vi.fn();
const isPortListening = vi.fn().mockReturnValue(false);

const result = runDetachedForwardStartWithDiagnostics(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
{ overallTimeoutMs: 180_000, pollIntervalMs: 500, sleepMs: sleep, isPortListening },
);

expect(result.ok).toBe(false);
expect(result.reason).toBe("listener-start-failure");
expect(result.diagnostic).not.toContain("forward did not appear in list within");
expect(isPortListening).toHaveBeenCalledOnce();
expect(sleep).not.toHaveBeenCalled();
});

it("rejects another sandbox's live row during listener-start failure (#7266)", () => {
const fetchList = vi
.fn()
.mockReturnValue(forwardListWith([{ sandbox: "other-sandbox", port: 18789 }]));
const spawn = vi.fn().mockImplementation(({ stderr }: { stderr: number }) => {
fs.writeSync(
stderr,
"local forward listener did not open on 127.0.0.1:18789 within 10000ms\n",
);
return {};
});
const isPortListening = vi.fn().mockReturnValue(true);

const result = runDetachedForwardStartWithDiagnostics(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
{ overallTimeoutMs: 180_000, sleepMs: vi.fn(), isPortListening },
);

expect(result.ok).toBe(false);
expect(result.reason).toBe("listener-start-failure");
expect(isPortListening).not.toHaveBeenCalled();
});

it("keeps waiting (then times out) when openshell reports untracked but the port is not live", () => {
Expand Down Expand Up @@ -490,7 +542,7 @@ describe("runDetachedForwardStartWithDiagnostics", () => {
});
});

describe("runDetachedForwardStartWithPortReleaseRetries", () => {
describe("runDetachedForwardStartWithRetries", () => {
it("retries after a port-conflict diagnostic, then succeeds", () => {
const fetchList = vi
.fn()
Expand All @@ -506,12 +558,18 @@ describe("runDetachedForwardStartWithPortReleaseRetries", () => {
.mockReturnValueOnce({ pid: 99 });
const sleep = vi.fn();

const result = runDetachedForwardStartWithPortReleaseRetries(
const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{ overallTimeoutMs: 30, pollIntervalMs: 10, sleepMs: sleep, maxRetries: 3 },
{
overallTimeoutMs: 30,
pollIntervalMs: 10,
sleepMs: sleep,
maxRetries: 3,
isPortListening: vi.fn().mockReturnValue(false),
},
);

expect(result.ok).toBe(true);
Expand All @@ -525,12 +583,18 @@ describe("runDetachedForwardStartWithPortReleaseRetries", () => {
const spawn = vi.fn().mockReturnValue({ pid: 42 });
const sleep = vi.fn();

const result = runDetachedForwardStartWithPortReleaseRetries(
const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{ overallTimeoutMs: 20, pollIntervalMs: 10, sleepMs: sleep, maxRetries: 3 },
{
overallTimeoutMs: 20,
pollIntervalMs: 10,
sleepMs: sleep,
maxRetries: 3,
isPortListening: vi.fn().mockReturnValue(false),
},
);

expect(result.ok).toBe(false);
Expand All @@ -547,18 +611,156 @@ describe("runDetachedForwardStartWithPortReleaseRetries", () => {
.mockReturnValue({ error: new Error("EADDRINUSE: address already in use") });
const sleep = vi.fn();

const result = runDetachedForwardStartWithPortReleaseRetries(
const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{ overallTimeoutMs: 20, pollIntervalMs: 10, sleepMs: sleep, maxRetries: 2 },
{
overallTimeoutMs: 20,
pollIntervalMs: 10,
sleepMs: sleep,
maxRetries: 2,
isPortListening: vi.fn().mockReturnValue(false),
},
);

expect(result.ok).toBe(false);
expect(beforeRetry).toHaveBeenCalledTimes(2);
expect(spawn).toHaveBeenCalledTimes(3); // initial + 2 retries
});

it("cleans and retries a failed listener start, then succeeds (#7266)", () => {
const fetchList = vi
.fn()
.mockReturnValueOnce(forwardListWith([]))
.mockReturnValue(forwardListWith([{ sandbox: "my-sandbox", port: 18789 }]));
const spawn = vi
.fn()
.mockImplementationOnce(({ stderr }: { stderr: number }) => {
fs.writeSync(
stderr,
"local forward listener did not open on 127.0.0.1:18789 within 10000ms\n",
);
return {};
})
.mockReturnValueOnce({ pid: 785 });
const beforeRetry = vi.fn();

const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{
overallTimeoutMs: 180_000,
pollIntervalMs: 500,
sleepMs: vi.fn(),
isPortListening: vi.fn().mockReturnValue(false),
},
);

expect(result.ok).toBe(true);
expect(beforeRetry).toHaveBeenCalledOnce();
expect(spawn).toHaveBeenCalledTimes(2);
});

it("preserves a ControlMaster listener created by the current attempt (#6099)", () => {
const fetchList = vi.fn().mockReturnValue(forwardListWith([]));
const spawn = vi.fn().mockImplementation(({ stderr }: { stderr: number }) => {
fs.writeSync(stderr, "ssh exited before local forward listener opened on 127.0.0.1:18789\n");
return { pid: 785 };
});
const beforeRetry = vi.fn();
const isPortListening = vi
.fn()
.mockReturnValueOnce(false) // free before this attempt starts
.mockReturnValueOnce(true); // mux owns the listener after ssh exits

const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{ isPortListening },
);

expect(result.ok).toBe(true);
expect(result.reason).toBe("ok-port-live");
expect(beforeRetry).not.toHaveBeenCalled();
expect(isPortListening).toHaveBeenCalledTimes(2);
});

it("stops after bounded retries when listener startup keeps failing (#7266)", () => {
const fetchList = vi.fn().mockReturnValue(forwardListWith([]));
const spawn = vi.fn().mockImplementation(({ stderr }: { stderr: number }) => {
fs.writeSync(
stderr,
"local forward listener did not open on 127.0.0.1:18789 within 10000ms\n",
);
return {};
});
const beforeRetry = vi.fn();

const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{
overallTimeoutMs: 180_000,
pollIntervalMs: 500,
sleepMs: vi.fn(),
isPortListening: vi.fn().mockReturnValue(false),
maxRetries: 2,
},
);

expect(result.ok).toBe(false);
expect(result.reason).toBe("listener-start-failure");
expect(beforeRetry).toHaveBeenCalledTimes(2);
expect(spawn).toHaveBeenCalledTimes(3);
});

it("does not retry unrelated authentication failures (#7266)", () => {
const fetchList = vi.fn();
const spawn = vi.fn().mockReturnValue({ error: new Error("Permission denied (publickey)") });
const beforeRetry = vi.fn();

const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{ maxRetries: 3, isPortListening: vi.fn().mockReturnValue(false) },
);

expect(result.ok).toBe(false);
expect(result.reason).toBe("spawn-error");
expect(beforeRetry).not.toHaveBeenCalled();
expect(spawn).toHaveBeenCalledOnce();
});

it("never spawns over an arbitrary listener that predates the attempt (#7266)", () => {
const fetchList = vi.fn();
const spawn = vi.fn();
const beforeRetry = vi.fn();
const isPortListening = vi.fn().mockReturnValue(true);

const result = runDetachedForwardStartWithRetries(
spawn,
fetchList,
{ port: 18789, sandboxName: "my-sandbox" },
beforeRetry,
{ maxRetries: 2, isPortListening },
);

expect(result.ok).toBe(false);
expect(result.reason).toBe("spawn-conflict");
expect(beforeRetry).toHaveBeenCalledTimes(2);
expect(spawn).not.toHaveBeenCalled();
expect(fetchList).not.toHaveBeenCalled();
});
});

describe("looksLikeForwardPortConflict", () => {
Expand All @@ -576,6 +778,23 @@ describe("looksLikeForwardPortConflict", () => {
});
});

describe("looksLikeForwardListenerStartFailure", () => {
it("matches only definitive listener termination diagnostics", () => {
expect(
looksLikeForwardListenerStartFailure(
"local forward listener did not open on 127.0.0.1:18789 within 10000ms",
),
).toBe(true);
expect(
looksLikeForwardListenerStartFailure(
"ssh exited before local forward listener opened on 127.0.0.1:18789",
),
).toBe(true);
expect(looksLikeForwardListenerStartFailure("Permission denied (publickey)")).toBe(false);
expect(looksLikeForwardListenerStartFailure("gateway transport unavailable")).toBe(false);
});
});

describe("looksLikeUntrackedForward", () => {
it("matches openshell's untracked-forward notice", () => {
expect(
Expand Down
Loading
Loading