Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/clamp-retry-interval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eventsource": patch
---

Clamp the server-sent `retry` value to the maximum timer delay, so a very large value no longer causes a reconnect attempt every millisecond
3 changes: 2 additions & 1 deletion src/EventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,8 @@ class EventSourceImpl extends EventTarget implements EventSource {
* @internal
*/
#onRetryChange = (value: number) => {
this.#reconnectInterval = value
// Timers treat delays above 2^31-1 ms as 1ms, so clamp to avoid a tight reconnect loop
this.#reconnectInterval = Math.min(value, 2147483647)
}

/**
Expand Down
35 changes: 35 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,41 @@ test('[NON-SPEC] custom `maxBufferSize` fails connection on parser buffer overfl
await deferClose(es)
})

test('[NON-SPEC] clamps an oversized `retry` value so the reconnection delay does not overflow', async () => {
const url = `${serverUrl}/`
let fetchCount = 0

const retryFetch: FetchLike = async () => {
fetchCount++
return {
body: new ReadableStream<Uint8Array>({
start(controller) {
const encoder = new TextEncoder()
// Larger than the 2^31-1 ms maximum that timers accept, which would otherwise be
// treated as a 1ms delay and cause a tight reconnect loop
controller.enqueue(encoder.encode('retry: 2147483648\n'))
controller.enqueue(encoder.encode('data: hello\n\n'))
controller.close()
},
}),
redirected: false,
status: 200,
headers: new Headers({'content-type': 'text/event-stream'}),
url,
}
}

const onError = getCallCounter<ErrorEvent>({name: 'onError'})
const es = new OurEventSource(url, {fetch: retryFetch})

es.addEventListener('error', onError.listener)
await onError.waitForCallCount(1)

await new Promise((resolve) => setTimeout(resolve, 250))
expect(fetchCount, 'reconnect attempts').toBe(1)
await deferClose(es)
})

test('[NON-SPEC] unrefs the reconnection timer where supported, so it does not keep the event loop alive', async () => {
// Browsers return numeric timer handles, so there is nothing to unref there.
const probe = setTimeout(() => {}, 0)
Expand Down