Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/lib/actions/sandbox/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function runSandboxConnectProbe(sandboxName: string): void {
process.exit(1);
}
if (processCheck.wasRunning) {
ensureSandboxInferenceRouteOrExit(sandboxName, { quiet: false });
ensureSandboxInferenceRoute(sandboxName, { quiet: true });
if (processCheck.forwardRecovered) {
console.log(
` Probe complete: ${agentName} gateway is running in '${sandboxName}'; restored dashboard port forward.`,
Expand All @@ -139,11 +139,11 @@ function runSandboxConnectProbe(sandboxName: string): void {
return;
}
if (processCheck.recovered) {
ensureSandboxInferenceRouteOrExit(sandboxName, { quiet: false });
ensureSandboxInferenceRoute(sandboxName, { quiet: true });
console.log(` Probe complete: recovered ${agentName} gateway in '${sandboxName}'.`);
return;
}
ensureSandboxInferenceRouteOrExit(sandboxName, { quiet: false });
ensureSandboxInferenceRoute(sandboxName, { quiet: true });
console.error(
` Probe failed: ${agentName} gateway is not running in '${sandboxName}' and automatic recovery failed.`,
);
Expand Down
15 changes: 6 additions & 9 deletions src/lib/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const {
}: typeof import("./onboard/branding") = require("./onboard/branding");
const { cleanupTempDir }: typeof import("./onboard/temp-files") = require("./onboard/temp-files");
const { stopStaleDashboardListenersForSandbox } = require("./onboard/stale-gateway-cleanup");
const {
runBackgroundForwardStartWithDiagnostics,
}: typeof import("./onboard/forward-start") = require("./onboard/forward-start");
const { looksLikeForwardPortConflict, runBackgroundForwardStartWithPortReleaseRetries }: typeof import("./onboard/forward-start") = require("./onboard/forward-start");
const {
ensureOllamaLoopbackSystemdOverride,
}: typeof import("./onboard/ollama-systemd") = require("./onboard/ollama-systemd");
Expand Down Expand Up @@ -9303,17 +9301,16 @@ function ensureDashboardForward(
parsedUrl.port = String(actualPort);
const actualTarget = getDashboardForwardTarget(parsedUrl.toString());
runOpenshell(["forward", "stop", String(actualPort)], { ignoreError: true });
const { result: fwdResult, diagnostic: fwdDiagnostic } =
runBackgroundForwardStartWithDiagnostics((stdio, timeout) =>
const { result: fwdResult, diagnostic: fwdDiagnostic } = runBackgroundForwardStartWithPortReleaseRetries(
(stdio, timeout) =>
runOpenshell(
["forward", "start", "--background", actualTarget, sandboxName],
{ ignoreError: true, suppressOutput: true, stdio, timeout },
),
);
() => { sleep(1); runOpenshell(["forward", "stop", String(actualPort)], { ignoreError: true }); },
);
if (fwdResult && fwdResult.status !== 0) {
const looksLikePortConflict =
fwdDiagnostic === "" ||
/eaddrinuse|address already in use|port .* in use|bind: .*in use/i.test(fwdDiagnostic);
const looksLikePortConflict = looksLikeForwardPortConflict(fwdDiagnostic);
if (rollbackSandboxOnFailure) {
// The sandbox was just created, committed to actualPort via its
// baked-in CHAT_UI_URL and NEMOCLAW_DASHBOARD_PORT env. Silently
Expand Down
21 changes: 21 additions & 0 deletions src/lib/onboard/forward-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function readDiagnosticFile(filePath: string): string {
}
}

export function looksLikeForwardPortConflict(diagnostic: string): boolean {
return /eaddrinuse|address already in use|port .* in use|bind: .*in use/i.test(diagnostic);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

export function runBackgroundForwardStartWithDiagnostics(
runForwardStart: BackgroundForwardStartRunner,
timeoutMs = 30_000,
Expand Down Expand Up @@ -82,3 +86,20 @@ export function runBackgroundForwardStartWithDiagnostics(
cleanupTempDir(forwardDiagPath, "nemoclaw-forward-start");
}
}

export function runBackgroundForwardStartWithPortReleaseRetries(
runForwardStart: BackgroundForwardStartRunner,
beforeRetry: () => void,
maxRetries = 3,
): { result: BackgroundForwardStartResult; diagnostic: string } {
let attempt = runBackgroundForwardStartWithDiagnostics(runForwardStart);
for (
let retries = 0;
attempt.result.status !== 0 && looksLikeForwardPortConflict(attempt.diagnostic) && retries < maxRetries;
retries++
) {
beforeRetry();
attempt = runBackgroundForwardStartWithDiagnostics(runForwardStart);
}
return attempt;
}
6 changes: 4 additions & 2 deletions test/onboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10315,9 +10315,9 @@ const { createSandbox } = require(${onboardPath});
assert.match(source, /if \(fwdResult && fwdResult\.status !== 0\)/);
assert.match(source, /if \(rollbackSandboxOnFailure\)/);
assert.match(source, /const looksLikePortConflict =/);
assert.match(source, /eaddrinuse\|address already in use/i);
assert.match(source, /looksLikeForwardPortConflict/);
assert.match(source, /suppressOutput: true/);
assert.match(source, /runBackgroundForwardStartWithDiagnostics/);
assert.match(source, /runBackgroundForwardStartWithPortReleaseRetries/);
assert.doesNotMatch(
source,
/forward", "start", "--background"[\s\S]{0,260}stdio: \["ignore", "pipe", "pipe"\]/,
Expand All @@ -10329,6 +10329,8 @@ const { createSandbox } = require(${onboardPath});
);
assert.match(helperSource, /secureTempFile\("nemoclaw-forward-start", "\.out"\)/);
assert.match(helperSource, /runForwardStart\(\["ignore", outFd, errFd\], timeoutMs\)/);
assert.match(helperSource, /eaddrinuse\|address already in use/i);
assert.match(helperSource, /maxRetries = 3/);
assert.match(
source,
/runOpenshell\(\["sandbox", "delete", sandboxName\], \{ ignoreError: true \}\)/,
Expand Down
Loading