Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions packages/server/src/server/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
private _enableDnsRebindingProtection: boolean;
private _retryInterval?: number;
private _supportedProtocolVersions: string[];
private _isClosing = false;

sessionId?: string;
onclose?: () => void;
Expand Down Expand Up @@ -887,15 +888,28 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
}

async close(): Promise<void> {
// Close all SSE connections
for (const { cleanup } of this._streamMapping.values()) {
cleanup();
}
this._streamMapping.clear();
if (this._isClosing) return;
this._isClosing = true;

try {
// Snapshot and clear mapping before cleanup to prevent
// re-entrant deletes from cancel callbacks (fixes #1699)
const entries = [...this._streamMapping.values()];
this._streamMapping.clear();

// Clear any pending responses
this._requestResponseMap.clear();
this.onclose?.();
for (const { cleanup } of entries) {
try {
cleanup();
} catch {
// Suppress errors from already-closed controllers
}
}

// Clear any pending responses
this._requestResponseMap.clear();
} finally {
this.onclose?.();
}
}

/**
Expand Down
Loading