Repository navigation
Unexpected error when using signal with reason in fetch #49557
Copy link
Copy link
Closed
nodejs/undici
#2597Closed
Copy link
Labels
fetchIssues and PRs related to the Fetch API.Issues and PRs related to the Fetch API.
Description
Version
18.17.1
Platform
Linux undefined 6.2.0-32-generic #32~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 18 10:40:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
fetch
What steps will reproduce the bug?
import express from 'express';
const app = express()
const port = 3333
app.get('/', (req, res) => {
setTimeout(() => {
res.send('Hey!')
}, 10_000)
})
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})async function test_signal() {
try {
const controller = new AbortController();
const signal = controller.signal;
setTimeout(() => {
const reason = 'heeeey';
// const reason = {
// reason: 'reason'
// };
// const reason = undefined;
controller.abort(reason);
}, 1_000);
const res = await fetch('http://localhost:3333/', {signal: signal});
await res.json()
console.log('request succeeded > ', res)
} catch (err) {
if (err.name === 'AbortError') {
console.log('request aborted > ', err);
} else {
console.log('request failed > ', err)
}
}
}
test_signal().catch(err => console.error('final error > ', err));How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
The expected behavior is to have [AbortError]: This operation was aborted with the reason obtained from the signal.
What do you see instead?
When reason = undefined
request aborted > DOMException [AbortError]: This operation was aborted
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async test_signal (/home/alex/WebstormProjects/test/abort-request.js:16:21)
When reason = object
request failed > { reason: 'reason' }
When reason = string
request failed > TypeError: invalid_argument
at Function.captureStackTrace (<anonymous>)
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async test_signal (/home/alex/WebstormProjects/test/abort-request.js:16:21)
Additional information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
fetchIssues and PRs related to the Fetch API.Issues and PRs related to the Fetch API.
Activity
zivni commentedon Sep 12, 2023
This also happens in node v20.6.1
atlowChemi commentedon Sep 13, 2023
@alexkorsun the underlaying issue is with https://github.com/nodejs/undici, and the issue here is the
reasonvalue is a string, and not an actual error.This
reasonis being passed intoError. captureStackTracewhich expects an object, and crashes when receiving a string (and any other non-object).This was already fixed in nodejs/undici#2243, which was released in
undici@5.24.0that did not yet reach Node (a PR for that is open: #49559)b5414 commentedon Nov 10, 2023
ShadowLNC commentedon Jan 4, 2024
@atlowChemi Can this issue be reopened please?
I am still seeing this issue in Node 20.10.0 (with Undici 5.26.4, based on the changelog).
I even tried Node 20.8.0 (with Undici 5.24.0 - which was the version you mentioned) and I got the same error.
atlowChemi commentedon Jan 5, 2024
@ShadowLNC does the code above reproduce the issue for you?
CC @nodejs/undici
ShadowLNC commentedon Jan 5, 2024
@atlowChemi Using the code from the original issue description, I do indeed get the
TypeErroras described.For my other testsing I was using this one-line script
fetch('https://example.com', {signal: AbortSignal.abort("fake error")});- output from the REPL is as follows (showingundefinedreason as well as string value)atlowChemi commentedon Jan 7, 2024
@ShadowLNC nodejs/undici#2597
garazy commentedon Apr 16, 2024
I came across this as I had the same issue even with v21.7.31. I fixed it with
new DOMException ( 'your reason' )as the parameter to the.abortmethod ofAbortController. This stopped this error from happening for me https://developer.mozilla.org/en-US/docs/Web/API/DOMExceptionCauses the undici error -
controller.abort()Doesn't cause the error -
controller.abort(new DOMException('reason'))Hope this helps anyone with similar issue.
equt commentedon Jul 4, 2024
FYI, the fix in undici was released in https://github.com/nodejs/undici/releases/tag/v6.3.0, and is available in Node in v22.0.0.