|
1 | 1 | import { TaskRunProcess, trustStoreEnv, type TaskRunProcessOptions } from "./taskRunProcess.js"; |
2 | 2 | import { describe, it, expect, vi } from "vitest"; |
| 3 | +import { execFileSync } from "node:child_process"; |
| 4 | +import { closeSync, constants, openSync } from "node:fs"; |
| 5 | +import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | +import { join } from "node:path"; |
3 | 8 | import { UncaughtExceptionError, UnexpectedExitError } from "@trigger.dev/core/v3/errors"; |
4 | 9 | import type { |
5 | 10 | TaskRunExecution, |
@@ -195,4 +200,171 @@ describe("TaskRunProcess", () => { |
195 | 200 | expect(result.message).toBe("boom"); |
196 | 201 | }); |
197 | 202 | }); |
| 203 | + |
| 204 | + describe("a child killed with SIGKILL", () => { |
| 205 | + type Cgroup = { dir: string; events: string; writeEvents: (kills: number) => Promise<void> }; |
| 206 | + |
| 207 | + // A fake cgroup whose out-of-memory counter the test writes. |
| 208 | + async function withCgroup<T>(run: (cgroup: Cgroup) => Promise<T>): Promise<T> { |
| 209 | + const dir = await mkdtemp(join(tmpdir(), "task-run-process-oom-")); |
| 210 | + try { |
| 211 | + await writeFile(join(dir, "cgroup"), "0::/run\n"); |
| 212 | + await mkdir(join(dir, "sys", "run"), { recursive: true }); |
| 213 | + const events = join(dir, "sys", "run", "memory.events"); |
| 214 | + return await run({ |
| 215 | + dir, |
| 216 | + events, |
| 217 | + writeEvents: (kills) => writeFile(events, eventsWith(kills)), |
| 218 | + }); |
| 219 | + } finally { |
| 220 | + await rm(dir, { recursive: true, force: true }); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + function eventsWith(kills: number) { |
| 225 | + return `low 0\nhigh 0\nmax 3\noom 4\noom_kill ${kills}\noom_group_kill 0\n`; |
| 226 | + } |
| 227 | + |
| 228 | + // Starts an attempt on a child running `childSource`, and returns what it fails with. |
| 229 | + async function attemptOn( |
| 230 | + cgroup: Cgroup, |
| 231 | + childSource: string, |
| 232 | + options: Partial<TaskRunProcessOptions> = {} |
| 233 | + ) { |
| 234 | + const entryPoint = join(cgroup.dir, "child.mjs"); |
| 235 | + await writeFile(entryPoint, childSource); |
| 236 | + const proc = new TaskRunProcess( |
| 237 | + createTaskRunProcessOptions({ |
| 238 | + workerManifest: { |
| 239 | + runtime: "node", |
| 240 | + workerEntryPoint: entryPoint, |
| 241 | + configEntryPoint: entryPoint, |
| 242 | + otelImportHook: {}, |
| 243 | + } as unknown as WorkerManifest, |
| 244 | + serverWorker: { id: "worker-1", version: "1", contentHash: "hash" }, |
| 245 | + env: { OOM_EVENTS: cgroup.events, OOM_EVENTS_AFTER: eventsWith(1) }, |
| 246 | + oomKillCounter: { |
| 247 | + procSelfCgroup: join(cgroup.dir, "cgroup"), |
| 248 | + cgroupRoot: join(cgroup.dir, "sys"), |
| 249 | + }, |
| 250 | + ...options, |
| 251 | + }) |
| 252 | + ).initialize(); |
| 253 | + |
| 254 | + const failure = proc |
| 255 | + .execute( |
| 256 | + { |
| 257 | + payload: { execution: createExecution("run-1", 1), traceContext: {}, metrics: [] }, |
| 258 | + messageId: "run_run-1", |
| 259 | + env: {}, |
| 260 | + }, |
| 261 | + false |
| 262 | + ) |
| 263 | + .then( |
| 264 | + () => { |
| 265 | + throw new Error("the attempt resolved"); |
| 266 | + }, |
| 267 | + (error: unknown) => TaskRunProcess.parseExecuteError(error) |
| 268 | + ); |
| 269 | + return { proc, failure }; |
| 270 | + } |
| 271 | + |
| 272 | + // Opens the FIFO for writing once its reader is waiting, so the blocked read sees end of file |
| 273 | + // and does not outlive the test. |
| 274 | + async function releaseReader(fifo: string) { |
| 275 | + const deadline = Date.now() + 5_000; |
| 276 | + while (Date.now() < deadline) { |
| 277 | + try { |
| 278 | + closeSync(openSync(fifo, constants.O_WRONLY | constants.O_NONBLOCK)); |
| 279 | + return; |
| 280 | + } catch { |
| 281 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + const IDLE_CHILD = "setInterval(() => {}, 1000);\n"; |
| 287 | + |
| 288 | + // Counts an out-of-memory kill and dies by SIGKILL as soon as the attempt reaches it. |
| 289 | + const OOM_ON_EXECUTE_CHILD = [ |
| 290 | + 'import { writeFileSync } from "node:fs";', |
| 291 | + 'process.on("message", () => {', |
| 292 | + " writeFileSync(process.env.OOM_EVENTS, process.env.OOM_EVENTS_AFTER);", |
| 293 | + ' process.kill(process.pid, "SIGKILL");', |
| 294 | + "});", |
| 295 | + "", |
| 296 | + ].join("\n"); |
| 297 | + |
| 298 | + async function killedAfterBaseline(oomKillsBefore: number, oomKillsAtKill: number) { |
| 299 | + return withCgroup(async (cgroup) => { |
| 300 | + await cgroup.writeEvents(oomKillsBefore); |
| 301 | + const { proc, failure } = await attemptOn(cgroup, IDLE_CHILD); |
| 302 | + // Let the attempt read its baseline before the kill is counted. |
| 303 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 304 | + await cgroup.writeEvents(oomKillsAtKill); |
| 305 | + process.kill(proc.pid!, "SIGKILL"); |
| 306 | + return failure; |
| 307 | + }); |
| 308 | + } |
| 309 | + |
| 310 | + // Catches: an out-of-memory kill of the child failing the attempt as a generic non-zero |
| 311 | + // exit, so the run gets the wrong error and is retried as an ordinary crash. |
| 312 | + it("reports TASK_PROCESS_OOM_KILLED when the cgroup counted an out-of-memory kill", async () => { |
| 313 | + const error = await killedAfterBaseline(0, 1); |
| 314 | + |
| 315 | + expect(error.code).toBe("TASK_PROCESS_OOM_KILLED"); |
| 316 | + expect(error.message).toBe("Process exited with code -1 after signal SIGKILL."); |
| 317 | + }); |
| 318 | + |
| 319 | + // Catches: the counter read as a total rather than a change, so one earlier out-of-memory |
| 320 | + // kill in the cgroup makes every later SIGKILL read as out of memory. |
| 321 | + it("reports a non-zero exit when no out-of-memory kill was counted during the attempt", async () => { |
| 322 | + const error = await killedAfterBaseline(1, 1); |
| 323 | + |
| 324 | + expect(error.code).toBe("TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE"); |
| 325 | + }); |
| 326 | + |
| 327 | + // Catches: the baseline read racing the attempt's dispatch, so a kill right after the |
| 328 | + // attempt starts lands in the baseline and is reported as a non-zero exit. |
| 329 | + it("counts an out-of-memory kill that happens as soon as the attempt starts", async () => { |
| 330 | + const error = await withCgroup(async (cgroup) => { |
| 331 | + await cgroup.writeEvents(0); |
| 332 | + return (await attemptOn(cgroup, OOM_ON_EXECUTE_CHILD)).failure; |
| 333 | + }); |
| 334 | + |
| 335 | + expect(error.code).toBe("TASK_PROCESS_OOM_KILLED"); |
| 336 | + }); |
| 337 | + |
| 338 | + // Catches: a child that exits while the attempt waits on the counter rejecting the attempt |
| 339 | + // before anything handles it, an unhandled rejection that can take the controller down, and |
| 340 | + // the exit then being reported as a child that was never connected. |
| 341 | + it("reports a child that exits during a slow counter read as the attempt's error", async () => { |
| 342 | + const unhandled: unknown[] = []; |
| 343 | + const onUnhandled = (reason: unknown) => unhandled.push(reason); |
| 344 | + process.on("unhandledRejection", onUnhandled); |
| 345 | + try { |
| 346 | + const error = await withCgroup(async (cgroup) => { |
| 347 | + // Nothing writes to it, so the read blocks until the test releases it. |
| 348 | + execFileSync("mkfifo", [cgroup.events]); |
| 349 | + try { |
| 350 | + const { proc, failure } = await attemptOn(cgroup, IDLE_CHILD); |
| 351 | + const exited = new Promise((resolve) => proc.onExit.attachOnce(resolve)); |
| 352 | + process.kill(proc.pid!, "SIGKILL"); |
| 353 | + await exited; |
| 354 | + // Gives Node a turn to report an unhandled rejection while the read is still blocked. |
| 355 | + await new Promise((resolve) => setImmediate(resolve)); |
| 356 | + return await failure; |
| 357 | + } finally { |
| 358 | + await releaseReader(cgroup.events); |
| 359 | + } |
| 360 | + }); |
| 361 | + |
| 362 | + expect(error.code).toBe("TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE"); |
| 363 | + expect(error.message).toBe("Process exited with code -1 after signal SIGKILL."); |
| 364 | + expect(unhandled).toEqual([]); |
| 365 | + } finally { |
| 366 | + process.off("unhandledRejection", onUnhandled); |
| 367 | + } |
| 368 | + }); |
| 369 | + }); |
198 | 370 | }); |
0 commit comments