-
-
Notifications
You must be signed in to change notification settings - Fork 696
fix: prevent AbortController GC when redirect is 'error' #4750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mcollina
wants to merge
1
commit into
main
Choose a base branch
from
fix/issue-4627-abort-redirect-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| 'use strict' | ||
|
|
||
| // Regression test for https://github.com/nodejs/undici/issues/4627 | ||
| // Fetch abort may not take effect when fetch init.redirect = 'error' | ||
| // causing SSE connection leak | ||
|
|
||
| const { test } = require('node:test') | ||
| const { fetch } = require('../..') | ||
| const { createServer } = require('node:http') | ||
| const { once } = require('node:events') | ||
| const { closeServerAsPromise } = require('../utils/node-http') | ||
|
|
||
| // This test requires --expose-gc flag | ||
| const hasGC = typeof global.gc === 'function' | ||
|
|
||
| test('abort should work with redirect: error', { skip: !hasGC, timeout: 3000 }, async (t) => { | ||
| let connectionClosed = false | ||
| let messagesReceivedAfterAbort = 0 | ||
|
|
||
| const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { | ||
| res.writeHead(200, { | ||
| 'Content-Type': 'text/event-stream', | ||
| 'Cache-Control': 'no-cache', | ||
| Connection: 'keep-alive' | ||
| }) | ||
|
|
||
| // Send data every 20ms for faster test | ||
| const interval = setInterval(() => { | ||
| res.write(`data: ${Date.now()}\n\n`) | ||
| }, 20) | ||
|
|
||
| res.on('close', () => { | ||
| connectionClosed = true | ||
| clearInterval(interval) | ||
| }) | ||
| }) | ||
|
|
||
| t.after(closeServerAsPromise(server)) | ||
| await once(server.listen(0), 'listening') | ||
| const port = server.address().port | ||
|
|
||
| const ac = new AbortController() | ||
|
|
||
| const response = await fetch(`http://localhost:${port}/sse`, { | ||
| signal: ac.signal, | ||
| redirect: 'error' | ||
| }) | ||
|
|
||
| let aborted = false | ||
|
|
||
| // Start reading the stream in background | ||
| const readPromise = (async () => { | ||
| try { | ||
| const reader = response.body.getReader() | ||
| while (true) { | ||
| const { done } = await reader.read() | ||
| if (done) break | ||
|
|
||
| if (aborted) { | ||
| messagesReceivedAfterAbort++ | ||
| if (messagesReceivedAfterAbort >= 3) { | ||
| // Bug confirmed - received multiple messages after abort | ||
| reader.cancel() | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } catch (err) { | ||
| // AbortError is expected | ||
| if (err.name !== 'AbortError' && err.message !== 'aborted' && !err.message?.includes('cancel')) { | ||
| throw err | ||
| } | ||
| } | ||
| })() | ||
|
|
||
| // Wait for some data to be received | ||
| await new Promise(resolve => setTimeout(resolve, 100)) | ||
|
|
||
| // Trigger GC to potentially collect the AbortController | ||
| global.gc() | ||
|
|
||
| // Abort the request | ||
| aborted = true | ||
| ac.abort() | ||
|
|
||
| // Wait for the read to complete or timeout | ||
| const timeout = new Promise((_resolve, reject) => | ||
| setTimeout(() => reject(new Error('Read did not complete in time')), 1000) | ||
| ) | ||
|
|
||
| try { | ||
| await Promise.race([readPromise, timeout]) | ||
| } catch (e) { | ||
| // If timed out, that's also a bug indication | ||
| if (e.message === 'Read did not complete in time') { | ||
| messagesReceivedAfterAbort = 999 // Force failure | ||
| } else { | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| t.assert.strictEqual(messagesReceivedAfterAbort, 0, 'No data should be received after abort') | ||
|
|
||
| // Give some time for the connection to close | ||
| await new Promise(resolve => setTimeout(resolve, 100)) | ||
|
|
||
| t.assert.ok(connectionClosed, 'Server connection should be closed after abort') | ||
| }) | ||
|
|
||
| test('abort should work without redirect: error (control test)', { skip: !hasGC, timeout: 3000 }, async (t) => { | ||
| let connectionClosed = false | ||
| let messagesReceivedAfterAbort = 0 | ||
|
|
||
| const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { | ||
| res.writeHead(200, { | ||
| 'Content-Type': 'text/event-stream', | ||
| 'Cache-Control': 'no-cache', | ||
| Connection: 'keep-alive' | ||
| }) | ||
|
|
||
| // Send data every 20ms | ||
| const interval = setInterval(() => { | ||
| res.write(`data: ${Date.now()}\n\n`) | ||
| }, 20) | ||
|
|
||
| res.on('close', () => { | ||
| connectionClosed = true | ||
| clearInterval(interval) | ||
| }) | ||
| }) | ||
|
|
||
| t.after(closeServerAsPromise(server)) | ||
| await once(server.listen(0), 'listening') | ||
| const port = server.address().port | ||
|
|
||
| const ac = new AbortController() | ||
|
|
||
| // Without redirect: 'error' - this should work correctly | ||
| const response = await fetch(`http://localhost:${port}/sse`, { | ||
| signal: ac.signal | ||
| }) | ||
|
|
||
| let aborted = false | ||
|
|
||
| // Start reading the stream in background | ||
| const readPromise = (async () => { | ||
| try { | ||
| const reader = response.body.getReader() | ||
| while (true) { | ||
| const { done } = await reader.read() | ||
| if (done) break | ||
|
|
||
| if (aborted) { | ||
| messagesReceivedAfterAbort++ | ||
| if (messagesReceivedAfterAbort >= 3) { | ||
| reader.cancel() | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } catch (err) { | ||
| // AbortError is expected | ||
| if (err.name !== 'AbortError' && err.message !== 'aborted' && !err.message?.includes('cancel')) { | ||
| throw err | ||
| } | ||
| } | ||
| })() | ||
|
|
||
| // Wait for some data to be received | ||
| await new Promise(resolve => setTimeout(resolve, 100)) | ||
|
|
||
| // Trigger GC | ||
| global.gc() | ||
|
|
||
| // Abort the request | ||
| aborted = true | ||
| ac.abort() | ||
|
|
||
| // Wait for the read to complete | ||
| await readPromise | ||
|
|
||
| // Give some time for the connection to close | ||
| await new Promise(resolve => setTimeout(resolve, 100)) | ||
|
|
||
| t.assert.strictEqual(messagesReceivedAfterAbort, 0, 'No data should be received after abort') | ||
| t.assert.ok(connectionClosed, 'Server connection should be closed after abort') | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this line as it is just a "diary entry". The corresponding test is having the link and is totally fine.