From 7615308a92d3c8c90081fb99c55ab8bd59212396 Mon Sep 17 00:00:00 2001 From: Khafra <42794878+KhafraDev@users.noreply.github.com> Date: Mon, 6 Feb 2023 13:01:20 -0500 Subject: [PATCH] fix(fetch): raise AbortSignal max event listeners --- lib/fetch/request.js | 6 ++++++ test/fetch/issue-node-46525.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/fetch/issue-node-46525.js diff --git a/lib/fetch/request.js b/lib/fetch/request.js index 7264d152f87..eca7b060e0e 100644 --- a/lib/fetch/request.js +++ b/lib/fetch/request.js @@ -28,6 +28,7 @@ const { getGlobalOrigin } = require('./global') const { URLSerializer } = require('./dataURL') const { kHeadersList } = require('../core/symbols') const assert = require('assert') +const { setMaxListeners, getEventListeners, defaultMaxListeners } = require('events') let TransformStream = globalThis.TransformStream @@ -352,6 +353,11 @@ class Request { const abort = function () { acRef.deref()?.abort(this.reason) } + + if (getEventListeners(signal, 'abort').length >= defaultMaxListeners) { + setMaxListeners(100, signal) + } + signal.addEventListener('abort', abort, { once: true }) requestFinalizer.register(this, { signal, abort }) } diff --git a/test/fetch/issue-node-46525.js b/test/fetch/issue-node-46525.js new file mode 100644 index 00000000000..6fd9810c52b --- /dev/null +++ b/test/fetch/issue-node-46525.js @@ -0,0 +1,28 @@ +'use strict' + +const { once } = require('events') +const { createServer } = require('http') +const { test } = require('tap') +const { fetch } = require('../..') + +// https://github.com/nodejs/node/issues/46525 +test('No warning when reusing AbortController', async (t) => { + function onWarning (error) { + t.error(error, 'Got warning') + } + + const server = createServer((req, res) => res.end()).listen(0) + + await once(server, 'listening') + + process.on('warning', onWarning) + t.teardown(() => { + process.off('warning', onWarning) + return server.close() + }) + + const controller = new AbortController() + for (let i = 0; i < 15; i++) { + await fetch(`http://localhost:${server.address().port}`, { signal: controller.signal }) + } +})