You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with an API that is a bit unstable; in some cases, the server might go down entirely, and I need to account for that, log an error, and retry later. Alas, if the server is down Node encounters a refused connection, which in turn triggers a fetch failed error in Undici, and that - for some reason - kills the entire process.
Reproducible By
Take a look at this example (the Open Notify API used here isn't available via HTTPS, triggering the same error):
asyncfunctionloadPosition(){constresponse=awaitfetch('https://api.open-notify.org/iss-now.json');constdata=awaitresponse.json();if(!response.ok||!data.iss_position||data.message!=='success'){thrownewError('Invalid response');}const{latitude, longitude}=data.iss_position||{};return{latitude, longitude};}setTimeout(async()=>{try{const{latitude, longitude}=awaitloadPosition();console.log(`The ISS is at ${latitude}/${longitude}!`)}catch(error){console.error('Could not fetch ISS position data');}finally{setTimeout(loadPosition,5000);}},5000)
Expected Behavior
If a connection error occurs, I'd expect it to be caught in the catch block, reported on the console, and be done with; the newly scheduled timeout in the finally block should be run after just fine.
Logs & Screenshots
Instead, this causes the process to exit immediately:
Could not fetch ISS position data
node:internal/deps/undici/undici:13737
Error.captureStackTrace(err, this);
^
TypeError: fetch failed
at node:internal/deps/undici/undici:13737:13
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at Timeout.loadPosition (/index.ts:53:22) {
[cause]: Error: connect ECONNREFUSED 138.68.39.196:443
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '138.68.39.196',
port: 443
}
}
Node.js v21.7.1
[Process exited with code 0]
Environment
MacOS 14.3.1
NodeJS 21.7.1
The text was updated successfully, but these errors were encountered:
👋
The issue is rooted to the way your code is scheduling the retry within the timeout for retrying once more.
The first setTimeout has the correct statements for handling a situation of a failure in case of an error, but on the second timeout called within the finally scope, it just calls the loadPosition once more without any error handling; meaning that if it fails, it will be treated as an unhandled rejection leading to the process termnation.
A possible alternative can be:
asyncfunctionloadPosition(){constresponse=awaitfetch('https://api.open-notify.org/iss-now.json')constdata=awaitresponse.json()if(!response.ok||!data.iss_position||data.message!=='success'){thrownewError('Invalid response')}const{ latitude, longitude }=data.iss_position||{}return{ latitude, longitude }}constcallback=async()=>{try{const{ latitude, longitude }=awaitloadPosition()console.log(`The ISS is at ${latitude}/${longitude}!`)}catch(error){console.error('Could not fetch ISS position data')}finally{setTimeout(callback,5000)}}setTimeout(callback,5000)
It produces:
Could not fetch ISS position data
Could not fetch ISS position data
Could not fetch ISS position data
Bug Description
I'm working with an API that is a bit unstable; in some cases, the server might go down entirely, and I need to account for that, log an error, and retry later. Alas, if the server is down Node encounters a refused connection, which in turn triggers a fetch failed error in Undici, and that - for some reason - kills the entire process.
Reproducible By
Take a look at this example (the Open Notify API used here isn't available via HTTPS, triggering the same error):
Expected Behavior
If a connection error occurs, I'd expect it to be caught in the
catch
block, reported on the console, and be done with; the newly scheduled timeout in thefinally
block should be run after just fine.Logs & Screenshots
Instead, this causes the process to exit immediately:
Environment
MacOS 14.3.1
NodeJS 21.7.1
The text was updated successfully, but these errors were encountered: