Skip to content

Commit

Permalink
run in separate thread maybe fixes it
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 13, 2024
1 parent 3f10dc6 commit d1d8686
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
39 changes: 27 additions & 12 deletions test/fetch/long-lived-abort-controller.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
'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 { fetch } = require('../../')
const { strictEqual, fail } = require('node:assert')

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

test('long-lived-abort-controller', { skip: isNode18 }, async (t) => {
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.write('Hello World!')
res.end()
}).listen(0)
// 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 emittedWarning = null
function onWarning (value) {
Expand All @@ -36,7 +51,7 @@ test('long-lived-abort-controller', { skip: isNode18 }, async (t) => {
// 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:437: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 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:437: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 (23, macos-latest) / Test with Node.js 23 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:285:24) at AbortSignal.addEventListener (node:internal/event_target:686:23) at Object.addAbortListener (/Users/runner/work/undici/undici/lib/core/util.js:643:12) at Request (/Users/runner/work/undici/undici/lib/web/fetch/request.js:437: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 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:437: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
})

Expand Down
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

0 comments on commit d1d8686

Please sign in to comment.