Skip to content

Conversation

@mcollina
Copy link
Member

Fixes issue where SSE and other long-lived connections could leak when using fetch with redirect: 'error' option. The spec optimization of reusing fetchParams when window is 'no-window' and redirect is 'error' was causing connection lifecycle issues.

In Node.js, request.window is always set to 'no-window', so this optimization was being applied in all cases where redirect: 'error' was used. This caused problems with connection cleanup, particularly for EventSource and SSE use cases where the connection needs to be properly aborted.

The fix disables this optimization and always clones the request and copies fetchParams to ensure proper connection lifecycle management.

🤖 Generated with Claude Code

Fixes: #4627

Fixes issue where SSE and other long-lived connections could leak when
using fetch with redirect: 'error' option. The spec optimization of
reusing fetchParams when window is 'no-window' and redirect is 'error'
was causing connection lifecycle issues.

In Node.js, request.window is always set to 'no-window', so this
optimization was being applied in all cases where redirect: 'error'
was used. This caused problems with connection cleanup, particularly
for EventSource and SSE use cases where the connection needs to be
properly aborted.

The fix disables this optimization and always clones the request and
copies fetchParams to ensure proper connection lifecycle management.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

Fixes: #4627
Signed-off-by: Matteo Collina <[email protected]>
@mcollina mcollina requested a review from KhafraDev October 18, 2025 16:09
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.92%. Comparing base (9adb5e6) to head (1cd5112).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4629      +/-   ##
==========================================
- Coverage   92.94%   92.92%   -0.02%     
==========================================
  Files         106      106              
  Lines       33092    33094       +2     
==========================================
- Hits        30756    30754       -2     
- Misses       2336     2340       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it highly unlikely that this is an issue in undici. The eventsource package on npm creates its own signal and doesn't seem to work with a custom one (when providing your own fetch function), hence it's never aborted. I'd need a smaller repro to be certain. Also, if it is a spec issue, we should upstream a fix, or at the very least a note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSE connection may leak when fetch init.redirect = 'error'

4 participants