Skip to content

Commit 45a46cf

Browse files
committed
fix(protocol): abort active request handlers on connection close
- Track request handlers with AbortControllers in _requestHandlerAbortControllers - Abort all active request handlers in _onclose() to prevent hanging operations - Ensure both outgoing requests (response handlers) and incoming requests (request handlers) are properly terminated Fixes issue where request handlers would continue running after connection closure, causing timeouts and resource leaks.
1 parent 2beb4f6 commit 45a46cf

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/shared/protocol.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,24 @@ export abstract class Protocol<
334334
this._progressHandlers.clear();
335335
this._pendingDebouncedNotifications.clear();
336336
this._transport = undefined;
337+
338+
// Abort all active request handlers
339+
const requestHandlerAbortControllers = this._requestHandlerAbortControllers;
340+
this._requestHandlerAbortControllers = new Map();
341+
337342
this.onclose?.();
338343

339344
const error = new McpError(ErrorCode.ConnectionClosed, "Connection closed");
345+
346+
// Reject all pending response handlers (for outgoing requests)
340347
for (const handler of responseHandlers.values()) {
341348
handler(error);
342349
}
350+
351+
// Abort all active request handlers (for incoming requests being processed)
352+
for (const abortController of requestHandlerAbortControllers.values()) {
353+
abortController.abort(error);
354+
}
343355
}
344356

345357
private _onerror(error: Error): void {

0 commit comments

Comments
 (0)