undici: Undici.Request and AbortController doesn't work well
Bug Description
Sometimes Aborting Undici.Request causes whole program to stop, even though everything is wrapped around try catch.
Reproducible By
I don’t have minimal reproducible example, as my code is larger in size. But Undici.Fetch doesn’t have this issue.
Here is minimal reproducible example:
const undici = require('undici')
async function testUndici() {
const controller = new AbortController()
const {statusCode,headers,trailers,body} = await undici.request("https://github.com/fawazahmed0/tiger/releases/download/v2/pipetocsv.7z", { signal: controller.signal }).catch(console.error)
setTimeout(()=>controller.abort(), 500)
}
testUndici()
Expected Behavior
Undici.Request and AbortController should work
Logs & Screenshots
node:events:498
throw er; // Unhandled 'error' event
^
DOMException [AbortError]: This operation was aborted
at new DOMException (node:internal/per_context/domexception:53:5)
at AbortController.abort (node:internal/abort_controller:395:18)
at Timeout._onTimeout (T:\xx\other scripts\undici-debug.js:7:31)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
Emitted 'error' event on BodyReadable instance at:
at emitErrorNT (node:internal/streams/destroy:170:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Node.js v22.3.0
Environment
Ubuntu 24.04 & Windows 11 Node v22.3.0 Undici 6.19.1
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 42 (41 by maintainers)
Commits related to this issue
- fix: handle stream error to prevent uncaught DOMException and consume body if not used (per <https://github.com/nodejs/undici/issues/3353\#issuecomment-2184635954>) — committed to forwardemail/nodejs-dns-over-https-tangerine by titanism 2 years ago
- fix: attempt to fix uncaught exception per <https://github.com/nodejs/undici/issues/3353> — committed to forwardemail/nodejs-dns-over-https-tangerine by titanism 2 years ago
- fix: attempt to fix uncaught exception per <https://github.com/nodejs/undici/issues/3353> — committed to forwardemail/forwardemail.net by titanism 2 years ago
- Bugfix - when using AbortController, errors on resulting stream must be caught (#539) * use-native-fetch * Bugfix: `res.body` should handle errors according to https://github.com/nodejs/undici/iss... — committed to webtorrent/bittorrent-tracker by uriva 2 years ago
@mcollina wdyt? I think it’s a reasonable opinion but not sure what to do with it. Would break everyone to change it now.
Hi team, we got bitten by this issue in production like others in this thread.
I think that while
Readableandundiciare each working as intended, the combination of the two leads to an API that is unnecessarily hostile for a user.In our case, the issue manifested because we’re holding onto a reference to
clientResto pipe into a subsequent bit of work. However, after obtaining a reference toclientResand before callingpipeline(clientRes.body, nextConsumer, { signal }), we callsignal.throwIfAborted().This leads to a whole-program crash despite the whole sequence above being orchestrated in an
async function, attempting idiomatic use ofAbortSignal.The fix, in our case is the following, which I believe is indicative of the dangerous nature of this interaction:
There is nothing to fix. It works as designed.
request returns a body: Stream which gets destroyed with an abort error and emits an error that you are not handling and therefore the process crashes with an unhandled exception. This is normal nodejs behavior. Feel free to open a PR if you think that needs to be explicitly documented by undici.