Skip to content

Commit

Permalink
don't check AbortSignal maxListeners on some node versions
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Feb 3, 2025
1 parent a9176c9 commit fe1b6d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
21 changes: 12 additions & 9 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {

const dependentControllerMap = new WeakMap()

let abortSignalHasEventHandlerLeakWarning

try {
abortSignalHasEventHandlerLeakWarning = getMaxListeners(new AbortController().signal) > 0
} catch {
abortSignalHasEventHandlerLeakWarning = false
}

function buildAbort (acRef) {
return abort

Expand Down Expand Up @@ -424,15 +432,10 @@ class Request {
const acRef = new WeakRef(ac)
const abort = buildAbort(acRef)

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

util.addAbortListener(signal, abort)
// The third argument must be a registry key to be unregistered.
Expand Down
3 changes: 2 additions & 1 deletion test/fetch/long-lived-abort-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const http = require('node:http')
const { fetch } = require('../../')
const { once } = require('events')
const { once, setMaxListeners } = require('node:events')
const { test } = require('node:test')
const { closeServerAsPromise } = require('../utils/node-http')
const { strictEqual } = require('node:assert')
Expand Down Expand Up @@ -30,6 +30,7 @@ test('long-lived-abort-controller', { skip: true }, async (t) => {
})

const controller = new AbortController()
setMaxListeners(1500, controller.signal)

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

0 comments on commit fe1b6d1

Please sign in to comment.