diff --git a/nodejs/src/session.ts b/nodejs/src/session.ts index ca9789c..bc5080f 100644 --- a/nodejs/src/session.ts +++ b/nodejs/src/session.ts @@ -145,11 +145,12 @@ export class CopilotSession { } }); + let timeoutId: NodeJS.Timeout | undefined; try { await this.send(options); const timeoutPromise = new Promise((_, reject) => { - setTimeout( + timeoutId = setTimeout( () => reject( new Error( @@ -159,8 +160,13 @@ export class CopilotSession { effectiveTimeout ); }); - await Promise.race([idlePromise, timeoutPromise]); + try { + await Promise.race([idlePromise, timeoutPromise]); + } finally { + // Clear timeout immediately after race completes to prevent memory leaks + if (timeoutId) clearTimeout(timeoutId); + } return lastAssistantMessage; } finally { unsubscribe();