Skip to content

Commit d685d38

Browse files
authoredFeb 6, 2025··
don't check AbortSignal maxListeners on some node versions (#4045)
1 parent a9176c9 commit d685d38

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed
 

‎lib/web/fetch/request.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
3737

3838
const dependentControllerMap = new WeakMap()
3939

40+
let abortSignalHasEventHandlerLeakWarning
41+
42+
try {
43+
abortSignalHasEventHandlerLeakWarning = getMaxListeners(new AbortController().signal) > 0
44+
} catch {
45+
abortSignalHasEventHandlerLeakWarning = false
46+
}
47+
4048
function buildAbort (acRef) {
4149
return abort
4250

@@ -424,15 +432,10 @@ class Request {
424432
const acRef = new WeakRef(ac)
425433
const abort = buildAbort(acRef)
426434

427-
// Third-party AbortControllers may not work with these.
428-
// See, https://github.com/nodejs/undici/pull/1910#issuecomment-1464495619.
429-
try {
430-
// If the max amount of listeners is equal to the default, increase it
431-
// This is only available in node >= v19.9.0
432-
if (typeof getMaxListeners === 'function' && getMaxListeners(signal) === defaultMaxListeners) {
433-
setMaxListeners(1500, signal)
434-
}
435-
} catch {}
435+
// If the max amount of listeners is equal to the default, increase it
436+
if (abortSignalHasEventHandlerLeakWarning && getMaxListeners(signal) === defaultMaxListeners) {
437+
setMaxListeners(1500, signal)
438+
}
436439

437440
util.addAbortListener(signal, abort)
438441
// The third argument must be a registry key to be unregistered.

‎test/fetch/long-lived-abort-controller.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const http = require('node:http')
44
const { fetch } = require('../../')
5-
const { once } = require('events')
5+
const { once, setMaxListeners } = require('node:events')
66
const { test } = require('node:test')
77
const { closeServerAsPromise } = require('../utils/node-http')
88
const { strictEqual } = require('node:assert')
@@ -30,6 +30,7 @@ test('long-lived-abort-controller', { skip: true }, async (t) => {
3030
})
3131

3232
const controller = new AbortController()
33+
setMaxListeners(1500, controller.signal)
3334

3435
// The maxListener is set to 1500 in request.js.
3536
// we set it to 2000 to make sure that we are not leaking event listeners.

0 commit comments

Comments
 (0)
Please sign in to comment.