getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
8.01k stars 1.58k forks source link

Sentry doesn't report `UnhandledRejection` errors in AWS Lambda #11431

Open jackie-greenbaum opened 7 months ago

jackie-greenbaum commented 7 months ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/aws-serverless

SDK Version

7.84.0

Framework Version

Remix 2.4.0

Link to Sentry event

No response

SDK Setup

Sentry.init({
    tracesSampleRate: 1,
    environment,
    includeLocalVariables: true,
    attachStacktrace: true,
    release: process.env.SENTRY_RELEASE
})

Steps to Reproduce

Deploy a lambda function which inits Sentry with default args and then trigger an UnhandledPromiseRejection

Expected Result

In my AWS Lambda function configured with Sentry, when I trigger an unhandledRejection, I expect Sentry to capture that error and create an issue for it.

Actual Result

However, in my case it appears as though the Sentry unhandledRejection handler is getting clobbered by the handler added by AWS Lambda.

Here's a printout of the two handlers that node currently knows about it, the first from AWS and the second from Sentry

Screenshot 2024-04-04 at 9 36 44 AM

As a workaround, we can (before Sentry.init) call process.removeAllListeners('unhandledRejection'); as suggested by AWS, but this feels like something Sentry should handle or at least warn the user about.

AbhiPrasad commented 7 months ago

This is not ideal, but there's not much we can do because this is the behaviour that Node.js itself expects.

We should evaluate switching to using https://nodejs.org/api/process.html#event-uncaughtexceptionmonitor now that the SDK supports Node 14+ in v8.

Backlogging this for now, but PRs are welcome if you have any ideas for improving this behaviour.

jackie-greenbaum commented 7 months ago

Appreciate the quick response!

Perhaps something should be added to the documentation here if we don't (currently) expect UnhandledRejections to be reported?

Unless I'm misunderstanding

AbhiPrasad commented 7 months ago

We should add the workaround of clearing listeners and using Sentry's in the docs - PRs are welcome!

https://github.com/getsentry/sentry-docs/edit/master/docs/platforms/node/common/configuration/integrations/default-integrations.mdx

Gervwyk commented 2 months ago

Also experiencing this issue. Any change this can be fixed? or a workaround?

Gervwyk commented 2 months ago

If you point me in the right direction i'd be happy to give it a shot.

Gervwyk commented 2 months ago

For others, currently a template render error in https://github.com/resend/react-email will surface this error and not show up in Sentry.

andreiborza commented 2 months ago

Hi @Gervwyk, sorry for the late response. The team is at a conference.

The workaround is to call process.removeAllListeners('unhandledRejection'); before you call Sentry.init. Meaning you can't rely on our auto-patching of the lambda function handler (if you're using our AWS Lambda layer). This means you'll have to manually wrap the handler with Sentry.wrapHandler, see more details here.

I will add this to the docs, and also investigate if the SDK can switch to uncaughtExceptionMonitor.