Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

investigate warning #3831

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const { getMaxListeners, setMaxListeners, defaultMaxListeners } = require('node:

const kAbortController = Symbol('abortController')

// This is only available in node >= v19.9.0
const hasGetMaxListeners = typeof getMaxListeners === 'function'

const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
signal.removeEventListener('abort', abort)
})
Expand Down Expand Up @@ -424,15 +427,16 @@ 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 (hasGetMaxListeners) {
// 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
if (getMaxListeners(signal) === defaultMaxListeners) {
setMaxListeners(1500, signal)
}
} catch {}
}

util.addAbortListener(signal, abort)
// The third argument must be a registry key to be unregistered.
Expand Down
57 changes: 38 additions & 19 deletions test/fetch/long-lived-abort-controller.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
'use strict'

const http = require('node:http')
const { fetch } = require('../../')
const { once } = require('events')
const { fork } = require('node:child_process')
const { resolve: pathResolve } = require('node:path')
const { test } = require('node:test')
const { closeServerAsPromise } = require('../utils/node-http')
const { strictEqual } = require('node:assert')
const { fetch } = require('../../')
const { strictEqual, fail } = require('node:assert')

// const isNode18 = process.version.startsWith('v18')
const isNode18 = process.version.startsWith('v18')

test('long-lived-abort-controller', { skip: true }, async (t) => {
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.write('Hello World!')
res.end()
}).listen(0)
test('long-lived-abort-controller', { skip: isNode18 }, async (t) => {
// Spawn a server in a new process to avoid effects from the blocking event loop
const {
serverProcess,
address
} = await new Promise((resolve, reject) => {
const childProcess = fork(
pathResolve(__dirname, '../utils/hello-world-server.js'),
[],
{ windowsHide: true }
)

await once(server, 'listening')
childProcess.on('message', (address) => {
resolve({
serverProcess: childProcess,
address
})
})
childProcess.on('error', err => {
reject(err)
})
})

t.after(closeServerAsPromise(server))
t.after(() => {
serverProcess.kill('SIGKILL')
})

let warningEmitted = false
function onWarning () {
warningEmitted = true
let emittedWarning = null
function onWarning (value) {
emittedWarning = value
}
process.on('warning', onWarning)
t.after(() => {
Expand All @@ -36,13 +51,17 @@
// Unfortunately we are relying on GC and implementation details here.
for (let i = 0; i < 2000; i++) {
// make request
const res = await fetch(`http://localhost:${server.address().port}`, {
const res = await fetch(address, {

Check failure on line 54 in test/fetch/long-lived-abort-controller.js

View workflow job for this annotation

GitHub Actions / test (23, ubuntu-latest) / Test with Node.js 23 on ubuntu-latest

long-lived-abort-controller

[Error [ERR_TEST_FAILURE]: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: MaxListenersExceededWarning: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit at [kNewListener] (node:internal/event_target:562:17) at [kNewListener] (node:internal/abort_controller:285:24) at AbortSignal.addEventListener (node:internal/event_target:686:23) at Object.addAbortListener (/home/runner/work/undici/undici/lib/core/util.js:643:12) at Request (/home/runner/work/undici/undici/lib/web/fetch/request.js:441:14) at fetch (/home/runner/work/undici/undici/lib/web/fetch/index.js:139:21) at fetch (/home/runner/work/undici/undici/index.js:113:18) at TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/long-lived-abort-controller.js:54:23) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async Test.run (node:internal/test_runner/test:935:9) { target: {}, type: 'abort', count: 1501 } }

Check failure on line 54 in test/fetch/long-lived-abort-controller.js

View workflow job for this annotation

GitHub Actions / test (22, macos-latest) / Test with Node.js 22 on macos-latest

long-lived-abort-controller

[Error [ERR_TEST_FAILURE]: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: MaxListenersExceededWarning: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit at [kNewListener] (node:internal/event_target:562:17) at [kNewListener] (node:internal/abort_controller:272:24) at AbortSignal.addEventListener (node:internal/event_target:675:23) at Object.addAbortListener (/Users/runner/work/undici/undici/lib/core/util.js:643:12) at new Request (/Users/runner/work/undici/undici/lib/web/fetch/request.js:441:14) at fetch (/Users/runner/work/undici/undici/lib/web/fetch/index.js:139:21) at fetch (/Users/runner/work/undici/undici/index.js:113:18) at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/long-lived-abort-controller.js:54:23) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async Test.run (node:internal/test_runner/test:935:9) { target: {}, type: 'abort', count: 1501 } }

Check failure on line 54 in test/fetch/long-lived-abort-controller.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 22 compiled --without-intl

long-lived-abort-controller

[Error [ERR_TEST_FAILURE]: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: MaxListenersExceededWarning: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit at [kNewListener] (node:internal/event_target:562:17) at [kNewListener] (node:internal/abort_controller:272:24) at AbortSignal.addEventListener (node:internal/event_target:675:23) at Object.addAbortListener (/home/runner/work/undici/undici/lib/core/util.js:643:12) at new Request (/home/runner/work/undici/undici/lib/web/fetch/request.js:441:14) at fetch (/home/runner/work/undici/undici/lib/web/fetch/index.js:139:21) at fetch (/home/runner/work/undici/undici/index.js:113:18) at TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/long-lived-abort-controller.js:54:23) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async Test.run (node:internal/test_runner/test:935:9) { target: {}, type: 'abort', count: 1501 } }

Check failure on line 54 in test/fetch/long-lived-abort-controller.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 23 compiled --without-intl

long-lived-abort-controller

[Error [ERR_TEST_FAILURE]: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: MaxListenersExceededWarning: Possible EventTarget memory leak detected. 1501 abort listeners added to [AbortSignal]. MaxListeners is 1500. Use events.setMaxListeners() to increase limit at [kNewListener] (node:internal/event_target:562:17) at [kNewListener] (node:internal/abort_controller:285:24) at AbortSignal.addEventListener (node:internal/event_target:686:23) at Object.addAbortListener (/home/runner/work/undici/undici/lib/core/util.js:643:12) at Request (/home/runner/work/undici/undici/lib/web/fetch/request.js:441:14) at fetch (/home/runner/work/undici/undici/lib/web/fetch/index.js:139:21) at fetch (/home/runner/work/undici/undici/index.js:113:18) at TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/long-lived-abort-controller.js:54:23) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async Test.run (node:internal/test_runner/test:935:9) { target: {}, type: 'abort', count: 1501 } }
signal: controller.signal
})

// drain body
await res.text()
}

strictEqual(warningEmitted, false)
if (emittedWarning !== null) {
fail(emittedWarning)
} else {
strictEqual(emittedWarning, null)
}
})
4 changes: 2 additions & 2 deletions test/issue-3410.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('https://github.com/nodejs/undici/issues/3410', () => {
address
} = await new Promise((resolve, reject) => {
const childProcess = fork(
pathResolve(__dirname, './utils/hello-world-server.js'),
pathResolve(__dirname, './utils/hello-world-server-delayed.js'),
[],
{ windowsHide: true }
)
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('https://github.com/nodejs/undici/issues/3410', () => {
address
} = await new Promise((resolve, reject) => {
const childProcess = fork(
pathResolve(__dirname, './utils/hello-world-server.js'),
pathResolve(__dirname, './utils/hello-world-server-delayed.js'),
[],
{ windowsHide: true }
)
Expand Down
30 changes: 30 additions & 0 deletions test/utils/hello-world-server-delayed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { createServer } = require('node:http')
const hostname = '127.0.0.1'

const server = createServer(async (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')

await sendInDelayedChunks(res, 'Hello World', 125)
res.end()
})

async function sendInDelayedChunks (res, payload, delay) {
const chunks = payload.split('')

for (const chunk of chunks) {
await new Promise(resolve => setTimeout(resolve, delay))

res.write(chunk)
}
}

server.listen(0, hostname, () => {
if (process.send) {
process.send(`http://${hostname}:${server.address().port}/`)
} else {
console.log(`http://${hostname}:${server.address().port}/`)
}
})
14 changes: 1 addition & 13 deletions test/utils/hello-world-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,9 @@ const hostname = '127.0.0.1'
const server = createServer(async (req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')

await sendInDelayedChunks(res, 'Hello World', 125)
res.end()
res.end('Hello World')
})

async function sendInDelayedChunks (res, payload, delay) {
const chunks = payload.split('')

for (const chunk of chunks) {
await new Promise(resolve => setTimeout(resolve, delay))

res.write(chunk)
}
}

server.listen(0, hostname, () => {
if (process.send) {
process.send(`http://${hostname}:${server.address().port}/`)
Expand Down
Loading