Clear the CLI startup timeout when we are told to stop#1046
Clear the CLI startup timeout when we are told to stop#1046igfoo wants to merge 1 commit intogithub:mainfrom
Conversation
Otherwise we won't terminate until the 10 seconds are up.
There was a problem hiding this comment.
Pull request overview
This PR addresses a Node.js SDK shutdown delay by tracking the “CLI startup timeout” timer and clearing it when the client is stopped, so short-lived scripts don’t remain alive waiting for the 10s startup timeout to elapse.
Changes:
- Track the CLI startup timeout handle on
CopilotClient(cliStartTimeout). - Clear the pending startup timeout during
stop()andforceStop(). - Store the timeout handle when starting the CLI server.
Show a summary per file
| File | Description |
|---|---|
| nodejs/src/client.ts | Track and clear the CLI startup timeout so stop/forceStop can exit promptly instead of being held by a pending 10s timer. |
Copilot's findings
Comments suppressed due to low confidence (2)
nodejs/src/client.ts:1538
cliStartTimeoutis set here but never cleared when the CLI successfully starts or when startup fails (e.g.,error/exithandlers). Ifstart()throws and the caller does not invokestop(), this pending timeout can keep the Node.js event loop alive for up to 10 seconds. Consider clearing+nullingthis.cliStartTimeouton all completion paths (resolve/reject), and also clearing any existing timeout before assigning a new one to avoid orphaned timers on concurrentstart()calls.
// Timeout after 10 seconds
this.cliStartTimeout = setTimeout(() => {
if (!resolved) {
resolved = true;
reject(new Error("Timeout waiting for CLI server to start"));
nodejs/src/client.ts:624
- The
clearTimeout+ nulling logic is duplicated in bothstop()andforceStop(). To reduce the chance of the two paths diverging over time, consider extracting this into a small private helper (e.g.,clearCliStartTimeout()) and calling it from both places.
if (this.cliStartTimeout) {
clearTimeout(this.cliStartTimeout);
this.cliStartTimeout = null;
}
- Files reviewed: 1/1 changed files
- Comments generated: 1
| if (this.cliStartTimeout) { | ||
| clearTimeout(this.cliStartTimeout); | ||
| this.cliStartTimeout = null; | ||
| } |
There was a problem hiding this comment.
There’s now important cleanup logic for cliStartTimeout, but there are currently no unit tests covering stop()/forceStop() behavior (this file’s tests mainly use forceStop() as cleanup). Adding a small unit test with fake timers to assert that stop() (and forceStop()) clears a pending startup timeout would help prevent regressions where short-lived scripts hang for ~10s again.
This issue also appears in the following locations of the same file:
- line 621
- line 1534
With
index.tsasrunning
npx tsx index.tstakes more than 10 seconds. This is problematic for scripts that run quick queries, or even decide to run no queries after starting a client.This is due to the 10 second timeout that is used to declare the CLI server failed to start.
With this PR, the above takes less than a second.
I can't run the
npm testtests locally on main; I get as far asand then it seemingly makes no further progress. But I get the same on my branch, and I assume that CI has run them.