-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[@sentry/nextjs] Errored API routes trigger "API resolved without sending a response" #3852
Comments
We're getting these false positives as well. I haven't put in the time to look at testing this but I think that the only way to workaround this issue is in the wrapper, since there are transitive calls to |
It does this for all my requests as well, even though I do this:
I hope this will be fixed :) |
Any plan on this one? I'm currently doing something like this to get rid of the message
|
I am having the same issue in development. I have not deployed it to production to verify yet. This gets logged in the server log:
And no error is logged in sentry. If I have the barebone code from the example, it works fine. However, my actual code does a call to server and uses |
You can set |
@kaykdm Thanks for this workaround! Is there a way to set a default config for all API routes ? From the docs it seems that there is no option to do this in @KieraDOG the workaround in this comment did not work for me. This resulted in the response being ended before the actual content is sent, which means stalling requests (especially in a development environment). |
Why are you guys monkey patching .end() ? Can you not just return a promise to Next? |
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you label it "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
Please don't close - this is still an issue that should be resolved |
Is this fixed? |
…warning (#4139) This prevents a false positive warning from nextjs which arises from the interaction of our API route wrapper `withSentry()` with nextjs's native API route handling. In dev, nextjs checks that API route handlers return a response to the client before they resolve, and it throws a warning[1] if this hasn't happened. Meanwhile, in `withSentry()`, we wrap the `res.end()` method to ensure that events are flushed before the request/response lifecycle finishes. As a result, there are cases where the handler resolves before the response is finished, while flushing is still in progress. This triggers the warning mentioned above, but it's a false alarm - the response may not have ended _yet_, but it will end. This suppresses the warning by temporarily marking the response finished, and then restoring it to unfinished before the original `res.end()` is called. The fix here is simple, but the async-i-ness of it all leads to some complex interactions in sequencing between the SDK, nextjs, and Node itself. I've tried to lay it out as clearly as I can in comments. Fixes #4007 Fixes #3852 [1] https://github.com/vercel/next.js/blob/e1464ae5a5061ae83ad015018d4afe41f91978b6/packages/next/server/api-utils.ts#L106-L118
@adikari This is a different error |
@adarnon my bad. I Mixed up different issues :) |
…ue (#4516) In #4139, a change was introduced in order to suppress a false positive warning thrown by nextjs, by setting `res.finished` to `true` just long enough for nextjs to check it and decide no warning was needed. In simple cases, it worked just fine, but in some set-ups the "just long enough for nextjs to check it and calm down" turned out to also be "just long enough for other things to check it and get mad." This backs out that change, as it seems it's doing more harm than good. In order to address the original problem (documented in [1] and [2]), a new warning is added, explaining that the false positive is just that. Not as elegant as suppressing the message altogether, but it should tide us over until such time as we're able to try again with a different approach. Fixes #4151. [1] #3852 [2] #4007
Why is this error closed? With the last version I still see the error, there is a warning but the problem is still there. |
Because it was fixed... in a way which turned out to break other things, and had to be reverted. I'll reopen. As for finding a new solution, I'm open to ideas. We've been focusing on trying to ship our next major version, so I haven't been able to dive back into this at a deep level, but I will admit that even coming up with my turned-out-to-cause-other-problems hack took quite a bit of poking and prodding and digging through not only nextjs source code but node source code as well, and so I don't expect finding a new approach is going to be a trivial problem. In the meantime, you can suppress next's warning yourself by using the "external resolver" option in your API route (docs here). I've also just pushed #4706, to allow our warning to be suppressed as well. |
I am running a tRPC backend through NextJS with a Sentry wrapper, and this is by far the nicest solution imo. It worked like a charm. edit: grammar |
Setting This is my API code to test - very trivial const handler = (request: NextApiRequest, response: NextApiResponse) => {
response.status(200);
response.end();
};
export default withSentry(handler); EDIT: if I remove the |
I use this config and the messages at the console stop but still does not log the errors in Sentry Dashboard. I change the withSentry hoc in my api for |
We ever fixing this or what? Annoying to have logs spammed. |
Ok I made a quick little hack around until this issue is resolved... it's not the cleanest but at the end of each
Annoying but works. |
This is the way that is in the Sentry docs. But for me it doesn't show anything at Sentry dashboard :/. |
It seems like the Edit: I don't even see anything in the repo that'd be looking for such a variable, but maybe it's buried in the CLI itself? In my case it's being provided in |
Same boat as @bsplosion, Edit: I lie. The |
#336) * fix: set config.api.externalResolver to true in order to avoid weird behavior (getsentry/sentry-javascript#3852 (comment)) * Update packages/web/app/src/lib/api/extract-access-token-from-request.ts
As part of #5505, this applies to API route handlers the same kind of auto-wrapping we've done with the data fetchers (`withServerSideProps` and the like). Though the general idea is the same, the one extra complicating factor here is that there's a good chance the handlers get to us already wrapped in `withSentry`, which we've up until now been telling users to use as a manual wrapper. This is handled by making `withSentry` idempotent - if it detects that it's already been run on the current request, it simply acts as a pass-through to the function it wraps. Notes: - A new template has been created to act as a proxy module for API routes, but the proxying work itself is done by the same `proxyLoader` as before - it just loads one template or the other depending on an individual page's path. - Doing this auto-wrapping gives us a chance to do one thing manual `withSentry` wrapping isn't able to do, which is set the [route config](https://nextjs.org/docs/api-routes/request-helpers) to use an external resolver, which will prevent next's dev server from throwing warnings about API routes not sending responses. (In other words, it should solve #3852.)
For anyone who still is getting the log messages, 7.14.0 doesn't resolve the logs on its own. You actually have to go through and strip out all the workaround code you've put in, including the Thanks for getting a solution in place, @lobsterkatie. The logging is much better now! |
I'm still seeing the messages on |
I can confirm this is an issue on I didn't see anything in the diff which would indicate the breaking change, but the constant logging has definitely returned. Downgrading to |
Still hoping for a solution here. |
Hey @statico - since we've changed a lot with the SDK the last couple of versions (we are on
Let's see if we can figure out whats going on here! |
Package + Version
@sentry/nextjs
Version:
Description
Describe your issue in detail, ideally, you have a reproducible demo that you can show.
The
withSentry()
API route wrapper monkeypatchesres.end()
function to await for flush before closing a request. While this is certainly useful for serverless environments, it could cause a false positive warning on deployments with a custom server (e.g., Express).Since normal Next API route code does not await for
res.end()
to finish, the handler's promise returns before the response has ended properly. This causes Next to detect the API route as having finished without a response, even though the response is sent a few moments later. Here is an example:A workaround is to wrap all API routes with another callback that always performs
await res.end()
before returning.The text was updated successfully, but these errors were encountered: