Skip to content

Commit 34b18ea

Browse files
Zaf (via OpenClaw)steipete
authored andcommitted
fix: respect OPENCLAW_HOME for isolated gateway instances
When OPENCLAW_HOME is set (indicating an isolated instance), the gateway port should be read from config rather than inheriting OPENCLAW_GATEWAY_PORT from a parent process. This fixes running multiple OpenClaw instances where a child process would incorrectly use the parent's port. Changes: - resolveGatewayPort() now prioritizes config.gateway.port when OPENCLAW_HOME is set - Added getConfigPath() function for runtime-evaluated config path - Deprecated CONFIG_PATH constant with warning about module-load-time evaluation - Updated gateway run command to use getConfigPath() instead of CONFIG_PATH Fixes the issue where spawning a sandbox OpenClaw instance from within another OpenClaw process would fail because OPENCLAW_GATEWAY_PORT from the parent (set in server.impl.ts) would override the child's config.
1 parent 4641e45 commit 34b18ea

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

‎src/cli/gateway-cli/run.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "node:path";
44
import type { GatewayAuthMode } from "../../config/config.js";
55
import type { GatewayWsLogStyle } from "../../gateway/ws-logging.js";
66
import {
7-
CONFIG_PATH,
7+
getConfigPath,
88
loadConfig,
99
readConfigFileSnapshot,
1010
resolveStateDir,
@@ -161,7 +161,7 @@ async function runGatewayCommand(opts: GatewayRunOpts) {
161161
const tokenRaw = toOptionString(opts.token);
162162

163163
const snapshot = await readConfigFileSnapshot().catch(() => null);
164-
const configExists = snapshot?.exists ?? fs.existsSync(CONFIG_PATH);
164+
const configExists = snapshot?.exists ?? fs.existsSync(getConfigPath());
165165
const configAuditPath = path.join(resolveStateDir(process.env), "logs", "config-audit.jsonl");
166166
const mode = cfg.gateway?.mode;
167167
if (!opts.allowUnconfigured && mode !== "local") {

‎src/config/paths.ts‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,21 @@ export function resolveConfigPath(
182182
return path.join(stateDir, CONFIG_FILENAME);
183183
}
184184

185+
/**
186+
* @deprecated Use resolveConfigPathCandidate() instead. This constant is evaluated
187+
* at module load time and does not respect OPENCLAW_HOME set after import.
188+
* Kept for backwards compatibility but should be avoided in new code.
189+
*/
185190
export const CONFIG_PATH = resolveConfigPathCandidate();
186191

192+
/**
193+
* Runtime-evaluated config path that respects OPENCLAW_HOME.
194+
* Use this instead of CONFIG_PATH when OPENCLAW_HOME may be set dynamically.
195+
*/
196+
export function getConfigPath(env: NodeJS.ProcessEnv = process.env): string {
197+
return resolveConfigPathCandidate(env, envHomedir(env));
198+
}
199+
187200
/**
188201
* Resolve default config path candidates across default locations.
189202
* Order: explicit config path → state-dir-derived paths → new default.
@@ -258,18 +271,34 @@ export function resolveGatewayPort(
258271
cfg?: OpenClawConfig,
259272
env: NodeJS.ProcessEnv = process.env,
260273
): number {
274+
// When OPENCLAW_HOME is set (isolated instance), prefer config over inherited env vars.
275+
// This prevents a parent gateway's OPENCLAW_GATEWAY_PORT from bleeding through.
276+
const isIsolatedInstance = Boolean(env.OPENCLAW_HOME?.trim());
277+
278+
// Config port takes precedence for isolated instances
279+
const configPort = cfg?.gateway?.port;
280+
if (
281+
isIsolatedInstance &&
282+
typeof configPort === "number" &&
283+
Number.isFinite(configPort) &&
284+
configPort > 0
285+
) {
286+
return configPort;
287+
}
288+
289+
// Check env vars (for non-isolated or when config doesn't specify port)
261290
const envRaw = env.OPENCLAW_GATEWAY_PORT?.trim() || env.CLAWDBOT_GATEWAY_PORT?.trim();
262291
if (envRaw) {
263292
const parsed = Number.parseInt(envRaw, 10);
264293
if (Number.isFinite(parsed) && parsed > 0) {
265294
return parsed;
266295
}
267296
}
268-
const configPort = cfg?.gateway?.port;
269-
if (typeof configPort === "number" && Number.isFinite(configPort)) {
270-
if (configPort > 0) {
271-
return configPort;
272-
}
297+
298+
// Fall back to config for non-isolated instances
299+
if (typeof configPort === "number" && Number.isFinite(configPort) && configPort > 0) {
300+
return configPort;
273301
}
302+
274303
return DEFAULT_GATEWAY_PORT;
275304
}

0 commit comments

Comments
 (0)