Random page crash with Runtime.UnhandledPromiseRejection

Yes, trying to reproduce, unfortunately it’s very tricky because it’s very random, from 500-1k requests in the day, only a few of them have the crash, and it’s always when on my phone coming from a FB link or something random like this, not when I’m actually actively trying to break it. I’ll try today though, I already tried for 15 min right now without success. I’ll keep you posted.

I finally managed to get a reproducible example, although more on my side. I update collection data in the backend, refresh the collection page on the frontend and it crashes every time.
machinart.app.har (2.2 MB)

Hi @ksallee,

In that HAR file, I see 1 failed page and when I checked the logs for that, I found this in your function logs: Netlify App

{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "TypeError: fetch failed",
  "reason": {
    "errorType": "TypeError",
    "errorMessage": "fetch failed",
    "cause": {
      "errorType": "ConnectTimeoutError",
      "errorMessage": "Connect Timeout Error",
      "code": "UND_ERR_CONNECT_TIMEOUT",
      "name": "ConnectTimeoutError",
      "message": "Connect Timeout Error",
      "stack": [
        "ConnectTimeoutError: Connect Timeout Error",
        "    at onConnectTimeout (file:///var/task/.netlify/functions-internal/render.mjs:21859:26)",
        "    at file:///var/task/.netlify/functions-internal/render.mjs:21817:46",
        "    at Immediate._onImmediate (file:///var/task/.netlify/functions-internal/render.mjs:21848:9)",
        "    at processImmediate (node:internal/timers:466:21)"
      ]
    },
    "stack": [
      "TypeError: fetch failed",
      "    at fetch2 (file:///var/task/.netlify/functions-internal/render.mjs:29034:15)",
      "    at processTicksAndRejections (node:internal/process/task_queues:96:5)",
      "    at runNextTicks (node:internal/process/task_queues:65:3)",
      "    at processImmediate (node:internal/timers:437:9)",
      "    at async Object.fetch (file:///var/task/.netlify/functions-internal/render.mjs:31353:12)",
      "    at async Object.<anonymous> (file:///var/task/.netlify/functions-internal/render.mjs:30092:21)",
      "    at async getJobs (file:///var/task/.netlify/functions-internal/render.mjs:1015:15)",
      "    at async getNextJobs (file:///var/task/.netlify/functions-internal/render.mjs:4457:25)",
      "    at async refresh (file:///var/task/.netlify/functions-internal/render.mjs:4413:9)"
    ]
  },
  "promise": {},
  "stack": [
    "Runtime.UnhandledPromiseRejection: TypeError: fetch failed",
    "    at process.<anonymous> (file:///var/runtime/index.mjs:1194:17)",
    "    at process.emit (node:events:527:28)",
    "    at emit (node:internal/process/promises:140:20)",
    "    at processPromiseRejections (node:internal/process/promises:274:27)",
    "    at processTicksAndRejections (node:internal/process/task_queues:97:32)",
    "    at runNextTicks (node:internal/process/task_queues:65:3)",
    "    at processImmediate (node:internal/timers:437:9)"
  ]
}

It sounds like, whichever server you were trying to connect to, did not respond within the the timeout. This doesn’t look like a Netlify error.

But is there a way to know which call is timing out, or at least knowing what the timeout is so that I can try to adjust for this?
Or a way to increase that netlify timeout?

This is not a Netlify timeout issue, so changing that would not make a difference here.

You need to check the page’s code on which you’re getting the error. Your framework handles all the server-side functionality using a single Netlify Function, but within that, each page would behave how you’ve coded it. So if you’re having issue on /page1, check the API that you’re calling on that page.

OK I’ll try to look into it but I do a simple call to my server on that page, it’s quite fast, and it’s just a fetch from Svelte and it’s between a try catch so I don’t really understand how to fix it or even debug it for that matter since it’s wrapped in code I don’t have access to…

Are you @ksallee talking about the same website? If not, the above advise might not be relevant to you.

yes, sorry, replying via email.

Hi, @kevinyaya. Our support team doesn’t have any “secret” tools to debug serverless functions. Either the functions logs a helpful message or it doesn’t. If it does log a helpful message, that can be used to debug but what about situations like this one where the message isn’t helpful?

If the log messages are not helpful, then the recommendation is to add console.log() and/or console.time() statements around the code in question to reveal more information about the root cause.

Note, there is a timeout for function that our support team can increase but @hrishikesh’s comment above is still correct. The default timeout is 10 seconds and we can increase it to a recommended maximum of 26 seconds. @hrishikesh’s comment is still correct, however, because the function isn’t timing out due to that limited based on your logs from before:

Feb 22, 03:45:48 PM: 88190453 ERROR  Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: fetch failed","reason":{"errorType":"TypeError","errorMessage":"fetch failed","cause":{"errorType":"ConnectTimeoutError","errorMessage":"Connect Timeout Error","code":"UND_ERR_CONNECT_TIMEOUT","name":"ConnectTimeoutError","message":"Connect Timeout Error","stack":["ConnectTimeoutError: Connect Timeout Error","    at onConnectTimeout (file:///var/task/.netlify/functions-internal/render.mjs:21835:26)","    at file:///var/task/.netlify/functions-internal/render.mjs:21793:46","    at Immediate._onImmediate (file:///var/task/.netlify/functions-internal/render.mjs:21824:9)","    at processImmediate (node:internal/timers:466:21)"]},"stack":["TypeError: fetch failed","    at fetch2 (file:///var/task/.netlify/functions-internal/render.mjs:29010:15)","    at processTicksAndRejections (node:internal/process/task_queues:96:5)","    at runNextTicks (node:internal/process/task_queues:65:3)","    at processImmediate (node:internal/timers:437:9)","    at async Object.fetch (file:///var/task/.netlify/functions-internal/render.mjs:31329:12)","    at async Object.fetch (file:///var/task/.netlify/functions-internal/render.mjs:30068:21)","    at async getJobs (file:///var/task/.netlify/functions-internal/render.mjs:1015:15)","    at async getNextJobs (file:///var/task/.netlify/functions-internal/render.mjs:4448:25)","    at async refresh (file:///var/task/.netlify/functions-internal/render.mjs:4411:9)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: fetch failed","    at process.<anonymous> (file:///var/runtime/index.mjs:1194:17)","    at process.emit (node:events:527:28)","    at emit (node:internal/process/promises:140:20)","    at processPromiseRejections (node:internal/process/promises:274:27)","    at processTicksAndRejections (node:internal/process/task_queues:97:32)","    at runNextTicks (node:internal/process/task_queues:65:3)","    at processImmediate (node:internal/timers:437:9)"]}
Feb 22, 03:45:48 PM: 88190453 ERROR  [ERROR] [1677102348079] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 400.
Feb 22, 03:45:48 PM: 88190453 ERROR  RequestId: 88190453-18ce-4659-8123-30c9dd718090 Error: Runtime exited with error: exit status 128
Feb 22, 03:45:48 PM: 88190453 ERROR  Runtime.ExitError
Feb 22, 03:45:48 PM: 88190453 Duration: 8.09 ms	Memory Usage: 116 MB

That shows it exited after less than 9 ms so the 10 second timeout isn’t a factor for these errors in any way. We could increase the timeout but that won’t change the error above at all. In order to fix that error the code in question requires debugging and the console.log() and console.time() statements are the recommended way to do that.

If that doesn’t help reveal the root cause or if there are other questions, please do reply here anytime and we’ll be ready to assist.

I think I managed to find the root cause, or at least fix the problem because I never fully understood what was happening.
Even with the try/catch, things seem to act weirdly in SvelteKit’s fetch calls in layout.js https://kit.svelte.dev/docs/routing#layout-layout-js
I moved everything to calls within the routes/components themselves, with try/catch/retry logic and things seem to behave.

Thank you for stating so clearly the fault was in my code!

Hi, @kevinyaya. Thank you for letting us know the issue is resolved and also for sharing how you resolved it. If there is anything else to share, please feel free to reply here anytime and new topics are also always welcome.