-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
lib: ensure every unhandled promise reason is wrapped in an error #21372
Conversation
This change adds more debugging context to any unhandled promise. If the unhandled promise does not have a reason which is an error, it is very difficult to determine where the error came from. This ensures every reason has an appropriate stack trace before the "next_tick" when the stack is lost. Refs: nodejs#16768
i would say that this should only be done if the rejection value is a string, and even then i wouldn't be enthusiastic about the coercion to an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @jd-carroll! Welcome, and thanks for the pull request! This will need a test before landing. If you want to try to write the test yourself, there is a guide in https://github.com/nodejs/node/blob/master/doc/guides/writing-tests.md. It's also possible that if this feature/change gets support, someone else might offer up a test, as I imagine the test file for this would be short.
I am certainly open to other ideas / strategies, but the intention is to capture a stacktrace when it is actually meaningful. We recently had to chase down an unhandled promise rejection which was rejecting with a So if there is a better way to capture the stack when the rejection actually occurs, and not on the next-tick (when |
Would it make more sense to hide this under a flag? Something like |
I like the general idea about improving this. However, I wonder if this is the right approach in this case. It somewhat mixes two different concerns. I personally feel like it would be best to print a warning in general in case a promise is rejected with a primitive. Some people might want to rely those though, so it could be a bit intrusive for these people. But most people are likely not aware that there is no stack trace in case a primitive is rejected. @benjamingr @addaleax what do you think about adding a specific option to first opt-in and as a semver-major opt-out to receive a warning with a stack trace in case a promise is rejected with a primitive? |
We did this in bluebird in version 2 and removed this behaviour in bluebird 3. People found this confusing and we ended up removing it. What we can do however is to create the That said - this would require the actual stack context for the unhandled rejection - the code in this PR creates the exception way too late. I think this might be doable when we get the async-stack-traces API from V8 - but I think it makes sense to do it after your planned logic rewrite with the setImmediate and closure replacing the WeakMap. |
@benjamingr I think your suggestion is already implementable – the V8 |
@bmeurer can you please confirm that is something you can/want to expose? |
@benjamingr @addaleax Not sure this was intended to be exposed. @hashseed and @gsathya might know. |
This indeed seems to be an oversight and could be added. The stack trace only exists for rejections caused by a thrown exception, and if detailed stack traces were enabled with |
Ideally |
Hey, this seems to have stalled. @jd-carroll are you interested in exploring the route outlined by Anna here #21372 (comment) ? |
@benjamingr I would love to submit a PR! But i’m a little lost as to where the most appropriate fix would be. Is this something that belongs in the C layer or in Node’s integration with the V8 runtime? Highly motivated to contribute something meaningful 👍 |
@hashseed @addaleax would either of you have time to guide @jd-carroll (not urgent) with those changes? |
@benjamingr @jd-carroll Sure! The first step would be exposing the The change itself would look something along the lines of adding a sibling method to the existing ones, e.g. https://github.com/v8/v8/blob/56baf56790de439b3f69e887e94beb3b301ed77c/include/v8.h#L6669 |
What's the status on this one? |
Does someone want to try to get this across the finish line? Or should this be closed? @nodejs/promises-debugging |
Going to close this at the moment, feel free to continue discussion in an issue. Thanks! |
This change adds more debugging context to any unhandled promise.
If the unhandled promise does not have a reason which is an error,
it is very difficult to determine where the error came from. This
ensures every reason has an appropriate stack trace before the
"next_tick" when the stack is lost.
Refs: #16768
If people like this change, I will happily add any tests/documentation as well.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes