Unhandled Rejection Abort Proposal #27
Description
(Written by @BridgeAR and @benjamingr)
New proposal following gathering user data for the promise-use-cases talk.
Migration & Opt-out Path
We propose an environment variable called UNHANDLED_REJECTION
which Node.js will use in order to determine what the behaviour for unhandled rejection is.
The environment variable can take two values abort
and warn
.
- Abort enables GC unhandled rejection detection and the abort semantics
- Warn disable the GC unhandled rejection detection and keeps the current "warn" semantics
If the user passes another value - Node.js should throw an error. We propose that the default value for the flag is warn
in v10 and v8 of Node and abort
from v11 on.
An environment variable has the advantage of being backportable to older versions of Node.js and makes it easier for people to upgrade and use the semantics they want. It's easy to opt-in and opt-out of the environment variable.
Actual semantics:
- Keep the current warnings and hooks for
unhandledRejection
andrejectionHandled
. Warning will not be printed in case you have the hook (current behaviour). - If promise is unhandled on garbage collection trigger an uncaught exception (crash and go through
uncaughtException
).- No need to add additional semantics for opt-out, if you want to opt-out of this you can add a
.catch
inside theunhandledRejection
hook. - Add a second optional argument to
uncaughtException
that will allow optionally detecting if the exception originated from a promise.- Note that this isn't strictly necessary because of the
.catch
ability inside the handler but people have asked for this.
- Note that this isn't strictly necessary because of the
- No need to add additional semantics for opt-out, if you want to opt-out of this you can add a
We think that a minimal implementation would be a good idea.
Future Semantics
- Eventually combine the warn and abort semantics and move the WeakMap for
unhandledRejection
to C++. - On Exit - log all the pending unhandled rejections (explicitly let's not solve this now).