-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
unhandled promise crahing server with sentry middleware #6750
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
Comments
Hi @modestaspruckus thanks for writing in. Can you post your |
Hey @Lms24, here is the config. Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.ENV_NAME || 'local',
release: `backend-${process.env.ENV_NAME}@${process.env.BUILD_NUMBER}`,
}); I'm not using any integrations. Additionally, there is sentry error middleware app.use(Sentry.Handlers.errorHandler()); |
Hey, any news for this? Various API calls sometimes fails due network issues and this causes node instances to restart. This wasn't the case on This can simply be reproduced by rejecting promise app.get('/test', async (req, res, next) => {
const promise = new Promise((resolve, reject) => {
reject(new Error('Something bad hapened'));
});
res.json();
}); By commenting out sentry middleware it starts working as expected // app.use(Sentry.Handlers.requestHandler()); |
@modestaspruckus no news yet. I quickly tried reproducing this and so far I couldn't. Here's what I did:
const express = require("express");
const Sentry = require("@sentry/node");
const app = express();
Sentry.init({
dsn: "___DSN___",
environment: "local",
release: `backend-test@$1`,
});
app.get("/test", async (req, res, next) => {
const promise = new Promise((resolve, reject) => {
reject(new Error("Something bad hapened"));
});
res.json();
});
// app.use(Sentry.Handlers.errorHandler());
app.listen(3000);
console.log("Listening on port 3000");
Regardless of using the errorHandler or not, the app doesn't crash for me. Can you provide a full reproduction so that we can investigate this? Oh, one more thing: The order in which you add middleware matters. According to our docs, you should add the errorhandler "before any other error middleware and after all controllers". |
You need to add request middleware also app.use(Sentry.Handlers.requestHandler()); |
I've created repository to debug - https://github.com/modestaspruckus/sentry-debug
Comment out |
Thanks for the repro (and my bad about the request handler, I misread your description, sorry). I can reproduce it now. Will try to find a fix. |
Update: It seems #5627 is the culprit here. Let's see how to fix this |
Hi @modestaspruckus, I thought a little about this and did some research: We can't revert #5627 as it would be a breaking change (I know ironic, as introducing it was also kind of an accidental breaking change - really sorry about that). After some research, I think the problem is more related to the fact that your handler is app.get("/test", async (req, res, next) => {
try {
const promise = new Promise((resolve, reject) => {
reject(new Error("Something bad hapened"));
});
await promise;
} catch (error) {
next(error);
}
res.json();
}); More information about async handlers: https://zellwk.com/blog/async-await-express/ Hope this helps. Let me know if this issue is resolved or you have additional questions :) |
Thanks @Lms24. Adding try/catch is a solution.
https://github.com/modestaspruckus/sentry-debug/blob/main/index.js#L7 |
We are running into the same issue preventing us from being able to upgrade to the latest sentry. We will see if this works for us as well... |
We're running into this also. Unhandled async errors work fine when using Sentry 7.11.1, but start breaking in 7.12.0 when running express 4.18.2.
We're going to stick with Sentry 7.11.1 until we get things fixed up. Ultimately, this seems to come down to an anti-pattern of allowing unhandled async errors / promise rejections in Express. According to the express documentation:
It seems like something in older versions of Sentry actually captures these unhandled promise rejections and masks the true problem. |
Yes, after researching and discussing this internally, we came to the same conclusion. I'm going to close this issue, as it seems most users here were able to resolve this with properly handling async errors. Everyone, feel free to leave are reply if things are still unclear. |
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which package are you using?
@sentry/node
SDK Version
7.30.0
Framework Version
No response
Link to Sentry event
No response
Steps to Reproduce
Sentry request handler middlware is changing behaviour of unhandledPromise. Without this middleware below
Error with unhandled promise is thrown with
unhandledRejection
event which doesn't force node instance to restart.With middleware above the error is thrown with
uncaughtException
event which crashing the node server.Expected Result
Sentry.Handlers.requestHandler()
middleware should not change behavior of promise errors.Actual Result
Sentry.Handlers.requestHandler()
middleware is changing promise error behaviorThe text was updated successfully, but these errors were encountered: