Skip to content

nextjs: Network socket closes before events are being flushed on Vercel #14780

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

Closed
3 tasks done
Yuripetusko opened this issue Dec 18, 2024 · 15 comments
Closed
3 tasks done
Assignees

Comments

@Yuripetusko
Copy link

Yuripetusko commented Dec 18, 2024

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.42.0

Framework Version

react 18.3.1, next 14.2.4

Link to Sentry event

No response

Reproduction Example/SDK Setup

// apps/api/sentry-example-api/route.ts

import { NextResponse } from 'next/server'

export const dynamic = 'force-dynamic'

// A faulty API route to test Sentry's error monitoring
export const GET() {
  throw new Error('Sentry Example API Route Error')
  return NextResponse.json({ data: 'Testing Sentry Error...' })
}

Steps to Reproduce

@sentry/nextjs introduced a bug in version 8.36.0 that broke the auto instrumentation of nextjs api route handlers in next 14.2.4 (maybe other version too) App router.

The error in api route handler is not logged in sentry completly, and as soon as I downgrade to 8.33.0 it works fine. All version from 8.36.0 and up to the latest (8.42.0) have this bug.

Haven't investigated this deeper, but from a quick glance, perhaps this commit was when the issue was introduced?
#14084
Or this f9df1b4

Expected Result

For next: 14.2.4 api route handler unhandled error to be reported in sentry with latest @sentry/nextjs sdk

Actual Result

No error reported in Sentry

@s1gr1d
Copy link
Member

s1gr1d commented Dec 18, 2024

Hy, thanks for reporting this and also pinning down the version where this fails.
Based on the changelog it's probably one of those two PRs - we'll look into this.

@chargome
Copy link
Member

@Yuripetusko can't reproduce this, maybe there is a configuration issue. Can you provide a reproduction repository so we can better debug this?

@Yuripetusko
Copy link
Author

@Yuripetusko can't reproduce this, maybe there is a configuration issue. Can you provide a reproduction repository so we can better debug this?

Thanks for looking into it. I will re-test soon, as we discovered another issue that could have been the source of this problem all along. When using turbo as a build tool, you need to set env vars in turbo config to be exposed during build time, otherwise the values can be cached for previous builds so they are incorrect or undefined. and we were using process.env.VERCEL_ENV to conditionally enable sentry on production only. So will re-test with the latest sentry version of this is now fixed as we specified required env vars

@Yuripetusko
Copy link
Author

Yuripetusko commented Dec 30, 2024

@Yuripetusko can't reproduce this, maybe there is a configuration issue. Can you provide a reproduction repository so we can better debug this?

Just double checked this. Exactly the same code reports the api route error on 8.34.0 but not on 8.47.0

I will provide a reproduction soon

@getsantry getsantry bot moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 30, 2024
@Yuripetusko
Copy link
Author

Ok while preparing a reproducable example I discovered that an additional component is required to reproduce this issue, which is node 20.x on vercel. It works with node 22.x and latest version of @sentry/nextjs. While with node 20.x it works only with version 8.34.0 or below.

@Yuripetusko
Copy link
Author

Here's a reproduction. You can try deploying it on vercel with node 20.x and 22.x and see that 20.x doesn't work unless you downgrade sentry/nextjs version

https://github.com/Yuripetusko/sentry-14780-issue-reproduction

@chargome
Copy link
Member

chargome commented Jan 2, 2025

@Yuripetusko thanks for putting your effort into pinning this down. I looked into this today and found no reliable way of reproducing it, but nevertheless I was able to reproduce it sometimes. For me the node runtime had no impact, I was able to run into this in both 20 and 22.

The underlying issue probably that the vercel lambda runtime closes before the event gets flushed. I reached out to vercel already regarding this.

I wasn't able to reproduce it prior to v8.36 so it this very likely surfaced with our switch to Next.js OTEL performance monitoring in #13889.

@getsantry getsantry bot removed the status in GitHub Issues with 👀 3 Jan 2, 2025
@chargome chargome changed the title Api route handler unhandled error not reported in sentry with sentry/nextjs since 8.36.0. Auto instrumentation doesn't work for app router api route handlers nextjs: Network socket closes before events are being flushed on Vercel Jan 2, 2025
@Yuripetusko
Copy link
Author

@Yuripetusko thanks for putting your effort into pinning this down. I looked into this today and found no reliable way of reproducing it, but nevertheless I was able to reproduce it sometimes. For me the node runtime had no impact, I was able to run into this in both 20 and 22.

The underlying issue probably that the vercel lambda runtime closes before the event gets flushed. I reached out to vercel already regarding this.

I wasn't able to reproduce it prior to v8.36 so it this very likely surfaced with our switch to Next.js OTEL performance monitoring in #13889.

Thanks. We actually can reproduce it 100% of the time, but only on api route handlers or server actions (edge and node), but on client it works fine. So hence we suspect something about route wrapper and instrumentation. When I deploy this PR on vercel with node 20 it happens every time that errors are not caputred on sentry and with node 22 they are captured every time 🤔

For now we'll just update to node 22 so it's not a huge deal for us, but maybe relevant for other users

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 2, 2025
@Yuripetusko
Copy link
Author

I forgot one more thing. Since I wanted to reproduce it in an environment similar to ours, I use turbo build, so in vercel I set custom build command override

Image

turbo run build

Maybe that's another missing piece for you to reproduce consistently, in this case maybe it's turbo to blame

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 2, 2025
@getsantry getsantry bot removed the status in GitHub Issues with 👀 3 Jan 2, 2025
@chargome chargome removed their assignment Jan 21, 2025
@lforst lforst self-assigned this Mar 3, 2025
@lforst
Copy link
Member

lforst commented Mar 3, 2025

I think what we need to do here is call Vercel's waitUntil in our http instrumentation when we detect that we are on Vercel.

@osdiab
Copy link

osdiab commented Mar 6, 2025

Don't know if this is the same issue, but I am finding in my Vercel NextJS app as well that when calling captureException inside of a Node API Route (App Router), server logs make it to our log drain but Sentry only occasionally seems to get the message. Things are working consistently fine on the browser. We did get a small handful of Sentry messages through the API route, but mostly they don't make it, don't show up as dropped in Sentry nor does my app throw any errors about Sentry failing.

Just followed the default instructions for setting up Sentry and didn't touch the auto instrumentation options. We do use a tunnel route.

We don't use Turbo. We do use Node 22 though. If it is a waitUntil() issue then I would be surprised if the version bump really has an effect, but anything's possible 🤷🏼

Following this.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Mar 6, 2025
@lforst
Copy link
Member

lforst commented Mar 6, 2025

@osdiab Yeah the thing you're describing sounds like it would be fixed by adding waitUntil. Basically what is happening is that the Vercel lambdas are freezing before Sentry is able to flush out the events. Calling waitUntil will block the freezing until Sentry was able to flush out data. This comes at a compute cost though.

@lforst
Copy link
Member

lforst commented Mar 6, 2025

I forgot. We cannot do the generic waitUntil because Next.js doesn't let us patch http.

@lforst
Copy link
Member

lforst commented Apr 15, 2025

Actually, I forgot. Our Http instrumentation works on Next.js 13 and 15, but not 14.

@lforst lforst closed this as completed Apr 15, 2025
@lforst
Copy link
Member

lforst commented Apr 15, 2025

So this should be fixed for the latest Next.js versions!

@stephanie-anderson stephanie-anderson removed the Package: nextjs Issues related to the Sentry Nextjs SDK label Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

7 participants