Repository navigation
fix(onboard): poll forward list instead of waiting on openshell CLI exit #4072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b072c10
fix(onboard): poll forward list instead of waiting on openshell CLI e…
laitingsheng 45f5671
fix(onboard): shrink forward-start callsite, fix sleep child, capture…
laitingsheng 86ca19d
Merge remote-tracking branch 'origin/main' into fix/forward-start-det…
laitingsheng 2a7d7b9
fix(onboard): trim forward-start callsite, surface async spawn errors…
laitingsheng 9b635e4
Merge remote-tracking branch 'origin/main' into fix/forward-start-det…
laitingsheng 209bcb2
fix(onboard): extend forward-start deadline + surface gateway list st…
laitingsheng 5a98412
fix(onboard): owner-gate forward-stop, time-out forward-list, preflig…
laitingsheng fcacdc2
Merge remote-tracking branch 'origin/main' into fix/forward-start-det…
laitingsheng 10f4f32
fix(onboard): close TOCTOU + dead-code paths in forward-start helpers
laitingsheng 169d51f
fix(onboard): null-pid spawn-error, clear stale fetch error, SIGTERM …
laitingsheng 3e80e72
Merge remote-tracking branch 'origin/main' into fix/forward-start-det…
laitingsheng dddbe5d
fix(onboard): inline CONTROL_UI_PORT to keep onboard.ts within budget
laitingsheng 707d647
fix(onboard): list-failed catches forward-list errors; lift fs import…
laitingsheng 6ce79ad
fix(onboard): scope dashboard forward cleanup stops
ericksoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { | ||
| looksLikeForwardPortConflict, | ||
| runDetachedForwardStartWithDiagnostics, | ||
| runDetachedForwardStartWithPortReleaseRetries, | ||
| } from "../../../dist/lib/onboard/forward-start"; | ||
|
|
||
| // Build an `openshell forward list`-shaped output for the given live entries. | ||
| // Mirrors the column layout (SANDBOX BIND PORT PID STATUS) that | ||
| // `getOccupiedPorts` parses, so the helper recognises the forward as live. | ||
| function forwardListWith(entries: Array<{ sandbox: string; port: number; status?: string }>): string { | ||
| const header = "SANDBOX BIND PORT PID STATUS"; | ||
| const rows = entries.map( | ||
| (e) => `${e.sandbox} 127.0.0.1 ${e.port} 1234 ${e.status ?? "running"}`, | ||
| ); | ||
| return [header, ...rows].join("\n"); | ||
| } | ||
|
|
||
| describe("runDetachedForwardStartWithDiagnostics", () => { | ||
| it("returns ok as soon as the forward appears in the list", () => { | ||
| const fetchList = vi | ||
| .fn() | ||
| .mockReturnValueOnce(forwardListWith([])) // first poll: nothing yet | ||
| .mockReturnValue(forwardListWith([{ sandbox: "my-sandbox", port: 18789 }])); | ||
| const spawn = vi.fn().mockReturnValue({ pid: 42 }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithDiagnostics( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| { overallTimeoutMs: 10_000, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| expect(result.reason).toBe("ok"); | ||
| expect(result.pid).toBe(42); | ||
| expect(spawn).toHaveBeenCalledTimes(1); | ||
| // First poll missed → one sleep before the second poll observed the entry. | ||
| expect(sleep).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("ignores entries that belong to a different sandbox", () => { | ||
| const fetchList = vi | ||
| .fn() | ||
| .mockReturnValue(forwardListWith([{ sandbox: "other-sandbox", port: 18789 }])); | ||
| const spawn = vi.fn().mockReturnValue({ pid: 42 }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithDiagnostics( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| { overallTimeoutMs: 50, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.reason).toBe("timeout"); | ||
| }); | ||
|
|
||
| it("reports timeout when the forward never appears", () => { | ||
| const fetchList = vi.fn().mockReturnValue(forwardListWith([])); | ||
| const spawn = vi.fn().mockReturnValue({ pid: 42 }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithDiagnostics( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| { overallTimeoutMs: 30, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.reason).toBe("timeout"); | ||
| expect(result.diagnostic).toMatch(/forward did not appear in list within 30ms/); | ||
| }); | ||
|
|
||
| it("surfaces spawn errors immediately without polling", () => { | ||
| const fetchList = vi.fn(); | ||
| const spawn = vi.fn().mockReturnValue({ error: new Error("ENOENT: openshell not found") }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithDiagnostics( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| { overallTimeoutMs: 10_000, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.reason).toBe("spawn-error"); | ||
| expect(result.diagnostic).toMatch(/ENOENT/); | ||
| expect(fetchList).not.toHaveBeenCalled(); | ||
| expect(sleep).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("surfaces async spawn errors fired after the runner returned", () => { | ||
| const fetchList = vi.fn().mockReturnValue(""); | ||
| let asyncErrorCallback: ((err: Error) => void) | undefined; | ||
| const spawn = vi.fn().mockImplementation((_stdio, onAsyncError) => { | ||
| asyncErrorCallback = onAsyncError; | ||
| return { pid: 4242 }; | ||
| }); | ||
| // The sleep stub is the only synchronous yield point in the helper's | ||
| // poll loop. Simulate Node's `error` event firing during that pause so | ||
| // the next iteration observes `asyncSpawnError` and returns spawn-error. | ||
| let sleepCalls = 0; | ||
| const sleep = vi.fn().mockImplementation(() => { | ||
| sleepCalls += 1; | ||
| if (sleepCalls === 1 && asyncErrorCallback) { | ||
| asyncErrorCallback(new Error("spawn ENOENT")); | ||
| } | ||
| }); | ||
|
|
||
| const result = runDetachedForwardStartWithDiagnostics( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| { overallTimeoutMs: 1_000, pollIntervalMs: 5, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.reason).toBe("spawn-error"); | ||
| expect(result.diagnostic).toMatch(/spawn ENOENT/); | ||
| }); | ||
|
|
||
| it("surfaces persistent fetchForwardList failures in the timeout diagnostic", () => { | ||
| const fetchList = vi.fn().mockImplementation(() => { | ||
| throw new Error("gateway transport: connection refused"); | ||
| }); | ||
| const spawn = vi.fn().mockReturnValue({ pid: 42 }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithDiagnostics( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| { overallTimeoutMs: 30, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.reason).toBe("timeout"); | ||
| expect(result.diagnostic).toMatch(/openshell forward list failed/); | ||
| expect(result.diagnostic).toMatch(/connection refused/); | ||
| }); | ||
|
|
||
| it("treats fetchForwardList exceptions as transient and keeps polling", () => { | ||
| const fetchList = vi | ||
| .fn() | ||
| .mockImplementationOnce(() => { | ||
| throw new Error("gateway not reachable yet"); | ||
| }) | ||
| .mockReturnValue(forwardListWith([{ sandbox: "my-sandbox", port: 18789 }])); | ||
| const spawn = vi.fn().mockReturnValue({ pid: 42 }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithDiagnostics( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| { overallTimeoutMs: 10_000, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| expect(fetchList).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe("runDetachedForwardStartWithPortReleaseRetries", () => { | ||
| it("retries after a port-conflict diagnostic, then succeeds", () => { | ||
| const fetchList = vi | ||
| .fn() | ||
| .mockReturnValueOnce(forwardListWith([])) // first attempt: never appears | ||
| .mockReturnValueOnce(forwardListWith([])) // (timeout settles) | ||
| .mockReturnValue(forwardListWith([{ sandbox: "my-sandbox", port: 18789 }])); | ||
| const beforeRetry = vi.fn(); | ||
| // First spawn surfaces a port-conflict in its diagnostic synthesised via | ||
| // an Error message; the second spawn succeeds and the forward appears. | ||
| const spawn = vi | ||
| .fn() | ||
| .mockReturnValueOnce({ error: new Error("EADDRINUSE: address already in use") }) | ||
| .mockReturnValueOnce({ pid: 99 }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithPortReleaseRetries( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| beforeRetry, | ||
| 3, | ||
| { overallTimeoutMs: 30, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| expect(beforeRetry).toHaveBeenCalledTimes(1); | ||
| expect(spawn).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("does not retry when the failure does not look like a port conflict", () => { | ||
| const fetchList = vi.fn().mockReturnValue(forwardListWith([])); | ||
| const beforeRetry = vi.fn(); | ||
| const spawn = vi.fn().mockReturnValue({ pid: 42 }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithPortReleaseRetries( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| beforeRetry, | ||
| 3, | ||
| { overallTimeoutMs: 20, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.reason).toBe("timeout"); | ||
| expect(beforeRetry).not.toHaveBeenCalled(); | ||
| expect(spawn).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("stops retrying after maxRetries even if conflict diagnostics persist", () => { | ||
| const fetchList = vi.fn().mockReturnValue(forwardListWith([])); | ||
| const beforeRetry = vi.fn(); | ||
| const spawn = vi | ||
| .fn() | ||
| .mockReturnValue({ error: new Error("EADDRINUSE: address already in use") }); | ||
| const sleep = vi.fn(); | ||
|
|
||
| const result = runDetachedForwardStartWithPortReleaseRetries( | ||
| spawn, | ||
| fetchList, | ||
| { port: 18789, sandboxName: "my-sandbox" }, | ||
| beforeRetry, | ||
| 2, | ||
| { overallTimeoutMs: 20, pollIntervalMs: 10, sleepMs: sleep }, | ||
| ); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(beforeRetry).toHaveBeenCalledTimes(2); | ||
| expect(spawn).toHaveBeenCalledTimes(3); // initial + 2 retries | ||
| }); | ||
| }); | ||
|
|
||
| describe("looksLikeForwardPortConflict", () => { | ||
| it("matches the common port-in-use signals", () => { | ||
| expect(looksLikeForwardPortConflict("listen tcp 0.0.0.0:18789: bind: address already in use")).toBe( | ||
| true, | ||
| ); | ||
| expect(looksLikeForwardPortConflict("EADDRINUSE")).toBe(true); | ||
| expect(looksLikeForwardPortConflict("port 18789 in use")).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false for unrelated errors", () => { | ||
| expect(looksLikeForwardPortConflict("transport: connection refused")).toBe(false); | ||
| expect(looksLikeForwardPortConflict("")).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.