mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-29 19:01:44 +00:00
fix(process): wait for windows exit code settlement
This commit is contained in:
@@ -330,7 +330,7 @@ export async function runCommandWithTimeout(
|
||||
child.stderr?.destroy();
|
||||
}, 250);
|
||||
});
|
||||
child.on("close", (code, signal) => {
|
||||
const resolveFromClose = (code: number | null, signal: NodeJS.Signals | null) => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
@@ -363,6 +363,19 @@ export async function runCommandWithTimeout(
|
||||
termination,
|
||||
noOutputTimedOut,
|
||||
});
|
||||
};
|
||||
child.on("close", (code, signal) => {
|
||||
if (
|
||||
childExitState == null &&
|
||||
code == null &&
|
||||
signal == null &&
|
||||
child.exitCode == null &&
|
||||
child.signalCode == null
|
||||
) {
|
||||
setImmediate(() => resolveFromClose(code, signal));
|
||||
return;
|
||||
}
|
||||
resolveFromClose(code, signal);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ function createMockChild(params?: {
|
||||
closeCode?: number | null;
|
||||
closeSignal?: NodeJS.Signals | null;
|
||||
exitCode?: number | null;
|
||||
exitCodeAfterClose?: number | null;
|
||||
signal?: NodeJS.Signals | null;
|
||||
}): MockChild {
|
||||
const child = new EventEmitter() as MockChild;
|
||||
@@ -47,6 +48,11 @@ function createMockChild(params?: {
|
||||
child.killed = false;
|
||||
queueMicrotask(() => {
|
||||
child.emit("close", params?.closeCode ?? 0, params?.closeSignal ?? params?.signal ?? null);
|
||||
if (params?.exitCodeAfterClose !== undefined) {
|
||||
setImmediate(() => {
|
||||
child.exitCode = params.exitCodeAfterClose ?? null;
|
||||
});
|
||||
}
|
||||
});
|
||||
return child;
|
||||
}
|
||||
@@ -117,6 +123,20 @@ describe("windows command wrapper behavior", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("waits one tick when Windows close reports null before exitCode settles", async () => {
|
||||
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
|
||||
const child = createMockChild({ closeCode: null, exitCode: null, exitCodeAfterClose: 0 });
|
||||
|
||||
spawnMock.mockImplementation(() => child);
|
||||
|
||||
try {
|
||||
const result = await runCommandWithTimeout(["npm", "--version"], { timeoutMs: 1000 });
|
||||
expect(result.code).toBe(0);
|
||||
} finally {
|
||||
platformSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("uses cmd.exe wrapper with windowsVerbatimArguments in runExec for .cmd shims", async () => {
|
||||
const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
|
||||
const expectedComSpec = process.env.ComSpec ?? "cmd.exe";
|
||||
|
||||
Reference in New Issue
Block a user