Skip to content

Commit c8092c3

Browse files
committed
fix(test): share Bun smoke force-kill ownership
Record the successful force-kill once across the timer and post-close drain. Native Darwin traces reproduced both duplicate-signal orders; genuine permission failures and uncleared groups still fail without extending deadlines.
1 parent 825fabf commit c8092c3

2 files changed

Lines changed: 123 additions & 3 deletions

File tree

‎scripts/e2e/lib/bun-global-install/assertions.mjs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,18 @@ const runWithTimeout = async (timeout, command, commandArgs) => {
9999
let parentSignal = null;
100100
let killTimer;
101101
let killDeadlineAt = 0;
102+
let forceKillIssued = false;
103+
const forceKill = () => {
104+
// The timer and post-close drain can race while a killed group is exiting.
105+
// Share the successful signal so neither path tries to kill it twice.
106+
if (!forceKillIssued) {
107+
signalChild(child, "SIGKILL");
108+
forceKillIssued = true;
109+
}
110+
};
102111
const scheduleForceKill = () => {
103112
killDeadlineAt = Date.now() + killGrace;
104-
killTimer ??= setTimeout(() => signalChild(child, "SIGKILL"), killGrace);
113+
killTimer ??= setTimeout(forceKill, killGrace);
105114
killTimer.unref();
106115
};
107116

@@ -152,8 +161,10 @@ const runWithTimeout = async (timeout, command, commandArgs) => {
152161
await waitForProcessGroupExit(child, remainingGraceMs);
153162
}
154163
if (processGroupAlive(child)) {
155-
signalChild(child, "SIGKILL");
156-
await waitForProcessGroupExit(child, 100);
164+
forceKill();
165+
if (!(await waitForProcessGroupExit(child, 100))) {
166+
throw new Error(`command process group remained active after SIGKILL: ${command}`);
167+
}
157168
}
158169
clearTimeout(killTimer);
159170
}

‎test/scripts/test-install-sh-docker.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,115 @@ describe("install-sh smoke runner", () => {
18431843
});
18441844

18451845
describe("bun global install smoke", () => {
1846+
const runForceKillOrderingFixture = (
1847+
first: "timer" | "drain",
1848+
failure?: "permission" | "uncleared",
1849+
) => {
1850+
const tempDir = tempDirs.make("openclaw-bun-global-force-kill-");
1851+
const preloadPath = path.join(tempDir, "lifecycle.mjs");
1852+
// Drive both native-observed callback orders at the real CLI boundary.
1853+
// Only the child and clock are simulated; the helper owns all cleanup logic.
1854+
writeFileSync(
1855+
preloadPath,
1856+
`import childProcess from "node:child_process";
1857+
import { EventEmitter } from "node:events";
1858+
import { syncBuiltinESMExports } from "node:module";
1859+
let now = 0;
1860+
let alive = true;
1861+
let forceKills = 0;
1862+
const timers = [];
1863+
process.on("exit", () => console.log("force-kill-attempts=" + forceKills));
1864+
process.kill = (pid, signal) => {
1865+
if (pid !== -1234) throw new Error("unexpected fixture signal target");
1866+
if (signal === "SIGKILL") {
1867+
forceKills++;
1868+
if (${JSON.stringify(failure)} === "permission" ||
1869+
(forceKills > 1 && ${JSON.stringify(failure)} !== "uncleared")) {
1870+
throw Object.assign(new Error("kill EPERM"), { code: "EPERM" });
1871+
}
1872+
}
1873+
if (signal === 0 && !alive) {
1874+
throw Object.assign(new Error("kill ESRCH"), { code: "ESRCH" });
1875+
}
1876+
return true;
1877+
};
1878+
childProcess.spawn = () => {
1879+
const stream = () => Object.assign(new EventEmitter(), { setEncoding() {} });
1880+
const child = Object.assign(new EventEmitter(), {
1881+
pid: 1234, stdout: stream(), stderr: stream(),
1882+
});
1883+
Date.now = () => now;
1884+
globalThis.setTimeout = (callback, delay) => {
1885+
const timer = { callback, delay, cleared: false, unref() {} };
1886+
timers.push(timer);
1887+
return timer;
1888+
};
1889+
globalThis.clearTimeout = (timer) => { if (timer) timer.cleared = true; };
1890+
const fire = async (delay, at) => {
1891+
now = at;
1892+
const timer = timers.find((entry) => !entry.cleared && entry.delay === delay);
1893+
if (!timer) throw new Error("missing fixture timer: " + delay);
1894+
timer.cleared = true;
1895+
timer.callback();
1896+
await Promise.resolve();
1897+
await Promise.resolve();
1898+
};
1899+
process.nextTick(async () => {
1900+
process.emit("SIGTERM");
1901+
child.emit("close", 0, null);
1902+
await Promise.resolve();
1903+
if (${JSON.stringify(first)} === "timer") {
1904+
await fire(100, 100);
1905+
await fire(25, 100);
1906+
} else {
1907+
await fire(25, 100);
1908+
if (${JSON.stringify(failure)} === "permission") return;
1909+
await fire(100, 100);
1910+
}
1911+
alive = ${JSON.stringify(failure)} === "uncleared";
1912+
await fire(25, 200);
1913+
});
1914+
return child;
1915+
};
1916+
syncBuiltinESMExports();
1917+
`,
1918+
);
1919+
return spawnSync(
1920+
process.execPath,
1921+
["--import", preloadPath, BUN_GLOBAL_ASSERTIONS_PATH, "run-with-timeout", "60000", "fixture"],
1922+
{
1923+
encoding: "utf8",
1924+
env: { ...process.env, OPENCLAW_BUN_GLOBAL_SMOKE_TIMEOUT_KILL_GRACE_MS: "100" },
1925+
},
1926+
);
1927+
};
1928+
1929+
it.runIf(process.platform !== "win32").each(["timer", "drain"] as const)(
1930+
"force-kills Bun descendants once when the %s callback runs first",
1931+
(first) => {
1932+
const result = runForceKillOrderingFixture(first);
1933+
expect(result.status, result.stderr).toBe(143);
1934+
expect(result.stdout).toContain("force-kill-attempts=1");
1935+
},
1936+
);
1937+
1938+
it.runIf(process.platform !== "win32")("propagates Bun force-kill permission failures", () => {
1939+
const result = runForceKillOrderingFixture("drain", "permission");
1940+
expect(result.status).toBe(1);
1941+
expect(result.stderr).toContain("kill EPERM");
1942+
expect(result.stdout).toContain("force-kill-attempts=1");
1943+
});
1944+
1945+
it.runIf(process.platform !== "win32")(
1946+
"fails when a Bun process group remains after force-kill cleanup",
1947+
() => {
1948+
const result = runForceKillOrderingFixture("timer", "uncleared");
1949+
expect(result.status).toBe(1);
1950+
expect(result.stderr).toContain("command process group remained active after SIGKILL");
1951+
expect(result.stdout).toContain("force-kill-attempts=1");
1952+
},
1953+
);
1954+
18461955
it("packs the current tree and verifies the installed package runtime through Bun", () => {
18471956
const script = readFileSync(BUN_GLOBAL_SMOKE_PATH, "utf8");
18481957
const assertions = readFileSync(BUN_GLOBAL_ASSERTIONS_PATH, "utf8");

0 commit comments

Comments
 (0)