Skip to content

Commit ccbc773

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 992a5b1 commit ccbc773

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
@@ -490,6 +490,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
490490
// Start keepalive timer to send periodic SSE comments that prevent
491491
// reverse proxies from closing the connection due to idle timeouts
492492
if (this._keepAliveInterval !== undefined) {
493+
this._clearKeepAliveTimer();
493494
this._keepAliveTimer = setInterval(() => {
494495
try {
495496
streamController!.enqueue(encoder.encode(': keepalive\n\n'));
@@ -977,9 +978,9 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
977978
* Use this to implement polling behavior for server-initiated notifications.
978979
*/
979980
closeStandaloneSSEStream(): void {
980-
this._clearKeepAliveTimer();
981981
const stream = this._streamMapping.get(this._standaloneSseStreamId);
982982
if (stream) {
983+
this._clearKeepAliveTimer();
983984
stream.cleanup();
984985
}
985986
}

0 commit comments

Comments
 (0)