fix(cli): exit when a timer or socket keeps the process alive after the command

Merged
TH
18d ago
+193-6

Fixes ENG-958.

What and why

  • The CLI process imports e2e.config.ts and, to collect tests, every test file (packages/e2e/src/collect/collect.ts, importModule). A timer or socket either one opens at import, such as a database pool, a keep-alive agent, or a setInterval, keeps e2e run and e2e list alive after the report is written and the exit code is set. e2e mcp stays alive the same way after its client disconnects.
  • bin.ts calls exitIfStopped() (packages/e2e/src/cli/run-output.ts, added in #1206), which exits only after Ctrl-C or when the run stopped waiting for a test filter or a reporter. Any other run ends only once nothing is left open. This change builds on it and on drainRunnerOutput() from the same file.
  • New exitAfterGrace() in run-output.ts, called from bin.ts after exitIfStopped(), starts an unref'd 2 s timer:
    • A process with nothing left open ends on its own first, as today. beforeExit hooks and short timers left by user code still print (tests/integration/run-user-output.test.ts).
    • A process still alive when the timer fires waits for app stops already in flight (new stopsSettled() in packages/e2e/src/run/managed-process.ts), so an app or service command keeps its full shutdownTimeout. Then it kills any app process group e2e started and nothing is stopping (killManagedProcessGroups), drains its output, and exits with the exit code the command set.
  • exitIfStopped() and its work-left-behind flag are unchanged.

Verified

Ran it locally: yes

  • Testbed, the built CLI, a scratch target whose test file or config calls setInterval(() => {}, 60_000) at import. A command still running after 25 s was killed.
main (69e123b1)this branch
No open timer, e2e runexit 0, 0.9 sexit 0, 0.9 s
Timer in a test file, e2e runstill running, killedexit 0, 2.9 s
Timer in the config, e2e runstill running, killedexit 0, 2.9 s
Timer in a test file, e2e liststill running, killedexit 0, 2.4 s
Timer in the config, e2e mcp: open_session, then the client closes stdinstill running after 20 sexit 0, 2.0 s after the disconnect
# tests-eng958/page.e2e.ts calls setInterval(() => {}, 60_000) at import; this branch
$ e2e run --config e2e.eng958.config.ts
 ✓ target "web" command ready 89ms
 ✓ |web| tests-eng958/page.e2e.ts (1 test) 144ms
   ✓ page renders 144ms
 Test Files  1 passed (1)
      Tests  1 passed (1)
   Duration  461ms (startup 89ms)
exit 0 after 2.8s
  • e2e run --watch with the timer keeps watching after the first run and exits on Ctrl-C.
  • The new integration tests hang on main and pass here: run and list exit codes with an open timer, and a 400-test --reporter json report through a pipe with an open timer (cli-run.test.ts, 60 s spawn limit); e2e mcp after a disconnect with a timer in the config (mcp-disconnect.test.ts, 20 s).
  • Exit codes the command sets survive the grace exit: a passed test whose cleanup failed (#1233) with a timer in the config exits 3 (cli-run.test.ts, hangs on main).
  • On ee901b33 (main before #1206), an iOS simulator run and a web plus iOS run with no open timer exited as soon as the command was done.