Skip to content

Commit 1a750b9

Browse files
committed
fix(server): prevent duplicate and accidental keepalive timer clears
Adds defensive _clearKeepAliveTimer() before setInterval in handleGetRequest() to prevent duplicate timers when the GET stream is reinitialized. Moves _clearKeepAliveTimer() inside the if(stream) guard in closeStandaloneSSEStream() so it only clears when the standalone stream is being torn down, preventing accidental clearing of a replay stream's timer. Closes #1726
1 parent 9cfa4f4 commit 1a750b9

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

packages/server/src/server/streamableHttp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
491491
// Start keepalive timer to send periodic SSE comments that prevent
492492
// reverse proxies from closing the connection due to idle timeouts
493493
if (this._keepAliveInterval !== undefined) {
494+
this._clearKeepAliveTimer();
494495
this._keepAliveTimer = setInterval(() => {
495496
try {
496497
streamController!.enqueue(encoder.encode(': keepalive\n\n'));
@@ -982,9 +983,9 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
982983
* Use this to implement polling behavior for server-initiated notifications.
983984
*/
984985
closeStandaloneSSEStream(): void {
985-
this._clearKeepAliveTimer();
986986
const stream = this._streamMapping.get(this._standaloneSseStreamId);
987987
if (stream) {
988+
this._clearKeepAliveTimer();
988989
stream.cleanup();
989990
}
990991
}

0 commit comments

Comments
 (0)