sentry-javascript: ERR_STREAM_WRITE_AFTER_END
Is there an existing issue for this?
- I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- I have reviewed the documentation https://docs.sentry.io/
- I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which package are you using?
SDK Version
7.17.2
Framework Version
NextJS 12.3.1
Link to Sentry event
No response
Steps to Reproduce
We’re using Sentry with Next.JS on Vercel. Last week we updated from @sentry/nextjs version 7.13.0 to 7.16.0, and we started seeing some requests to API endpoints returning 500 errors without any corresponding events in Sentry. We have since updated to @sentry/nextjs@7.17.2 and are getting the same errors. The Function logs on Vercel show up with an error message for ERR_STREAM_WRITE_AFTER_END.
Additional Info:
- Node Version: 16.16.0
- Webpack version: 5.73.0
We tried removing all of the wrapping withSentry calls from our API routes, but it didn’t stop these errors.
Expected Result
No internal stream errors output to Vercel’s function logs.
Actual Result
(Line Breaks added for readability)
2022-10-31T19:44:11.763Z 44cbcf3d-c3f9-48cf-8aa2-659e324a5ed5 ERROR Uncaught Exception
{"errorType":"Error",
"errorMessage":"write after end",
"trace":
["Error [ERR_STREAM_WRITE_AFTER_END]: write after end",
" at new NodeError (node:internal/errors:372:5)",
" at ServerResponse.end (node:_http_outgoing:846:15)",
" at ServerResponse.apiRes.end (/var/task/node_modules/next/dist/server/api-utils/node.js:346:25)",
" at ServerResponse.sentryWrappedEnd [as end] (/var/task/node_modules/@sentry/nextjs/cjs/config/wrappers/utils/responseEnd.js:29:22)",
" at processTicksAndRejections (node:internal/process/task_queues:96:5)"]}
Unknown application error occurred
I believe this to be related to https://github.com/getsentry/sentry-javascript/issues/4151
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 20 (6 by maintainers)
We suspect this is still happening on Vercel environments.
@pcardune Awesome! Thanks for letting us know.
I will close this issue just to clean up our backlog a bit but feel free to ping us here in case this comes up again.
Hi all - sorry for the delay, I was out for the holidays. I realize that calling
.end()and returning were probably not the best, but I thought it was still interesting to have code for reproducing this issue that didn’t include the rest of our stack. To answer your questions - I have only seen this error happen in production, but it doesn’t happen every time - only occasionally. Our production is in Vercel, I don’t have a different production environment to see if it happens.At this point
returnis only mildly suspicious to me because it differs from the examples. I’ll not get a chance to try this myself until after the US vacation weekend.Can you confirm that you only get the error in production? And is this only in production with Vercel? I haven’t managed to reproduce this error yet but I haven’t tried Vercel yet since it’ll be much more painful to debug!
We have been doing some more testing with this, and it looks like the issue is due to our
apollo-servergraphql endpoint being automatically instrumented with@sentry/nextjs> 7.14.0. When we addwithSentryto the same endpoint, even in version 7.13.0, we start seeing this error. Perhaps the real issue is that sentry and Apollo Server don’t play nice together in next API routes?