Skip to content

Fix timeout and listener leaks in readResponseById#8

Merged
frouaix merged 2 commits intoissue-1-e2e-testingfrom
copilot/sub-pr-7
Feb 22, 2026
Merged

Fix timeout and listener leaks in readResponseById#8
frouaix merged 2 commits intoissue-1-e2e-testingfrom
copilot/sub-pr-7

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 22, 2026

readResponseById never cleared its timeout on success and never removed the data handler on timeout, causing listener accumulation and keeping the test process alive up to timeoutMs per call.

Changes

  • Store and clear the timer — capture the setTimeout handle and call clearTimeout(timer) in the success path before resolving
  • Detach handler on timeout — call proc.stdout!.off("data", handler) inside the timeout callback before rejecting
  • Unref the timer — call timer.unref() so a pending timeout doesn't prevent the event loop from exiting naturally
proc.stdout!.on("data", handler);
const timer = setTimeout(() => {
  proc.stdout!.off("data", handler);          // detach on timeout
  reject(new Error(`Timeout waiting for response id=${id}`));
}, timeoutMs);
timer.unref();                                // don't block event loop exit

// in handler, on match:
proc.stdout!.off("data", handler);
clearTimeout(timer);                          // cancel timer on success
resolve(msg);

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…eout, unref timer

Co-authored-by: frouaix <876178+frouaix@users.noreply.github.com>
Copilot AI changed the title [WIP] Update E2E tests for feedback resolution Fix timeout and listener leaks in readResponseById Feb 22, 2026
Copilot AI requested a review from frouaix February 22, 2026 01:43
@frouaix frouaix marked this pull request as ready for review February 22, 2026 01:47
Copilot AI review requested due to automatic review settings February 22, 2026 01:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes resource leaks in the readResponseById test helper function that were causing listener accumulation and preventing the test process from exiting naturally. The function was failing to clear its timeout on success and was not removing event handlers on timeout.

Changes:

  • Added proper timeout cleanup on success path by capturing and clearing the timer handle
  • Added event handler removal in the timeout callback to prevent listener leaks
  • Added timer.unref() to prevent pending timeouts from blocking event loop exit

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 46 to +60
@@ -52,7 +53,11 @@ function readResponseById(proc: ChildProcess, id: number, timeoutMs = 15000): Pr
}
};
proc.stdout!.on("data", handler);
setTimeout(() => reject(new Error(`Timeout waiting for response id=${id}`)), timeoutMs);
const timer = setTimeout(() => {
proc.stdout!.off("data", handler);
reject(new Error(`Timeout waiting for response id=${id}`));
}, timeoutMs);
timer.unref();
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same timeout and listener leak issues fixed here also exist in packages/mcp-server/test/integration/server.test.ts. That file has two similar functions (readResponse on line 14 and readResponseById on line 38) that also need:

  1. The timer handle captured and cleared on success
  2. The handler removed on timeout
  3. The timer unref'd to prevent blocking event loop exit

Consider applying the same fix pattern to those functions as well.

Copilot uses AI. Check for mistakes.
@frouaix frouaix merged commit d1ecc40 into issue-1-e2e-testing Feb 22, 2026
4 checks passed
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.

3 participants