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

test: issue #2026 #2032

Closed
wants to merge 1 commit into from
Closed
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
75 changes: 75 additions & 0 deletions test/fetch/issue-2026.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict'

const { cpus } = require('os')
const { isMainThread, Worker, parentPort } = require('worker_threads')
const { test } = require('tap')
const { fetch } = require('../..')

const THREADS = cpus().length - 1
const ROUNDS = 50
const PARALLEL_REQUESTS = 6

if (isMainThread) {
test('https:/nodejs/undici/issues/2026', async (t) => {
let stuckWorkers = 0
const timeout = setTimeout(() => {
if (stuckWorkers > 0) {
t.fail(
`Not finished in 40s. ${stuckWorkers} workers finished fetch() calls but cannot exit.`
)
}
}, 40_000)

async function task (workerId) {
const worker = new Worker(__filename)

await new Promise((resolve) =>
worker.on(
'message',
(message) => message === 'FETCH_FINISHED' && resolve()
)
)

const timer = setTimeout(() => {
stuckWorkers++
console.error(`Unable to terminate worker #${workerId}`)
}, 10_000)
await worker.terminate()
clearTimeout(timer)
}

const pool = Array(ROUNDS).fill(task)

async function execute () {
const task = pool.shift()

if (task) {
await task(ROUNDS - pool.length)
return execute()
}
}

await Promise.all(
Array(THREADS)
.fill(execute)
.map((task) => task())
)

clearTimeout(timeout)
})
} else {
const urls = [
new URL('/v4/starlink', 'https://api.spacexdata.com'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid doing any calls to external servers

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been trying to reproduce this issue with local node:http server without success. I'm serving the raw data with Content-Encoding: br header but something is still missing as the issue does not appear.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think figuring that out will get us the fix.

new URL(
'/v4/starlink/5eed770f096e59000698560d',
'https://api.spacexdata.com'
)
]

const count = Math.round(PARALLEL_REQUESTS / urls.length)
const requests = Array(count)
.fill(urls.map((url) => fetch(url).then((r) => r.json())))
.flat()

Promise.all(requests).then(() => parentPort.postMessage('FETCH_FINISHED'))
}