sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

@sentry/nextjs: withSentry with webpack 5 crashes server with ERR_STREAM_WRITE_AFTER_END

Open AsherFoster opened this issue 5 years ago • 7 comments

  • [ ] Review the documentation: https://docs.sentry.io/
  • [ ] Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
  • [ ] Use the latest release: https://github.com/getsentry/sentry-javascript/releases
  • [ ] Provide a link to the affected event from your Sentry account

Package + Version

  • [x] @sentry/nextjs @ 6.5.0
  • [x] next @ 10.2.3

Description

When using @sentry/nextjs with webpack 5, calling any API function wrapped in withSentry will crash the server.

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
// next.config.js

const nextConfig = {
  future: {
    webpack5: true
  }
};

module.exports = withSentryConfig(nextConfig, sentryWebpackOpts);
// pages/api/hello.ts
import type { NextApiRequest, NextApiResponse } from 'next'
import { withSentry } from '@sentry/nextjs';

export default withSentry((req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).json({ name: 'John Doe' })
});

After a while debugging, the withSentry function seems to throw an error (well, it throws the value false) after the API function returns. This causes Nextjs to send a 500 error response, however because the function has already sent a response, this causes an uncaught ERR_STREAM_WRITE_AFTER_END error, crashing the server.

Workarounds

  • Downgrade to @sentry/nextjs @ 6.4.1
  • Use webpack 4
  • Remove withSentry uses

AsherFoster avatar Jun 02 '21 06:06 AsherFoster

Same setup and same issue. Thanks for reporting the temporary workaround -> downgrading to @sentry/nextjs @ 6.4.1 worked for me

joneath avatar Jun 02 '21 08:06 joneath

same issue here

aidan-ayala avatar Jun 02 '21 15:06 aidan-ayala

this move to @sentry/nextjs has been pretty nightmare-ish - y'all really should remove it from the official nextjs example as it's riddled with bugs and workarounds with very little documentation. Not a happy customer.

lcdproductions avatar Jun 02 '21 15:06 lcdproductions

Same issue here, just upgraded to 6.5.0, and it brought down our site. All NextJS API Routes started returning 500 errors with no Sentry logs / alerts, ERR_STREAM_WRITE_AFTER_END

nickfreeman avatar Jun 02 '21 15:06 nickfreeman

Hey, sorry about that, we are working on a fix for this as we speak.

HazAT avatar Jun 02 '21 16:06 HazAT

6.5.1 should unblock you all, we are working on a fix that resolves this.

HazAT avatar Jun 02 '21 17:06 HazAT

This should be fully resolved with https://github.com/getsentry/sentry-javascript/pull/3811. Please open another GH issue if there are further problems.

AbhiPrasad avatar Jul 14 '21 13:07 AbhiPrasad