Skip to content

Commit 3274c97

Browse files
authored
remove all fetchParam event handlers (#2823)
1 parent 2a17219 commit 3274c97

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

lib/web/fetch/index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ class Fetch extends EE {
8181
this.connection = null
8282
this.dump = false
8383
this.state = 'ongoing'
84-
// 2 terminated listeners get added per request,
85-
// but only 1 gets removed. If there are 20 redirects,
86-
// 21 listeners will be added.
87-
// See https://github.com/nodejs/undici/issues/1711
88-
// TODO (fix): Find and fix root cause for leaked listener.
89-
this.setMaxListeners(21)
9084
}
9185

9286
terminate (reason) {
@@ -1967,6 +1961,7 @@ async function httpNetworkFetch (
19671961
// 19. Run these steps in parallel:
19681962

19691963
// 1. Run these steps, but abort when fetchParams is canceled:
1964+
fetchParams.controller.onAborted = onAborted
19701965
fetchParams.controller.on('terminated', onAborted)
19711966
fetchParams.controller.resume = async () => {
19721967
// 1. While true
@@ -2235,6 +2230,10 @@ async function httpNetworkFetch (
22352230
fetchParams.controller.off('terminated', this.abort)
22362231
}
22372232

2233+
if (fetchParams.controller.onAborted) {
2234+
fetchParams.controller.off('terminated', fetchParams.controller.onAborted)
2235+
}
2236+
22382237
fetchParams.controller.ended = true
22392238

22402239
this.body.push(null)

test/fetch/issue-1711.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const assert = require('node:assert')
4+
const { once } = require('node:events')
5+
const { createServer } = require('node:http')
6+
const { test } = require('node:test')
7+
const { fetch } = require('../..')
8+
9+
test('Redirecting a bunch does not cause a MaxListenersExceededWarning', async (t) => {
10+
let redirects = 0
11+
12+
const server = createServer((req, res) => {
13+
if (redirects === 15) {
14+
res.end('Okay goodbye')
15+
return
16+
}
17+
18+
res.writeHead(302, {
19+
Location: `/${redirects++}`
20+
})
21+
res.end()
22+
}).listen(0)
23+
24+
t.after(server.close.bind(server))
25+
await once(server, 'listening')
26+
27+
process.emitWarning = assert.bind(null, false)
28+
29+
const url = `http://localhost:${server.address().port}`
30+
const response = await fetch(url, { redirect: 'follow' })
31+
32+
assert.deepStrictEqual(response.url, `${url}/${redirects - 1}`)
33+
})

0 commit comments

Comments
 (0)