Skip to content

Commit d3c07b0

Browse files
authored
fix(cli): reduce gateway status memory (#132784)
1 parent 59a3352 commit d3c07b0

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎src/cli/cli-utils.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,19 @@ describe("shouldSkipRespawnForArgv", () => {
113113
{ argv: ["node", "openclaw", "gateway"] },
114114
{ argv: ["node", "openclaw", "gateway", "--port", "14720", "--bind", "loopback"] },
115115
{ argv: ["node", "openclaw", "gateway", "run", "--port=14720", "--bind", "loopback"] },
116+
{ argv: ["node", "openclaw", "gateway", "status"] },
116117
{
117118
argv: ["node", "openclaw", "--profile", "server", "gateway", "run", "--allow-unconfigured"],
118119
},
120+
{
121+
argv: ["node", "openclaw", "--profile", "server", "gateway", "status", "--json"],
122+
},
119123
] as const)("skips respawn for argv %j", ({ argv }) => {
120124
expect(shouldSkipRespawnForArgv([...argv]), argv.join(" ")).toBe(true);
121125
});
122126

123127
it.each([
124128
{ argv: ["node", "openclaw", "status"] },
125-
{ argv: ["node", "openclaw", "gateway", "status"] },
126129
{ argv: ["node", "openclaw", "gateway", "call", "health"] },
127130
] as const)("keeps respawn path for argv %j", ({ argv }) => {
128131
expect(shouldSkipRespawnForArgv([...argv]), argv.join(" ")).toBe(false);

‎src/cli/gateway-backed-exit.process.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,72 @@ describe("gateway-backed CLI process exit", () => {
490490
},
491491
);
492492

493+
it.runIf(process.platform !== "win32")(
494+
"runs gateway status through one OpenClaw entry process",
495+
async () => {
496+
const root = tempDirs.make("openclaw-gateway-status-entry-process-");
497+
const stateDir = path.join(root, "state");
498+
const configPath = path.join(stateDir, "openclaw.json");
499+
const pidLogPath = path.join(root, "entry-pids");
500+
const preloadPath = path.join(root, "track-entry-pid.mjs");
501+
const token = "configured-token";
502+
const gateway = await startGatewayStabilityRpcServer(token, "issued-device-token");
503+
await fs.mkdir(stateDir, { recursive: true });
504+
await fs.writeFile(
505+
configPath,
506+
JSON.stringify({ gateway: { mode: "remote", remote: { url: gateway.url, token } } }),
507+
);
508+
await fs.writeFile(
509+
preloadPath,
510+
[
511+
'import fs from "node:fs";',
512+
'const entry = process.argv[1]?.replaceAll("\\\\", "/");',
513+
'if (entry?.endsWith("/src/entry.ts")) {',
514+
" fs.appendFileSync(process.env.OPENCLAW_ENTRY_PID_LOG, `${process.pid}\\n`);",
515+
"}",
516+
"",
517+
].join("\n"),
518+
);
519+
520+
const result = await runIsolatedGatewayCli({
521+
args: [
522+
"gateway",
523+
"status",
524+
"--url",
525+
gateway.url,
526+
"--token",
527+
token,
528+
"--require-rpc",
529+
"--json",
530+
"--timeout",
531+
"2000",
532+
],
533+
root,
534+
stateDir,
535+
configPath,
536+
env: {
537+
NODE_OPTIONS: `--import=${pathToFileURL(preloadPath).href}`,
538+
OPENCLAW_ENTRY_PID_LOG: pidLogPath,
539+
OPENCLAW_NODE_EXTRA_CA_CERTS_READY: "1",
540+
OPENCLAW_NODE_OPTIONS_READY: undefined,
541+
OPENCLAW_NO_RESPAWN: undefined,
542+
},
543+
});
544+
545+
expect(result, result.stderr).toMatchObject({ code: 0, signal: null, stderr: "" });
546+
expect(JSON.parse(result.stdout)).toMatchObject({
547+
rpc: { ok: true, kind: "read" },
548+
});
549+
const entryPids = new Set(
550+
(await fs.readFile(pidLogPath, "utf8"))
551+
.trim()
552+
.split(/\s+/u)
553+
.map((value) => Number.parseInt(value, 10)),
554+
);
555+
expect(entryPids.size).toBe(1);
556+
},
557+
);
558+
493559
it.each([
494560
{ label: "list", args: ["devices", "list", "--timeout", "250"] },
495561
{ label: "join-code", args: ["devices", "join-code", "--timeout", "250"] },

‎src/cli/respawn-policy.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,18 @@ export function shouldSkipRespawnForArgv(
7979
platform: NodeJS.Platform = process.platform,
8080
): boolean {
8181
const invocation = resolveCliArgvInvocation(argv);
82+
const isGatewayStatus =
83+
invocation.commandPath.length === 2 &&
84+
invocation.commandPath[0] === "gateway" &&
85+
invocation.commandPath[1] === "status";
8286
return (
8387
invocation.hasHelpOrVersion ||
8488
isInteractiveTtyCommandArgv(argv) ||
8589
isForegroundGmailRunArgv(argv) ||
8690
shouldKeepNativeHookRelayInProcess(argv, platform) ||
91+
// Status commonly overlaps the running Gateway; a warning-only wrapper doubles
92+
// transient CLI memory. Startup-environment respawn remains separately owned.
93+
isGatewayStatus ||
8794
(invocation.primary === "gateway" && isForegroundGatewayRunArgv(argv))
8895
);
8996
}

‎src/entry.respawn.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ describe("buildCliRespawnPlan", () => {
9595
expect(respawnPlan.detachForProcessTree).toBe(true);
9696
});
9797

98+
it("does not respawn gateway status only to suppress warnings", () => {
99+
expect(
100+
buildCliRespawnPlan({
101+
argv: ["node", "openclaw", "gateway", "status", "--json"],
102+
env: {},
103+
execArgv: [],
104+
autoNodeExtraCaCerts: undefined,
105+
platform: "linux",
106+
}),
107+
).toBeNull();
108+
});
109+
110+
it("preserves NODE_EXTRA_CA_CERTS respawn for gateway status", () => {
111+
const plan = buildCliRespawnPlan({
112+
argv: ["node", "openclaw", "gateway", "status", "--json"],
113+
env: {},
114+
execArgv: [],
115+
autoNodeExtraCaCerts: "/etc/ssl/certs/ca-certificates.crt",
116+
platform: "linux",
117+
});
118+
119+
const respawnPlan = expectCliRespawnPlan(plan);
120+
expect(respawnPlan.argv).toEqual(["openclaw", "gateway", "status", "--json"]);
121+
expect(respawnPlan.env.NODE_EXTRA_CA_CERTS).toBe("/etc/ssl/certs/ca-certificates.crt");
122+
expect(respawnPlan.env[OPENCLAW_NODE_EXTRA_CA_CERTS_READY]).toBe("1");
123+
expect(respawnPlan.env[OPENCLAW_NODE_OPTIONS_READY]).toBeUndefined();
124+
expect(respawnPlan.detachForProcessTree).toBe(true);
125+
});
126+
98127
it.each(["tui", "terminal", "chat"] as const)(
99128
"preserves NODE_EXTRA_CA_CERTS respawn for interactive %s",
100129
(command) => {

0 commit comments

Comments
 (0)