Fix timeout and listener leaks in readResponseById#8
Fix timeout and listener leaks in readResponseById#8frouaix merged 2 commits intoissue-1-e2e-testingfrom
readResponseById#8Conversation
…eout, unref timer Co-authored-by: frouaix <876178+frouaix@users.noreply.github.com>
readResponseById
There was a problem hiding this comment.
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.
| @@ -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(); | |||
There was a problem hiding this comment.
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:
- The timer handle captured and cleared on success
- The handler removed on timeout
- The timer unref'd to prevent blocking event loop exit
Consider applying the same fix pattern to those functions as well.
readResponseByIdnever cleared its timeout on success and never removed thedatahandler on timeout, causing listener accumulation and keeping the test process alive up totimeoutMsper call.Changes
setTimeouthandle and callclearTimeout(timer)in the success path before resolvingproc.stdout!.off("data", handler)inside the timeout callback before rejectingtimer.unref()so a pending timeout doesn't prevent the event loop from exiting naturally✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.