-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
process: allow multiple uncaught exception capture calbacks #24279
process: allow multiple uncaught exception capture calbacks #24279
Conversation
<!-- YAML | ||
added: v9.3.0 | ||
--> | ||
|
||
* `owner` {symbol} |
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.
owner
is a symbol so that each domain-like implementation can use a unique key to register their own capture callbacks.
} else { | ||
setUncaughtExceptionCaptureCallback(null); | ||
setUncaughtExceptionCaptureCallback((er) => { | ||
setUncaughtExceptionCaptureCallback(CAPTURE_CB_KEY, null); |
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.
Setting this to null
before setting it to an actual callback is actually not needed anymore, I'll update this shortly.
@@ -171,6 +171,8 @@ function setupChildProcessIpcChannel() { | |||
} | |||
} | |||
|
|||
process.domainsMap = new Map(); |
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.
process.domainsMap
is only needed to provide a way for applications to retrieve active domains, in the same way the currently active domain
instance is available as process.domain
. This might not necessarily be required though, and is here in this PR for now just to accommodate the REPL use case, which depends on being able to access any active domain in order to forward exceptions that are raised and caught by it to active domain instances..
if (exceptionHandlerState.captureFn !== null) { | ||
exceptionHandlerState.captureFn(er); | ||
if (exceptionHandlerState.captureFns.size !== 0) { | ||
for (const fn of exceptionHandlerState.captureFns.values()) { |
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.
the branch into multiple ownership here feels... off. these kinds of handlers should be at an application level, so the possibility of having like 30 functions all handling an uncaught exception seems a bit confusing
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.
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.
@misterdjules if we had some sort of stopPropagation behaviour, i think this would be doable.
maybe
for (const fn of exceptionHandlerState.captureFns.values()) {
if (fn() !== false) { break; } // return `false` to signal you didn't handle it, and the next function should be used
}
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.
I agree with @devsnek – the purpose of these hooks is to (possibly?) take ownership for the errors, otherwise we’d basically end up with a list of uncaughtException
listeners again
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.
The purpose of calling all (possibly many) registered callbacks was to address the use case tested by https://github.com/misterdjules/domaine/blob/master/test/test-domaine-domain-compat.js.
Basically, if one module runs a function within a user-land implemented domain-like module and that function runs another function within another domain-like instance, when an error is thrown in that second function, the goal would be to have both domain-like instances to handle that same error. The rationale for this is that we'd want those different domain-like implementations to not step on each other by handling errors for each other (think for instance of the case where those domain-like instances are created by different dependencies of the same Node app).
However, this is an opinionated design decision and I think I'd be totally fine with only allowing the top-level active domain-like instance (d2
in the example mentioned above) to handle uncaught errors.
In that case though it seems we'd need to be able to keep a single stack of all different domain-like instances and that exposing an API to set the capture callback to user-land is not useful, as there would be a need for setting one of them internally in core. @devsnek @addaleax What are your thoughts on this?
a5af4ca
to
7082a27
Compare
@@ -1720,15 +1703,6 @@ A `Transform` stream finished with data still in the write buffer. | |||
|
|||
The initialization of a TTY failed due to a system error. | |||
|
|||
<a id="ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET"></a> | |||
### ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET |
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.
We have a section for errors that were once present but are no longer below in this document :)
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.
Thanks for the pointer 👍 I'll add those errors to the Legacy Node.js Error Codes
section. Just to make sure: is it the section you were referring to?
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.
Yes, exactly. :)
ERR_UNHANDLED_ERROR | ||
} = require('internal/errors').codes; | ||
const { createHook } = require('async_hooks'); | ||
|
||
const CAPTURE_CB_KEY = Symbol('domain'); |
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.
Can we do something similar to what https://github.com/nodejs/node/blob/master/doc/guides/using-symbols.md suggests? e.g.
const CAPTURE_CB_KEY = Symbol('domain'); | |
const kCaptureKeyDomain = Symbol('kCaptureKeyDomain'); |
Having identical names for the variable and the symbol itself helps with grepping :)
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.
Great suggestion, I'll do that 👍
if (exceptionHandlerState.captureFn !== null) { | ||
exceptionHandlerState.captureFn(er); | ||
if (exceptionHandlerState.captureFns.size !== 0) { | ||
for (const fn of exceptionHandlerState.captureFns.values()) { |
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.
I agree with @devsnek – the purpose of these hooks is to (possibly?) take ownership for the errors, otherwise we’d basically end up with a list of uncaughtException
listeners again
throw new ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET(); | ||
|
||
if (typeof owner !== 'symbol') { | ||
throw new ERR_INVALID_ARG_TYPE('owner', ['Symbol'], owner); |
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.
I guess this makes this semver-major?
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.
why not use a Set and just add/remove the functions themselves
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.
@devsnek's suggestion seems like a good alternative, I'll try that 👍
@@ -1807,11 +1807,12 @@ This function is only available on POSIX platforms (i.e. not Windows or | |||
Android). | |||
This feature is not available in [`Worker`][] threads. | |||
|
|||
## process.setUncaughtExceptionCaptureCallback(fn) | |||
## process.setUncaughtExceptionCaptureCallback(owner, fn) |
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.
A bit concerned about backwards compat here. Is the new owner
argument optional? What about existing code that may be calling setUncaughtExceptionCaptureCallback(fn)
?
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.
why not use a Set instead of a Map and use the functions themselves as keys
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.
@devsnek's suggestion seems like a good alternative, I'll try that 👍
@misterdjules Still working on this? Should we add the |
@Trott Yes! Next steps from my perspective:
Adding the work-in-progress label sounds like a good idea 👍 |
After looking into this, I chose to take a different approach in solving this use case with #26326. That PR is in my opinion a better solution than this one, so I'll close this one for now. |
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesThis is a proof of concept for #23348. The goal is to enable the use case of having user-land implementations of the core
domain
module that are able to run concurrently with either the coredomain
module itself, or with other domain-like modules.I wrote such a user-land implementation of
domain
at https://github.com/misterdjules/domaine to test this, and used that user-land implementation in a custom branch of Restify to test it is working as expected.I will leave some comments inline to clarify some of the design/implementation decisions that might not be obvious at first sight.
I'm looking forward to reading your comments!
/cc @nodejs/domains @addaleax