undici: TypeError: fetch failed
Bug Description
Fetch throws ‘TypeError: fetch failed’ when posting
Reproducible By
Request with private production credentials to ERP
Expected Behavior
Successful response (ok). node-fetch
works just fine.
Logs & Screenshots
TypeError: fetch failed
at Object.processResponse (C:\Users\kyryl\dev\entriwise\server\node_modules\undici\lib\fetch\index.js:183:23)
at Fetch.fetchFinale (C:\Users\kyryl\dev\entriwise\server\node_modules\undici\lib\fetch\index.js:937:17)
at Fetch.mainFetch (C:\Users\kyryl\dev\entriwise\server\node_modules\undici\lib\fetch\index.js:744:17)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Environment
Linux and Windows, v17.5 v17.6
Additional context
Request takes up to several minutes until response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 12
- Comments: 83 (39 by maintainers)
Links to this issue
Commits related to this issue
- fix(cli): Add workaround for undici fetch DNS issue https://github.com/nodejs/undici/issues/1248 — committed to OpenNeuroOrg/openneuro by nellh a year ago
Maybe this can help?
I am experiencing the same with a Firebase Emulators host.
In one terminal:
In another:
Added: Once
npm run start
is running, you can exercise the server by:Trying with
127.0.0.1
instead oflocalhost
puts the problem at rest:As a temporary solution, please try this:
Had the same issue, had to switch from node v18 to v16 as a temp workaround.
@ronag I’m still having these errors. Cannot use undici fetch at all. Here is updated trace from Node v18:
@ronag Could you re-open or reply to my emails?
It works with
curl
- I am not very versed in using the normal http client, so… don’t know.Got some more light to why
localhost
isn’t recognized byfetch
.My
/etc/hosts
(macOS 12.2.1 Intel):If I comment out the line
::1 localhost
, things work as normal.Hello, I have been told I should post my example here as well since I might have the same error: https://stackoverflow.com/questions/73599889/fastapi-refuses-connection-on-a-parameterized-endpoint-if-called-sequentially-wi?noredirect=1#comment129970573_73599889 I am using svelte and fastapi and it seems that I can not fetch two endpoints together but only one of them each.
I also get some error, it’s not the same:
We have this issue with v20.4.0 also
@mcollina Could you please re-open as this started happening again in Node.js 20 with latest fetch and timeout of 5 minutes.
I’m facing the same issue with Next.js and Strapi. In localhost, the Next.js app can’t fetch the API from Strapi. I recently updated Node.js to version 18.15.0.
So this just started happening out of the blue. Working with firebase functions. From one minute to the next. I didn’t make any changes to my environment nor did i update or install packages between the time it worked and when it stopped. Restarted Firebase functions and it works again…
I don’t think it’s a network failure. On SvelteKit (which is where I got this error), once it happens once it keeps on happening multiple times (even if I restart node) and then just randomly decides to start working again. I also thought it could have been a network issue but everything else worked perfectly…
edit: typo
+1 node v20, simple fetch request, suddenly, no code changes
TypeError: fetch failed at Object.fetch (node:internal/deps/undici/undici:11522:11) { cause: SocketError: other side closed at Socket.onSocketEnd (node:internal/deps/undici/undici:9740:26) at Socket.emit (node:events:523:35) at endReadableNT (node:internal/streams/readable:1367:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { code: 'UND_ERR_SOCKET',
@bnb if that doesn’t solve the problem… I would definitely need a client/server combo that reproduces the problem.
I think I know what the problem is.
In node v17 we switched the default ordering of dns entries to follow what the OS is providing us vs reordering to put ipv4 always first.
I think we should try multiple addresses when dealing with localhost when establishing a new connection.
Wdyt @ronag?
@0oj I did by switching to Node 18.16.0
It can be, but it is not. Why? Because it happens only with a certain combination of Node / undici. And it doesn’t happen at all with other combinations. Thus it is a Node / undici issue. And we are talking about sampling periods of weeks here.
It is significant, but even more significant is the repro, which no one could produce so far because the issue is intermittent and not easily reproducible. In our case, for example, when it fails, it fails in like 1 request out of 1000. And as soon as we log the request and retry it, it works just fine without producing the error.
@pauldraper You’re right about the cause. But disregarding whether logging includes the cause or not, there is clearly something wrong with various versions of undici / Node.js. Because with some combinations of undici / Node.js versions
TypeError: fetch failed
doesn’t happen and with some it happens consistently often, whatever the underlying cause is.No one has provide a repro for them to look into
It’s not related to localhost in many cases.