Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waitUntil not working in cloudflare pages preset #2672

Closed
retuerto opened this issue Aug 18, 2024 · 2 comments
Closed

waitUntil not working in cloudflare pages preset #2672

retuerto opened this issue Aug 18, 2024 · 2 comments

Comments

@retuerto
Copy link

retuerto commented Aug 18, 2024

Environment

Nuxt 3.12.4 with Nitro 2.9.7 with cloudflare pages preset through nuxthub

Reproduction

https://stackblitz.com/edit/github-ch89s7?file=routes%2Findex.ts,plugins%2Fbackground.ts

Describe the bug

I just found out that it works in development environment but it doesn't work as expected when deployed on cloudflare throught nuxthub. event.waitUntil gets blocked and the user waits for the response when it shouldn't happen like that. I tried this and it works:

const waitFor = (ms: number) =>
  new Promise((resolve) => setTimeout(resolve, ms))

async function backgroundTask(id: number) {
  await waitFor(5000)
  console.log(`Task ${id} is done!`)
}

export default defineEventHandler((event) => {
  const cfCtx = event.context.cloudflare.context
  const taskId = Math.round(Math.random() * 1000)
  event.context.taskId = taskId
  let now = new Date()
  console.log('Time before backgroundTask : ' + now.toLocaleTimeString())
  cfCtx.waitUntil(backgroundTask(taskId))
  now = new Date()
  return `Task ${taskId} is running in background... : ${now.toLocaleTimeString()}`
})

Additional context

No response

Logs

No response

@Eckhardt-D
Copy link
Contributor

@atinux I'm not sure if this is related, but it seems that via NuxtHub this also causes issues with the cached endpoints and functions. I have a cached function with maxAge of 15s. I see the cache entries being created, but they are never updated / removed by Nitro if the mtime or expires date is past due, resulting in always seeing stale data. cc @pi0

image

@pi0
Copy link
Member

pi0 commented Oct 1, 2024

Consider that CPU (and exec) time limits still applied to waitUntil task (10ms free / up to 30sec for paid).

If CPU time for generating response + serialization + KV update takes more than this, it won't update the cache entry.

This is not a bug with nitro but a runtime limitation (which can be increased on paid plans of cf)

PS: Please consider using event.waitUntil, it will directly call cloudflare waitUntil from the context and is universally available.

@pi0 pi0 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants