-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nextjs): Use Vercel's waitUntil
to defer freezing of Vercel Lambdas
#12133
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
Conversation
size-limit report 📦
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed this offline, the approach LGTM! Great work!
// In case this is being run as part of a serverless function (as is the case with the server half of nextjs apps | ||
// deployed to vercel), make sure the error gets sent to Sentry before the lambda exits. | ||
await flushQueue(); | ||
vercelWaitUntil(flushSafelyWithTimeout()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, could we possibly still encapsulate this in some flushQueue
util or so? Feels a bit "dirty" to have all these vercelXXX
calls peppered throughout the code base, I think I'd prefer it if we could abstract this away a bit 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh it feels more dirty to me to mangle Vercel API with our own intentions. vercelWaitUntil
is basically just the vendored waitUntil
. Also, it's not like this is not readable or anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We literally call vercelWaitUntil(flushSafelyWithTimeout());
every time where we are calling this, as far as I see? 😅
I think it would be nicer to still just call flushQueue
everywhere (Which can literally be: function flushQueue() { vercelWaitUntil(flushSafelyWithTimeout()); }
now), call this everywhere. if we need to adjust this logic (e.g. it does more or less or whatever) we can encapsulate this there, no need to repeat this everywhere - we call this 9 times now?
But no strong feelings, if you prefer it this way all good for me :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the upside so I'll leave it as is.
waitUntil
to defer freezing of functionwaitUntil
to defer freezing of Vercel Lambdas
Co-authored-by: Francesco Novy <francesco.novy@sentry.io>
Fixes #12045
Fixes #11708
We have quite a bit of
await flush()
andflush()
in the Next.js SDK for the very simple reason that Vercel (which hosts a large number of our user-base) runs on AWS Lambdas which freeze as soon asres.end()
is called, essentially stomping any telemetry that we are attempting to flush.Luckily Vercel recently built a way of preventing the lambdas to freeze, even though the response was
.end()
ed. The functionality is abstracted through a package called@vercel/functions
but the code of thewaitUntil
function is just a callout to a global that is available on the Vercel JS runtime.Since we do not really want to depend on said
@vercel/functions
package we are vendoring that function in and call it with the flush promise whenever we assume that a lambda could be finishing soon.In the future we could be thinking about the following:
waitUntil(flush())
when we assume the lambda will shut down soon. (Potential drawback is that the signal handler could be called even though a request is currently active, losing telemetry for that request.)