Skip to content

Commit 89780d5

Browse files
fix(infra): gateway pid probe no longer hangs when ps sampling stalls (#109731)
* fix(infra): gateway pid probe no longer hangs when ps sampling stalls readGatewayProcessArgsSync probes ps to verify gateway argv before signaling or reporting listener PIDs. The darwin branch already passed timeout: 1000, but spawnSync defaults killSignal to SIGTERM, which an unresponsive or hung ps child can ignore and keep the gateway process blocked past the timeout. Force killSignal: SIGKILL (matching the doctor probe fix in #109243) so a stalled ps probe is reaped at the deadline instead of hanging gateway signal verification and listener PID discovery. Extract GATEWAY_PS_PROBE_TIMEOUT_MS for the named timeout and assert the full spawnSync options (encoding / killSignal / timeout) in the darwin test. * refactor: centralize bounded ps probes Co-authored-by: 琚耀辉0668001366 <[email protected]> * refactor: leave doctor ps hardening to main --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 1a2d9e5 commit 89780d5

5 files changed

Lines changed: 31 additions & 15 deletions

File tree

‎src/infra/gateway-processes.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ describe("gateway-processes", () => {
117117
"run",
118118
]);
119119
expect(readGatewayProcessArgsSync(124)).toBeNull();
120+
expect(spawnSyncMock).toHaveBeenCalledWith("ps", ["-o", "command=", "-p", "123"], {
121+
encoding: "utf8",
122+
killSignal: "SIGKILL",
123+
timeout: 1_000,
124+
});
120125
});
121126

122127
it("falls back from powershell to wmic for windows process args", () => {

‎src/infra/gateway-processes.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
// Inspects local gateway processes for status and diagnostics.
2-
import { spawnSync } from "node:child_process";
32
import fsSync from "node:fs";
43
import { uniqueValues } from "@openclaw/normalization-core/string-normalization";
54
import { isGatewayArgv, parseProcCmdline } from "./gateway-process-argv.js";
65
import { findGatewayPidsOnPortSync as findUnixGatewayPidsOnPortSync } from "./restart-stale-pids.js";
6+
import { spawnPsSync } from "./spawn-ps.js";
77
import {
88
readWindowsListeningPidsOnPortSync,
99
readWindowsProcessArgsSync,
1010
} from "./windows-port-pids.js";
1111

1212
// Gateway process helpers verify argv before signaling or reporting listener
1313
// PIDs so stale port owners cannot be mistaken for OpenClaw.
14+
const GATEWAY_PS_PROBE_TIMEOUT_MS = 1_000;
15+
1416
/** Read command argv for a PID using the current platform's process APIs. */
1517
export function readGatewayProcessArgsSync(pid: number): string[] | null {
1618
if (process.platform === "linux") {
@@ -21,10 +23,7 @@ export function readGatewayProcessArgsSync(pid: number): string[] | null {
2123
}
2224
}
2325
if (process.platform === "darwin") {
24-
const ps = spawnSync("ps", ["-o", "command=", "-p", String(pid)], {
25-
encoding: "utf8",
26-
timeout: 1000,
27-
});
26+
const ps = spawnPsSync(["-o", "command=", "-p", String(pid)], GATEWAY_PS_PROBE_TIMEOUT_MS);
2827
if (ps.error || ps.status !== 0) {
2928
return null;
3029
}

‎src/infra/restart-stale-pids.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ describe.skipIf(isWindows)("restart-stale-pids", () => {
312312
(call) => call[0] === "ps" && Array.isArray(call[1]) && (call[1] as unknown[])[0] === "-ww",
313313
);
314314
expect(psCall?.[1]).toEqual(["-ww", "-p", String(stalePid), "-o", "command="]);
315-
expect(psCall?.[2]).toEqual({ timeout: 2000, encoding: "utf8" });
315+
expect(psCall?.[2]).toEqual({ encoding: "utf8", killSignal: "SIGKILL", timeout: 2000 });
316316
});
317317

318318
it("skips malformed lsof pid tokens with trailing garbage", () => {
@@ -556,7 +556,11 @@ describe.skipIf(isWindows)("restart-stale-pids", () => {
556556
(call) =>
557557
call[0] === "ps" && Array.isArray(call[1]) && (call[1] as unknown[])[0] === "-o",
558558
);
559-
expect(ancestorPsCall?.[2]).toEqual({ timeout: 400, encoding: "utf8" });
559+
expect(ancestorPsCall?.[2]).toEqual({
560+
encoding: "utf8",
561+
killSignal: "SIGKILL",
562+
timeout: 400,
563+
});
560564
} finally {
561565
if (origDescriptor) {
562566
Object.defineProperty(process, "platform", origDescriptor);

‎src/infra/restart-stale-pids.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { formatErrorMessage, hasErrnoCode } from "./errors.js";
1010
import { isGatewayArgv, parseProcCmdline } from "./gateway-process-argv.js";
1111
import { parseStrictPositiveInteger } from "./parse-finite-number.js";
1212
import { resolveLsofCommandSync } from "./ports-lsof.js";
13+
import { spawnPsSync } from "./spawn-ps.js";
1314
import { getWindowsInstallRoots } from "./windows-install-roots.js";
1415
import {
1516
readWindowsListeningPidsOnPortSync,
@@ -110,10 +111,7 @@ function readParentPidFromProc(pid: number): number | null {
110111

111112
function readParentPidFromPs(pid: number, spawnTimeoutMs: number): number | null {
112113
try {
113-
const res = spawnSync("ps", ["-o", "ppid=", "-p", String(pid)], {
114-
encoding: "utf8",
115-
timeout: spawnTimeoutMs,
116-
});
114+
const res = spawnPsSync(["-o", "ppid=", "-p", String(pid)], spawnTimeoutMs);
117115
if (res.error || res.status !== 0 || !res.stdout.trim()) {
118116
return null;
119117
}
@@ -252,10 +250,7 @@ function readUnixProcessArgsSync(pid: number, spawnTimeoutMs: number): string[]
252250
// Fall back to ps below; /proc may be unavailable or restricted.
253251
}
254252
}
255-
const res = spawnSync("ps", ["-ww", "-p", String(pid), "-o", "command="], {
256-
encoding: "utf8",
257-
timeout: spawnTimeoutMs,
258-
});
253+
const res = spawnPsSync(["-ww", "-p", String(pid), "-o", "command="], spawnTimeoutMs);
259254
if (res.error || res.status !== 0 || !res.stdout.trim()) {
260255
return null;
261256
}

‎src/infra/spawn-ps.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { spawnSync, type SpawnSyncReturns } from "node:child_process";
2+
3+
/** Run a bounded ps probe without letting an ignored SIGTERM extend the synchronous wait. */
4+
export function spawnPsSync(
5+
args: readonly string[],
6+
timeoutMs: number,
7+
): SpawnSyncReturns<string> {
8+
return spawnSync("ps", args, {
9+
encoding: "utf8",
10+
killSignal: "SIGKILL",
11+
timeout: timeoutMs,
12+
});
13+
}

0 commit comments

Comments
 (0)