Repository navigation
SSE connection may leak when fetch init.redirect = 'error' #4627
Description
Activity
- added a commit that references this issue
on Oct 18, 2025 Can you please create a reproduction without using other packages other than undici? Have you tried using undici own EventSource?
I edit the source code and add code: console.log('requestFinalizer execute'), so the reason may be that the abortController ac was garbage collected too early.
I suspect it's because 'eventsource' package doesn't hold a reference to the fetch request.
Can you please create a reproduction without using other packages other than undici? Have you tried using undici own EventSource?
import { fetch } from 'undici' import { randomUUID } from 'crypto'; setInterval(async () => { const ac = new AbortController() const response = await fetch('http://127.0.0.1:3000/sse', { signal: ac.signal, redirect: 'error' }); response._id = randomUUID(); setTimeout(() => { ac.abort(); response._isAborted = true; }, 5000); try { const reader = response.body.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; if (response._isAborted) { const text = decoder.decode(value); console.log(`fetch:${response._id} aborted, still receive data: ${text}`); } } } catch (err) { if (err.name === 'AbortError' || err.type === 'aborted') { return } console.log(err); } }, 1000);Using only fetch also reproduced the bug:

Tests show that the cause is that requestObject in fetch function is garbage collected too early and code like this will not reproduce the bug.

I am still confused whyhttpFetchParams = { ...fetchParams }prevents requestObject from being garbage collected? Could you help explain? Thank you.- added a commit that references this issue
on Jan 20, 2026 - added a commit that references this issue
on Oct 3, 2026
Bug Description
Fetch abort may not take effect when fetch init.redirect = 'error' and this causes SSE connection leak
Reproducible By
SSE Server:
test
Expected Behavior
SSE connection should be closed properly after fetch abort
Logs & Screenshots
I edit the source code and add code: console.log('requestFinalizer execute'), so the reason may be that the abortController ac was garbage collected too early.

why use a copy or clone when redirect is not error?

Environment
Node v20.19.2
undici 7.16.0
Additional context