fix(run): kill a worker that blocks its event loop past the test timeout

Merged
OK
2mo ago
+597-32

A test body that blocks the worker's event loop (while (true) {}) never timed out: the test timeout is a timer inside the worker, which never fires, and the runner had no deadline of its own. The run hung until someone killed the worker process. Ctrl-C did not end it either.

  • The worker sends attempt-deadline (test timeout and cleanupTimeout) when an attempt's timeout starts, and attempt-end when the attempt ends (packages/e2e/src/run/execute.ts, worker/session.ts).
  • From the deadline on, the scheduler sends ping once per cleanupTimeout (at least 5 s). The worker answers pong right away, outside its unit queue. A worker that misses one is SIGKILLed through the existing crash path (SchedulerWorker.watch in packages/e2e/src/run/scheduler.ts). A worker still tearing down a timed-out attempt keeps answering and is not killed.
  • The hung test is reported timed-out with TEST_TIMEOUT (exit 1, no WORKER_EXIT run error). The file's tests it never reached are put back in the queue and run on a new worker, as they would after a normal timeout. If the target failed meanwhile (workers could not boot), they are skipped instead of left in a queue nothing reads.
  • worker/entry.ts send now passes every message to the channel right away. It used to wait for the previous message's write callback, so an attempt-deadline sent just before a blocking body (for example a serial member right after the previous one) never left the process.
  • No watchdog for a worker with a debugger attached when the attempt starts (editor auto-attach, NODE_OPTIONS=--inspect=0), so a paused breakpoint is not killed. In-process runs (inline config, in-memory tests) share the runner's event loop, so nothing can enforce the timeout there. The docs say so.
  • Builds on #837: the kill goes through its crash bookkeeping (synthesizeCrashResults). The attempts finished before the hung one stand, and the hung one is appended at its index as timed-out / TEST_TIMEOUT, with the id and start the worker sent in attempt-deadline. It is not retried. A hung serial member settles through SerialGroupProgress.crashed with status timed-out: earlier members keep their records, the ones after it skip.
  • The send change in worker/entry.ts was the same in #837, so there is one implementation now.
sequenceDiagram
  participant S as scheduler
  participant W as worker
  W->>S: attempt-deadline (timeout, cleanupTimeout)
  Note over W: body blocks the event loop
  S->>W: ping (at timeout)
  Note over S: no pong within cleanupTimeout (>= 5 s)
  S->>W: SIGKILL
  S->>S: report timed-out, requeue unreached tests

Verified

Ran it locally: yes

  • testbed, a scratch tests/zz-busy.e2e.ts (a 2 s timeout test that runs app.open() and then while (true) {}, followed by a passing test), node node_modules/e2e/dist/cli/bin.js run tests/zz-busy.e2e.ts --workers 1 with the default 30 s cleanupTimeout.
mainthis branch
Hung. timeout 120 sent SIGTERM and the run still did not end. It finished only after I kill -9'd the spinning worker at 212 s, with both tests skipped and a WORKER_EXIT errorEnded in 34 s, exit 1

main:

 ERROR  infrastructure error WORKER_EXIT
worker for target "web" exited unexpectedly (code null, signal SIGKILL) during file::web::tests/zz-busy.e2e.ts

 Test Files  0 passed | 1 skipped (1)
      Tests  0 passed | 2 skipped (2)
   Duration  211.96s (3m 32s, startup 86ms)

this branch:

 ❯ |web| tests/zz-busy.e2e.ts (2 tests | 1 failed) 32.13s
   × blocks the event loop (timed-out) 32.01s
   ✓ runs after it 123ms

 FAIL  |web| tests/zz-busy.e2e.ts > blocks the event loop
TEST_TIMEOUT: test ran past its 2000 ms timeout and its worker stopped responding, so the worker was killed

      Tests  1 failed | 1 passed (2)
   Duration  32.82s

(The branch output above was captured before the message wording changed. The behavior is the same.)

After the rebase on #837, same testbed setup with retries: 1, attempt 0 throws, attempt 1 while (true) {}: exit 1 in 34 s, report.json attempts [[0, failed, ERROR "first attempt fails"], [1, timed-out, TEST_TIMEOUT]], the next test passed.

Tests: new integration tests in parallel-workers.test.ts cover a body that blocks immediately, one that blocks after a step, a serial member, and a slow teardown that answers pings and is left alone. The first one hangs on main, and the slow-teardown test fails if the worker's ping handler is removed. Scheduler unit tests cover the kill and requeue, and pings that are answered. Added for the #837 integration: the retry case above (integration and unit) and a target that fails while a worker hangs (unit, hung forever before the fix). Mutation-checked: dropping the kept attempts or the serial timed-out status fails them.