diff --git a/src/cli/daemon-cli/restart-health.test.ts b/src/cli/daemon-cli/restart-health.test.ts deleted file mode 100644 index c438e9a0ea8..00000000000 --- a/src/cli/daemon-cli/restart-health.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; -import type { GatewayService } from "../../daemon/service.js"; - -const inspectPortUsage = vi.hoisted(() => vi.fn()); -const classifyPortListener = vi.hoisted(() => vi.fn(() => "gateway")); - -vi.mock("../../infra/ports.js", () => ({ - classifyPortListener: (...args: unknown[]) => classifyPortListener(...args), - formatPortDiagnostics: vi.fn(() => []), - inspectPortUsage: (...args: unknown[]) => inspectPortUsage(...args), -})); - -describe("inspectGatewayRestart", () => { - beforeEach(() => { - inspectPortUsage.mockReset(); - classifyPortListener.mockReset(); - classifyPortListener.mockReturnValue("gateway"); - }); - - it("treats a gateway listener child pid as healthy ownership", async () => { - const service = { - readRuntime: vi.fn(async () => ({ status: "running", pid: 7000 })), - } as unknown as GatewayService; - - inspectPortUsage.mockResolvedValue({ - port: 18789, - status: "busy", - listeners: [{ pid: 7001, ppid: 7000, commandLine: "openclaw-gateway" }], - hints: [], - }); - - const { inspectGatewayRestart } = await import("./restart-health.js"); - const snapshot = await inspectGatewayRestart({ service, port: 18789 }); - - expect(snapshot.healthy).toBe(true); - expect(snapshot.staleGatewayPids).toEqual([]); - }); - - it("marks non-owned gateway listener pids as stale while runtime is running", async () => { - const service = { - readRuntime: vi.fn(async () => ({ status: "running", pid: 8000 })), - } as unknown as GatewayService; - - inspectPortUsage.mockResolvedValue({ - port: 18789, - status: "busy", - listeners: [{ pid: 9000, ppid: 8999, commandLine: "openclaw-gateway" }], - hints: [], - }); - - const { inspectGatewayRestart } = await import("./restart-health.js"); - const snapshot = await inspectGatewayRestart({ service, port: 18789 }); - - expect(snapshot.healthy).toBe(false); - expect(snapshot.staleGatewayPids).toEqual([9000]); - }); -}); diff --git a/src/cli/daemon-cli/restart-health.ts b/src/cli/daemon-cli/restart-health.ts index 575fdee7ec9..4a0d5bcf4bb 100644 --- a/src/cli/daemon-cli/restart-health.ts +++ b/src/cli/daemon-cli/restart-health.ts @@ -21,13 +21,6 @@ export type GatewayRestartSnapshot = { staleGatewayPids: number[]; }; -function listenerOwnedByRuntimePid(params: { - listener: PortUsage["listeners"][number]; - runtimePid: number; -}): boolean { - return params.listener.pid === params.runtimePid || params.listener.ppid === params.runtimePid; -} - export async function inspectGatewayRestart(params: { service: GatewayService; port: number; @@ -63,26 +56,16 @@ export async function inspectGatewayRestart(params: { const running = runtime.status === "running"; const ownsPort = runtime.pid != null - ? portUsage.listeners.some((listener) => - listenerOwnedByRuntimePid({ listener, runtimePid: runtime.pid }), - ) + ? portUsage.listeners.some((listener) => listener.pid === runtime.pid) : gatewayListeners.length > 0 || (portUsage.status === "busy" && portUsage.listeners.length === 0); const healthy = running && ownsPort; const staleGatewayPids = Array.from( new Set( gatewayListeners - .filter((listener) => Number.isFinite(listener.pid)) - .filter((listener) => { - if (!running) { - return true; - } - if (runtime.pid == null) { - return true; - } - return !listenerOwnedByRuntimePid({ listener, runtimePid: runtime.pid }); - }) - .map((listener) => listener.pid as number), + .map((listener) => listener.pid) + .filter((pid): pid is number => Number.isFinite(pid)) + .filter((pid) => runtime.pid == null || pid !== runtime.pid || !running), ), ); diff --git a/src/infra/ports-inspect.ts b/src/infra/ports-inspect.ts index 344086ae14a..d6c172a7bd9 100644 --- a/src/infra/ports-inspect.ts +++ b/src/infra/ports-inspect.ts @@ -75,16 +75,6 @@ async function resolveUnixUser(pid: number): Promise { return line || undefined; } -async function resolveUnixParentPid(pid: number): Promise { - const res = await runCommandSafe(["ps", "-p", String(pid), "-o", "ppid="]); - if (res.code !== 0) { - return undefined; - } - const line = res.stdout.trim(); - const parentPid = Number.parseInt(line, 10); - return Number.isFinite(parentPid) && parentPid > 0 ? parentPid : undefined; -} - async function readUnixListeners( port: number, ): Promise<{ listeners: PortListener[]; detail?: string; errors: string[] }> { @@ -98,10 +88,9 @@ async function readUnixListeners( if (!listener.pid) { return; } - const [commandLine, user, parentPid] = await Promise.all([ + const [commandLine, user] = await Promise.all([ resolveUnixCommandLine(listener.pid), resolveUnixUser(listener.pid), - resolveUnixParentPid(listener.pid), ]); if (commandLine) { listener.commandLine = commandLine; @@ -109,9 +98,6 @@ async function readUnixListeners( if (user) { listener.user = user; } - if (parentPid !== undefined) { - listener.ppid = parentPid; - } }), ); return { listeners, detail: res.stdout.trim() || undefined, errors }; diff --git a/src/infra/ports-types.ts b/src/infra/ports-types.ts index 827a5b3ade9..56accc93aff 100644 --- a/src/infra/ports-types.ts +++ b/src/infra/ports-types.ts @@ -1,6 +1,5 @@ export type PortListener = { pid?: number; - ppid?: number; command?: string; commandLine?: string; user?: string;