Skip to content

Commit dd388b7

Browse files
refactor: centralize bounded ps probes
Co-authored-by: ็š่€€่พ‰0668001366 <[email protected]>
1 parent 533f1ef commit dd388b7

6 files changed

Lines changed: 33 additions & 25 deletions

โ€Žsrc/commands/doctor-whatsapp-responsiveness.test.tsโ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ describe("doctor WhatsApp responsiveness", () => {
5050
{ pid: 104, command: "openclaw tui --local" },
5151
{ pid: 105, command: "/usr/bin/openclaw chat" },
5252
]);
53+
expect(spawnSyncMock).toHaveBeenCalledWith("ps", ["-axo", "pid=,command="], {
54+
encoding: "utf8",
55+
killSignal: "SIGKILL",
56+
timeout: 1_000,
57+
});
5358
}
5459
});
5560

โ€Žsrc/commands/doctor-whatsapp-responsiveness.tsโ€Ž

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/** Doctor hints for WhatsApp responsiveness when local TUI clients block gateway work. */
2-
import { spawnSync } from "node:child_process";
32
import path from "node:path";
43
import { note } from "../../packages/terminal-core/src/note.js";
54
import { formatCliCommand } from "../cli/command-format.js";
65
import type { OpenClawConfig } from "../config/types.openclaw.js";
76
import type { HealthFinding } from "../flows/health-checks.js";
7+
import { spawnPsSync } from "../infra/spawn-ps.js";
88
import { sleep } from "../utils/sleep.js";
99
import type { StatusSummary } from "./status.types.js";
1010

@@ -21,6 +21,7 @@ type ProcessController = {
2121

2222
const LOCAL_TUI_SUBCOMMANDS = new Set(["chat", "terminal", "tui"]);
2323
const WHATSAPP_RESPONSIVENESS_CHECK_ID = "core/doctor/whatsapp-responsiveness";
24+
const LOCAL_TUI_PROCESS_PROBE_TIMEOUT_MS = 1_000;
2425

2526
function tokenizeCommandLine(command: string): string[] {
2627
return command.trim().split(/\s+/u).filter(Boolean);
@@ -60,11 +61,8 @@ function listLocalTuiProcesses(): LocalTuiProcess[] {
6061
if (process.platform === "win32") {
6162
return [];
6263
}
63-
const ps = spawnSync("ps", ["-axo", "pid=,command="], {
64-
encoding: "utf8",
65-
timeout: 1000,
66-
});
67-
if (ps.error || ps.status !== 0 || typeof ps.stdout !== "string") {
64+
const ps = spawnPsSync(["-axo", "pid=,command="], LOCAL_TUI_PROCESS_PROBE_TIMEOUT_MS);
65+
if (ps.error || ps.status !== 0) {
6866
return [];
6967
}
7068
const seen = new Set<number>();

โ€Žsrc/infra/gateway-processes.tsโ€Ž

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
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-
// A stalled ps probe must not block the gateway process indefinitely. spawnSync
15-
// defaults to SIGTERM, which an unresponsive process can ignore, so we force
16-
// SIGKILL once the timeout elapses.
1714
const GATEWAY_PS_PROBE_TIMEOUT_MS = 1_000;
1815

1916
/** Read command argv for a PID using the current platform's process APIs. */
@@ -26,11 +23,7 @@ export function readGatewayProcessArgsSync(pid: number): string[] | null {
2623
}
2724
}
2825
if (process.platform === "darwin") {
29-
const ps = spawnSync("ps", ["-o", "command=", "-p", String(pid)], {
30-
encoding: "utf8",
31-
killSignal: "SIGKILL",
32-
timeout: GATEWAY_PS_PROBE_TIMEOUT_MS,
33-
});
26+
const ps = spawnPsSync(["-o", "command=", "-p", String(pid)], GATEWAY_PS_PROBE_TIMEOUT_MS);
3427
if (ps.error || ps.status !== 0) {
3528
return null;
3629
}

โ€Ž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
@@ -9,6 +9,7 @@ import { createSubsystemLogger } from "../logging/subsystem.js";
99
import { isGatewayArgv, parseProcCmdline } from "./gateway-process-argv.js";
1010
import { parseStrictPositiveInteger } from "./parse-finite-number.js";
1111
import { resolveLsofCommandSync } from "./ports-lsof.js";
12+
import { spawnPsSync } from "./spawn-ps.js";
1213
import { getWindowsInstallRoots } from "./windows-install-roots.js";
1314
import {
1415
readWindowsListeningPidsOnPortSync,
@@ -109,10 +110,7 @@ function readParentPidFromProc(pid: number): number | null {
109110

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

โ€Ž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)