fix(cli): close undici global dispatcher on exit - #1488
Merged
Conversation
Ordinary commands left Node's global fetch (undici) keep-alive sockets pooled after finishing their work, keeping the event loop referenced so the process lingered on macOS (#1237). #1396 added a force-exit timer as a safety net; this addresses the root cause by closing the global dispatcher in runCli's finally, releasing the pooled sockets so the loop drains on its own. The timer stays as a backstop for handles the close can't reach. Refs #1237
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Author
|
The review thread points to code on the PR branch |
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit d66321c. Configure here.
Bot review flagged that an unguarded `await closeGlobalDispatcher()` in runCli's finally could reject (masking the command result, forcing exit 1) or hang (never arming the force-exit backstop). - Arm scheduleForceExit() before the await so the backstop fires regardless of teardown outcome. - Make closeGlobalDispatcher never reject (try/catch) and use destroy() instead of close() so it aborts in-flight requests immediately rather than waiting for them. Refs #1237
Contributor
Author
|
addressed the bot review findings in 3a2b750:
added a regression test for the rejection case. both review threads resolved. |
BYK
approved these changes
Aug 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Follow-up to #1396, which added a force-exit timer as a safety net for the process-hang (#1237). This addresses the root cause.
Root cause
Ordinary commands (`org list`, `project list`, `auth status`, …) run through Node's global `fetch` (undici), which keeps a pool of keep-alive sockets open after the response. Those pooled sockets keep the event loop referenced, so the process lingers instead of exiting on its own — most visibly on macOS.
Fix
Close undici's global dispatcher in `runCli`'s `finally`, after all recovery middleware has reached a terminal result. That releases the pooled sockets so the loop drains naturally. No new dependency: the dispatcher is reached via the well-known `Symbol.for("undici.globalDispatcher.1")` and closed only if present, so it's a no-op under any runtime that doesn't expose it.
The macOS force-exit timer from #1396 stays armed as a backstop for any handle the dispatcher close can't reach (e.g. a libuv refcount quirk). It's unref'd, so it remains a no-op on clean exits.
Tests
Refs #1237