#12987: fix(cli): exit process after successful command completion
cli
Cluster:
CLI Enhancements and Fixes
## Summary
Fixes CLI commands hanging indefinitely after completion due to persistent connections from eagerly-imported messaging providers.
Closes #12904
## Changes
- **`run-main.ts`**: Added `process.exit(0)` after both `tryRouteCli()` and `program.parseAsync()` success paths
## Why It's Safe
Long-lived commands (`gateway start`, `tui`, `agent --interactive`) never return from `parseAsync()` — they run until interrupted. The `process.exit(0)` only fires for short-lived commands that complete normally. Error paths already call `process.exit(1)`.
## Testing
- Added test verifying `process.exit(0)` is called after routed command completes
- All 5 tests pass (4 existing + 1 new)
🤖 Generated with Claude (AI-assisted)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates the CLI entry flow (`src/cli/run-main.ts`) to force the Node.js process to exit with code 0 after successful command completion. It adds `process.exit(0)` both when a command is handled by the routing layer (`tryRouteCli`) and after `commander` finishes `program.parseAsync()`, addressing hangs caused by eagerly-imported modules keeping the event loop alive.
A new unit test was added in `src/cli/run-main.test.ts` to assert that a routed command triggers `process.exit(0)`.
<h3>Confidence Score: 3/5</h3>
- This PR is close to safe, but the new test setup is likely incorrect and can be flaky due to mocking order.
- The `process.exit(0)` behavior change is straightforward and targeted, but the added test registers `vi.mock()` after importing `./run-main.js` at module scope, so the mock may not take effect reliably; this undermines the test’s ability to validate the change and could cause nondeterministic failures.
- src/cli/run-main.test.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#15306: fix: explicit exit after onboarding command completes
by jeroenbaas · 2026-02-13
84.1%
#6776: fix: CLI commands hang/fail on Fly.io deployments
by jckw · 2026-02-02
79.6%
#12656: fix: install unhandled rejection handler before async boot operations
by kiranirabatti · 2026-02-09
78.6%
#5823: fix(config): exit cleanly on invalid config instead of high CPU loop
by gavinbmoore · 2026-02-01
76.9%
#16685: Fix cli agents/approvals/discord routing edge cases
by craftowen · 2026-02-15
76.5%
#12308: fix(cli): redirect log output to stderr during completion script ge...
by mcaxtr · 2026-02-09
76.4%
#6059: fix #6044
by vivganes · 2026-02-01
75.3%
#17764: fix: CLI update module loading - syntax fixes (v2)
by Limitless2023 · 2026-02-16
75.1%
#6689: Fix: auto-remove idle session lanes to prevent unbounded command qu...
by kamb5686 · 2026-02-01
74.7%
#4109: fix(cli): bypass config guard for tui --url
by hopenjin · 2026-01-29
74.5%