Skip to content

Commit 9205f5e

Browse files
committed
refactor: simplify close() guard to match stdio.ts pattern
Use a _closed boolean flag instead of _closing, remove the snapshot and try/catch per review feedback. Matches the existing pattern in server/stdio.ts. Both tests still pass with this simpler approach.
1 parent 91a101a commit 9205f5e

1 file changed

Lines changed: 9 additions & 21 deletions

File tree

packages/server/src/server/streamableHttp.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -899,34 +899,22 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
899899
return undefined;
900900
}
901901

902-
private _closing = false;
902+
private _closed = false;
903903

904904
async close(): Promise<void> {
905-
// Guard against re-entrant calls. When onclose() triggers the
906-
// Protocol layer to call close() again, this prevents infinite
907-
// recursion that causes a stack overflow with many transports.
908-
if (this._closing) {
905+
if (this._closed) {
909906
return;
910907
}
911-
this._closing = true;
908+
this._closed = true;
912909

913-
// Snapshot and clear before iterating to avoid issues with
914-
// cleanup callbacks that modify the map during iteration.
915-
const streams = [...this._streamMapping.values()];
916-
this._streamMapping.clear();
917-
this._requestResponseMap.clear();
918-
919-
// Close all SSE connections with error isolation so one
920-
// failing cleanup doesn't prevent others from running.
921-
for (const { cleanup } of streams) {
922-
try {
923-
cleanup();
924-
} catch {
925-
// Individual stream cleanup failures should not
926-
// prevent other streams from being cleaned up.
927-
}
910+
// Close all SSE connections
911+
for (const { cleanup } of this._streamMapping.values()) {
912+
cleanup();
928913
}
914+
this._streamMapping.clear();
929915

916+
// Clear any pending responses
917+
this._requestResponseMap.clear();
930918
this.onclose?.();
931919
}
932920

0 commit comments

Comments
 (0)