Skip to content

Commit 34d11d5

Browse files
committed
fix(gateway): recognize Windows gateway listeners via PowerShell
1 parent 4bd6dd7 commit 34d11d5

3 files changed

Lines changed: 103 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Docs: https://docs.openclaw.ai
2626
- CLI/status: keep default text `openclaw status --usage` on metadata-only channel scans unless `--deep` or `--all` is set, and send stray `openclaw tools --help` through the precomputed root-help fast path so latency-triage commands avoid plugin/runtime cold loads before printing. Refs #73477 and #74220. Thanks @oromeis and @NianJiuZst.
2727
- Agents/diagnostics: trace embedded-run startup and preparation stage timings before model I/O, and warn only on severe slow stages, so Docker/VPS latency reports can identify whether plugin loading, auth/model resolution, tool inventory, bootstrap, MCP/LSP, resource loading, or stream setup is dominating pre-run latency without noisy normal logs. Refs #73428. Thanks @Dimaoggg, @quangtran88, and @Heyvhuang.
2828
- Gateway/clients: wait for the event loop to become responsive before opening Gateway WebSocket RPC/probe/client connections while charging that readiness wait to caller timeouts, so Windows deferred module-evaluation stalls no longer turn healthy loopback gateways into false handshake timeouts across status, TUI, ACP, MCP, node-host, and plugin client paths. Refs #74279 and #48270. Thanks @wongcode and @joost-heijden.
29+
- Gateway/Windows: read listener command lines via PowerShell before falling back to `wmic`, so restart health can recognize OpenClaw listeners on modern Windows installs and avoid long anonymous-port waits. Refs #74280. Thanks @zym951223.
2930
- Plugins/runtime-deps: memoize packaged bundled runtime dist-mirror preparation after the first successful pass while keeping source-checkout mirrors refreshable, so constrained Docker/VPS installs avoid repeated root scans before chat turns. Refs #73428, #73421, #73532, and #73477. Thanks @Dimaoggg, @oromeis, @oadiazp, @jmfraga, @bstanbury, @antoniusfelix, and @jkobject.
3031
- Channels/Discord: treat bare numeric outbound targets that match the effective Discord DM allowlist as user DMs while preserving account-specific legacy `dm.allowFrom` precedence over inherited root `allowFrom`. (#74303) Thanks @Squirbie.
3132
- Control UI: make the chat sidebar split divider focusable, keyboard-resizable, ARIA-described, and pointer-event based so sidebar resizing works without a mouse. Thanks @BunsDev.

‎src/infra/ports-inspect.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,20 @@ async function resolveWindowsImageName(pid: number): Promise<string | undefined>
250250
}
251251

252252
async function resolveWindowsCommandLine(pid: number): Promise<string | undefined> {
253-
const res = await runCommandSafe([
253+
const powershell = await runCommandSafe([
254+
"powershell",
255+
"-NoProfile",
256+
"-Command",
257+
`(Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}" | Select-Object -ExpandProperty CommandLine)`,
258+
]);
259+
if (powershell.code === 0) {
260+
const value = powershell.stdout.trim();
261+
if (value) {
262+
return value;
263+
}
264+
}
265+
266+
const wmic = await runCommandSafe([
254267
"wmic",
255268
"process",
256269
"where",
@@ -259,10 +272,10 @@ async function resolveWindowsCommandLine(pid: number): Promise<string | undefine
259272
"CommandLine",
260273
"/value",
261274
]);
262-
if (res.code !== 0) {
275+
if (wmic.code !== 0) {
263276
return undefined;
264277
}
265-
for (const rawLine of res.stdout.split(/\r?\n/)) {
278+
for (const rawLine of wmic.stdout.split(/\r?\n/)) {
266279
const line = rawLine.trim();
267280
if (!normalizeLowercaseStringOrEmpty(line).startsWith("commandline=")) {
268281
continue;

‎src/infra/ports.test.ts‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import net from "node:net";
2-
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
33
import { stripAnsi } from "../terminal/ansi.js";
44

55
const runCommandWithTimeoutMock = vi.hoisted(() => vi.fn());
@@ -14,6 +14,14 @@ let handlePortError: typeof import("./ports.js").handlePortError;
1414
let PortInUseError: typeof import("./ports.js").PortInUseError;
1515

1616
const describeUnix = process.platform === "win32" ? describe.skip : describe;
17+
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
18+
19+
function setPlatform(platform: NodeJS.Platform): void {
20+
Object.defineProperty(process, "platform", {
21+
value: platform,
22+
configurable: true,
23+
});
24+
}
1725

1826
async function listenServer(
1927
server: net.Server,
@@ -53,6 +61,12 @@ beforeEach(() => {
5361
runCommandWithTimeoutMock.mockReset();
5462
});
5563

64+
afterEach(() => {
65+
if (originalPlatformDescriptor) {
66+
Object.defineProperty(process, "platform", originalPlatformDescriptor);
67+
}
68+
});
69+
5670
describe("ports helpers", () => {
5771
it("ensurePortAvailable rejects when port busy", async () => {
5872
const server = net.createServer();
@@ -183,3 +197,74 @@ describeUnix("inspectPortUsage", () => {
183197
}
184198
});
185199
});
200+
201+
describe("inspectPortUsage on Windows", () => {
202+
it("uses PowerShell process command lines to classify OpenClaw listeners", async () => {
203+
setPlatform("win32");
204+
runCommandWithTimeoutMock.mockImplementation(async (argv: string[]) => {
205+
const [command] = argv;
206+
if (command === "netstat") {
207+
return {
208+
stdout: " TCP 127.0.0.1:18789 0.0.0.0:0 LISTENING 4242\r\n",
209+
stderr: "",
210+
code: 0,
211+
};
212+
}
213+
if (command === "tasklist") {
214+
return { stdout: "Image Name: node.exe\r\n", stderr: "", code: 0 };
215+
}
216+
if (command === "powershell") {
217+
return {
218+
stdout:
219+
'"C:\\Program Files\\nodejs\\node.exe" C:\\Users\\me\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\index.js gateway run\r\n',
220+
stderr: "",
221+
code: 0,
222+
};
223+
}
224+
return { stdout: "", stderr: "", code: 1 };
225+
});
226+
227+
const result = await inspectPortUsage(18789);
228+
229+
expect(result.status).toBe("busy");
230+
expect(result.listeners).toHaveLength(1);
231+
expect(result.listeners[0]?.command).toBe("node.exe");
232+
expect(result.listeners[0]?.commandLine).toContain("openclaw");
233+
expect(result.hints.some((hint) => hint.includes("Gateway already running locally"))).toBe(
234+
true,
235+
);
236+
});
237+
238+
it("falls back to wmic when PowerShell cannot read the command line", async () => {
239+
setPlatform("win32");
240+
runCommandWithTimeoutMock.mockImplementation(async (argv: string[]) => {
241+
const [command] = argv;
242+
if (command === "netstat") {
243+
return {
244+
stdout: " TCP 127.0.0.1:18789 0.0.0.0:0 LISTENING 4242\r\n",
245+
stderr: "",
246+
code: 0,
247+
};
248+
}
249+
if (command === "tasklist") {
250+
return { stdout: "Image Name: node.exe\r\n", stderr: "", code: 0 };
251+
}
252+
if (command === "powershell") {
253+
return { stdout: "", stderr: "access denied", code: 1 };
254+
}
255+
if (command === "wmic") {
256+
return {
257+
stdout: "CommandLine=node.exe C:\\openclaw\\dist\\index.js gateway run\r\n",
258+
stderr: "",
259+
code: 0,
260+
};
261+
}
262+
return { stdout: "", stderr: "", code: 1 };
263+
});
264+
265+
const result = await inspectPortUsage(18789);
266+
267+
expect(result.listeners[0]?.commandLine).toContain("openclaw");
268+
expect(runCommandWithTimeoutMock.mock.calls.some(([argv]) => argv[0] === "wmic")).toBe(true);
269+
});
270+
});

0 commit comments

Comments
 (0)