File tree 2 files changed +38
-6
lines changed
2 files changed +38
-6
lines changed Original file line number Diff line number Diff line change @@ -81,12 +81,6 @@ class Fetch extends EE {
81
81
this . connection = null
82
82
this . dump = false
83
83
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 )
90
84
}
91
85
92
86
terminate ( reason ) {
@@ -1967,6 +1961,7 @@ async function httpNetworkFetch (
1967
1961
// 19. Run these steps in parallel:
1968
1962
1969
1963
// 1. Run these steps, but abort when fetchParams is canceled:
1964
+ fetchParams . controller . onAborted = onAborted
1970
1965
fetchParams . controller . on ( 'terminated' , onAborted )
1971
1966
fetchParams . controller . resume = async ( ) => {
1972
1967
// 1. While true
@@ -2235,6 +2230,10 @@ async function httpNetworkFetch (
2235
2230
fetchParams . controller . off ( 'terminated' , this . abort )
2236
2231
}
2237
2232
2233
+ if ( fetchParams . controller . onAborted ) {
2234
+ fetchParams . controller . off ( 'terminated' , fetchParams . controller . onAborted )
2235
+ }
2236
+
2238
2237
fetchParams . controller . ended = true
2239
2238
2240
2239
this . body . push ( null )
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments