Skip to content

Commit

Permalink
fix(fetch): raise AbortSignal max event listeners (nodejs#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent 052e6c5 commit 1a63bf2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 })
}
Expand Down
28 changes: 28 additions & 0 deletions test/fetch/issue-node-46525.js
Original file line number Diff line number Diff line change
@@ -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 })
}
})

0 comments on commit 1a63bf2

Please sign in to comment.