@@ -1843,6 +1843,115 @@ describe("install-sh smoke runner", () => {
18431843} ) ;
18441844
18451845describe ( "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