Skip to content

fix(cli): exit when the event loop drains after the server probe (#1174) - #1179

Merged
agegr merged 1 commit into
mainfrom
fix/1174-cli-exit-after-fetch
Oct 11, 2026
Merged

agegr merged 1 commit into
mainfrom
fix/1174-cli-exit-after-fetch

Conversation

@agegr

@agegr agegr commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Closes #1174.

Problem

On Windows, pi-web status, open, update and the error paths of stop printed the right output but exited with 0xC0000409 once a run record existed:

Assertion failed: !(handle->flags & UV_HANDLE_CLOSING), file src\win\async.c

bin/pi-web.js called process.exit(code) straight after listRunRecords() had probed each server with fetch(). That is nodejs/node#56645 (minimal repro in nodejs/node#58091). It affects Windows only, from Node 23 through 24.19 and 26.6, and is fixed by nodejs/node#61999, released in 24.20.0 and 26.7.0. The reporter runs 24.18.0.

Change

  • bin/pi-web.js: exitWhenIdle(code) sets process.exitCode and lets the event loop drain. An unref'd 2 s timer calls process.exit(code) only if something still holds the loop open. Both the success and error paths use it.
  • bin/run-records.js: isServerAnswering() cancels the probe's response body, so its connection is released before exit. It is still a GET, and any HTTP status still counts, so a password-protected server answering 401 is still listed.

Verification (macOS; Windows not available)

  • status, open, update --check, and stop --port N / open --port N with no match all keep their output and exit codes. They finish as quickly as before (status about 60 ms, update about 410 ms), with responses that are short, 1 MB, never-ending, or 401.
  • With something forced to keep the loop alive, the fallback exits after about 2.05 s with the right code.
  • New tests in lib/pi-web-cli-commands.test.mjs:
    • the probe drops the body, and a 204 with no body is handled;
    • status against a real server on a temp port exits 0 on its own in under 1.5 s (about 95 ms here), so it doesn't rely on the fallback.
  • tsc --noEmit, eslint, and the CLI tests pass.
  • Not tested on Windows. That a natural exit avoids the assertion comes from the upstream analysis. Upgrading Node to 24.20+ or 26.7+ also fixes it.

🤖 Generated with Claude Code

On Windows, Node.js 23 up to 24.19 / 26.6 can abort with a libuv
UV_HANDLE_CLOSING assertion (nodejs/node#56645, fixed in 24.20.0 and
26.7.0) when process.exit() runs right after a fetch(). status, open,
update and the error paths of stop did exactly that once a run record
existed, so they exited 0xC0000409 despite correct output.

Set process.exitCode and let the loop drain, with an unref'd 2 s
process.exit() fallback, and drop the probe's response body so its
connection is released before exit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@agegr
agegr merged commit 5abdee3 into main Oct 11, 2026
2 checks passed
@agegr
agegr deleted the fix/1174-cli-exit-after-fetch branch October 29, 2026 13:01
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…gr#1174) (agegr#1179)

On Windows, Node.js 23 up to 24.19 / 26.6 can abort with a libuv
UV_HANDLE_CLOSING assertion (nodejs/node#56645, fixed in 24.20.0 and
26.7.0) when process.exit() runs right after a fetch(). status, open,
update and the error paths of stop did exactly that once a run record
existed, so they exited 0xC0000409 despite correct output.

Set process.exitCode and let the loop drain, with an unref'd 2 s
process.exit() fallback, and drop the probe's response body so its
connection is released before exit.

Co-authored-by: Claude Opus 5.5 <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…gr#1174) (agegr#1179)

On Windows, Node.js 23 up to 24.19 / 26.6 can abort with a libuv
UV_HANDLE_CLOSING assertion (nodejs/node#56645, fixed in 24.20.0 and
26.7.0) when process.exit() runs right after a fetch(). status, open,
update and the error paths of stop did exactly that once a run record
existed, so they exited 0xC0000409 despite correct output.

Set process.exitCode and let the loop drain, with an unref'd 2 s
process.exit() fallback, and drop the probe's response body so its
connection is released before exit.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows] pi-web status/open/update abort with 0xC0000409 (libuv win/async.c assertion) when a run record exists

1 participant