|
1 | 1 | // Run Tsgo tests cover run tsgo script behavior. |
2 | | -import { spawnSync } from "node:child_process"; |
| 2 | +import { spawn, spawnSync } from "node:child_process"; |
3 | 3 | import fs from "node:fs"; |
4 | 4 | import path from "node:path"; |
5 | 5 | import { describe, expect, it } from "vitest"; |
|
8 | 8 | getSparseTsgoGuardError, |
9 | 9 | shouldSkipSparseTsgoGuardError, |
10 | 10 | } from "../../scripts/lib/tsgo-sparse-guard.mts"; |
| 11 | +import { resolveTsgoTimeoutMs } from "../../scripts/run-tsgo.mts"; |
| 12 | +import { waitForChildClose, waitForDead, waitForPidFile } from "../helpers/process-wait.js"; |
11 | 13 | import { createScriptTestHarness } from "./test-helpers.js"; |
12 | 14 |
|
13 | 15 | const { createTempDir } = createScriptTestHarness(); |
@@ -232,3 +234,179 @@ describe("run-tsgo sparse guard", () => { |
232 | 234 | }); |
233 | 235 | }); |
234 | 236 | }); |
| 237 | + |
| 238 | +describe.skipIf(process.platform === "win32")("run-tsgo watchdog", () => { |
| 239 | + it("keeps the watchdog opt-in", () => { |
| 240 | + expect(resolveTsgoTimeoutMs({})).toBeUndefined(); |
| 241 | + expect(resolveTsgoTimeoutMs({ OPENCLAW_TSGO_TIMEOUT_MS: " " })).toBeUndefined(); |
| 242 | + expect(resolveTsgoTimeoutMs({ OPENCLAW_TSGO_TIMEOUT_MS: "30000" })).toBe(30_000); |
| 243 | + }); |
| 244 | + |
| 245 | + function writeFakeTsgo(cwd: string, body: string) { |
| 246 | + const binDir = path.join(cwd, "node_modules", ".bin"); |
| 247 | + fs.mkdirSync(binDir, { recursive: true }); |
| 248 | + const fakeTsgo = path.join(binDir, "tsgo"); |
| 249 | + fs.writeFileSync(fakeTsgo, body, "utf8"); |
| 250 | + fs.chmodSync(fakeTsgo, 0o755); |
| 251 | + } |
| 252 | + |
| 253 | + // The fake compiler is a grandchild in its own process group, so spawnSync's |
| 254 | + // killSignal never reaches it. Its recorded pid is the only handle the harness |
| 255 | + // has to tear the tree down when the outer timeout fires on a pre-fix run. |
| 256 | + function readFakeTsgoPid(cwd: string) { |
| 257 | + const pidFile = path.join(cwd, "fake-tsgo.pid"); |
| 258 | + if (!fs.existsSync(pidFile)) { |
| 259 | + return undefined; |
| 260 | + } |
| 261 | + const pid = Number.parseInt(fs.readFileSync(pidFile, "utf8").trim(), 10); |
| 262 | + return Number.isInteger(pid) && pid > 1 ? pid : undefined; |
| 263 | + } |
| 264 | + |
| 265 | + function reapFakeTsgo(cwd: string) { |
| 266 | + const pid = readFakeTsgoPid(cwd); |
| 267 | + if (pid === undefined) { |
| 268 | + return; |
| 269 | + } |
| 270 | + for (const target of [-pid, pid]) { |
| 271 | + try { |
| 272 | + process.kill(target, "SIGKILL"); |
| 273 | + } catch { |
| 274 | + // Already reaped by the watchdog under test. |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + function runFakeTsgo( |
| 280 | + cwd: string, |
| 281 | + timeoutMs: string | undefined, |
| 282 | + onBeforeReap?: (pid: number | undefined) => void, |
| 283 | + ) { |
| 284 | + const { OPENCLAW_TSGO_TIMEOUT_MS: _unset, ...baseEnv } = process.env; |
| 285 | + try { |
| 286 | + return spawnSync( |
| 287 | + process.execPath, |
| 288 | + [path.resolve("scripts/run-tsgo.mjs"), "-p", "tsconfig.extensions.json"], |
| 289 | + { |
| 290 | + cwd, |
| 291 | + encoding: "utf8", |
| 292 | + env: |
| 293 | + timeoutMs === undefined ? baseEnv : { ...baseEnv, OPENCLAW_TSGO_TIMEOUT_MS: timeoutMs }, |
| 294 | + // spawnSync blocks this thread, so vitest's own per-test budget can never |
| 295 | + // fire; a regression here would hang the worker instead of failing. |
| 296 | + timeout: 25_000, |
| 297 | + killSignal: "SIGKILL", |
| 298 | + }, |
| 299 | + ); |
| 300 | + } finally { |
| 301 | + onBeforeReap?.(readFakeTsgoPid(cwd)); |
| 302 | + reapFakeTsgo(cwd); |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + it.each([{ bound: "0" }, { bound: "abc" }])( |
| 307 | + "explains a rejected OPENCLAW_TSGO_TIMEOUT_MS of $bound instead of crashing", |
| 308 | + ({ bound }) => { |
| 309 | + const cwd = createTempDir("openclaw-run-tsgo-watchdog-"); |
| 310 | + writeFakeTsgo(cwd, "#!/bin/sh\nexit 0\n"); |
| 311 | + |
| 312 | + const result = runFakeTsgo(cwd, bound); |
| 313 | + |
| 314 | + expect(result.status).toBe(1); |
| 315 | + expect(result.stderr).toContain("must be plain decimal digits"); |
| 316 | + expect(result.stderr).toContain("Unset it to disable the watchdog"); |
| 317 | + expect(result.stderr).not.toContain("at readPositiveEnvInt"); |
| 318 | + expect(result.stderr.trim().split("\n").at(-1)).toBe("[tsgo] FAILED (exit 1)"); |
| 319 | + }, |
| 320 | + 30_000, |
| 321 | + ); |
| 322 | + |
| 323 | + it("kills a wedged tsgo that ignores SIGTERM instead of blocking its caller forever", () => { |
| 324 | + const cwd = createTempDir("openclaw-run-tsgo-watchdog-"); |
| 325 | + // Mirrors the observed wedge: the checker refuses SIGTERM and never reports, |
| 326 | + // so only a process-group SIGKILL frees the caller. It records its pid so the |
| 327 | + // harness can reap the tree, and self-exits as a last-resort backstop. |
| 328 | + writeFakeTsgo( |
| 329 | + cwd, |
| 330 | + '#!/bin/sh\necho $$ > "$(dirname "$0")/../../fake-tsgo.pid"\ntrap \'\' TERM\ni=0\nwhile [ $i -lt 60 ]; do sleep 1; i=$((i+1)); done\n', |
| 331 | + ); |
| 332 | + |
| 333 | + const observedBeforeReap = { |
| 334 | + error: undefined as unknown, |
| 335 | + pid: undefined as number | undefined, |
| 336 | + }; |
| 337 | + const result = runFakeTsgo(cwd, "2000", (pid) => { |
| 338 | + observedBeforeReap.pid = pid; |
| 339 | + if (pid === undefined) { |
| 340 | + return; |
| 341 | + } |
| 342 | + try { |
| 343 | + process.kill(pid, 0); |
| 344 | + } catch (error) { |
| 345 | + observedBeforeReap.error = error; |
| 346 | + } |
| 347 | + }); |
| 348 | + |
| 349 | + expect(result.status).toBe(1); |
| 350 | + expect(result.stderr).toContain("killed the tsgo process tree"); |
| 351 | + // Printing the message is not the contract; the tree actually being gone is. |
| 352 | + expect(observedBeforeReap.pid).toBeDefined(); |
| 353 | + expect(observedBeforeReap.error).toMatchObject({ code: "ESRCH" }); |
| 354 | + expect(result.stderr.trim().split("\n").at(-1)).toBe("[tsgo] FAILED (exit 1)"); |
| 355 | + }, 30_000); |
| 356 | + |
| 357 | + it("lets the inner supervisor reap a wedged compiler before the wrapper exits on SIGTERM", async () => { |
| 358 | + const cwd = createTempDir("openclaw-run-tsgo-signal-"); |
| 359 | + const pidFile = path.join(cwd, "fake-tsgo.pid"); |
| 360 | + fs.writeFileSync(path.join(cwd, "tsconfig.extensions.json"), "{}\n"); |
| 361 | + writeFakeTsgo( |
| 362 | + cwd, |
| 363 | + '#!/bin/sh\necho $$ > "$(dirname "$0")/../../fake-tsgo.pid"\ntrap \'\' TERM HUP INT\nwhile true; do sleep 1; done\n', |
| 364 | + ); |
| 365 | + const wrapper = spawn( |
| 366 | + process.execPath, |
| 367 | + [path.resolve("scripts/run-tsgo.mjs"), "-p", "tsconfig.extensions.json"], |
| 368 | + { cwd, stdio: "ignore" }, |
| 369 | + ); |
| 370 | + |
| 371 | + try { |
| 372 | + const compilerPid = await waitForPidFile(pidFile, 10_000); |
| 373 | + wrapper.kill("SIGTERM"); |
| 374 | + |
| 375 | + await expect(waitForChildClose(wrapper, 15_000)).resolves.toEqual({ |
| 376 | + code: 143, |
| 377 | + signal: null, |
| 378 | + }); |
| 379 | + await expect(waitForDead(compilerPid, 2_000)).resolves.toBeUndefined(); |
| 380 | + } finally { |
| 381 | + if (wrapper.exitCode === null && wrapper.signalCode === null) { |
| 382 | + wrapper.kill("SIGKILL"); |
| 383 | + } |
| 384 | + reapFakeTsgo(cwd); |
| 385 | + } |
| 386 | + }, 20_000); |
| 387 | + |
| 388 | + // Every bound that must leave a completing compiler alone. The ceiling case is the |
| 389 | + // regression that matters: without saturation Node collapses the delay to 1ms and |
| 390 | + // would kill this sleeping child immediately. |
| 391 | + it.each([ |
| 392 | + { bound: undefined, name: "the disabled watchdog", body: "#!/bin/sh\nsleep 2\nexit 0\n" }, |
| 393 | + { bound: "30000", name: "an explicit bound", body: "#!/bin/sh\nexit 0\n" }, |
| 394 | + { |
| 395 | + bound: "2147483648", |
| 396 | + name: "an override past Node's timer ceiling", |
| 397 | + body: "#!/bin/sh\nsleep 1\nexit 0\n", |
| 398 | + }, |
| 399 | + ])( |
| 400 | + "leaves a completing tsgo alone under $name", |
| 401 | + ({ bound, body }) => { |
| 402 | + const cwd = createTempDir("openclaw-run-tsgo-watchdog-"); |
| 403 | + writeFakeTsgo(cwd, body); |
| 404 | + |
| 405 | + const result = runFakeTsgo(cwd, bound); |
| 406 | + |
| 407 | + expect(result.status).toBe(0); |
| 408 | + expect(result.stderr).not.toContain("killed the tsgo process tree"); |
| 409 | + }, |
| 410 | + 30_000, |
| 411 | + ); |
| 412 | +}); |
0 commit comments