Skip to content

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

Closed
@AsherFoster

Description

@AsherFoster

Package + Version

  • @sentry/nextjs @ 6.5.0
  • 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

Metadata

Metadata

Assignees

Labels

Package: nextjsIssues related to the Sentry Nextjs SDK

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions