-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Remove async_hooks runInAsyncIdScope API #14328
Comments
I'd like to voice an issue I encountered when trying to use |
@TimothyGu Short answer is that you can use |
@AndreasMadsen Okay. I would still much prefer a more generic solution though, like so: class MyClass extends EventEmitter {
constructor() {
super();
AsyncResource.call(this, 'name');
}
method(cb) {
this.on('event', () => {
native((err, data) => {
this.emitBefore();
try {
cb(err, data);
} finally {
this.emitAfter();
}
});
});
}
}
Object.defineProperties(
MyClass.prototype,
Object.getOwnPropertyDescriptors(AsyncResource.prototype)); I don't want to derail the original conversation of course, and I'd be happy to move this into a new issue. |
So, EDIT: I tracked down the last commit left in my repo to show that it did at one point do as previously intended at trevnorris@4b16a58#diff-0bb01a51b135a5f68d93540808bac801R179 |
@TimothyGu Please do. |
@TimothyGu Please add the link to the new issue here when it's created. |
PR-URL: #16972 Refs: #14328 Refs: #15572 Reviewed-By: Anna Henningsen <[email protected]>
PR-URL: nodejs#16972 Backport-PR-URL: nodejs#18179 Refs: nodejs#14328 Refs: nodejs#15572 Reviewed-By: Anna Henningsen <[email protected]>
Backport-PR-URL: #18179 PR-URL: #16972 Refs: #14328 Refs: #15572 Reviewed-By: Anna Henningsen <[email protected]>
As I mentioned a few months ago I'm not comfortable exposing the low-level async_hooks JS API. In the documentation PR #12953 we agreed to keep the low-level JS API undocumented. Some time has now passed and I think we are in a better position to discuss this.
The low-level JS API is quite large, thus I would like to separate the discussion into:
setInitTriggerId
andtriggerIdScope
. (Remove async_hooks setTrigger API #14238)runInAsyncIdScope
. (this)newUid
,emitInit
,emitBefore
,emitAfter
andemitDestroy
(Remove async_hooks Sensitive/low-level Embedder API #15572).Note: There is some overlap between
emitInit
andsetInitTriggerId
in terms ofinitTriggerId
. Hopefully, that won't be an issue.Background
runInAsyncIdScope(asyncId, cb)
creates a new scope with theasyncId
asthe
executionAsyncId
and with the currentexecutionAsyncId
asthe
triggerAsyncId
. It does so without invoking the before and after hooks.runInAsyncIdScope
was not part of the originalasync_hooks
EP butwas included in the
async_hooks
PR.runInAsyncIdScope
is not used anywhere in node-core and as such, it purposeis not well documented. @trevnorris mentions it only a single time:
Although, later @trevnorris says it is a bad example.
Issues
emitBefore
andemitAfter
are not used, thusthe before and after hooks are never invoked. This can be an issue if the user
depends on the
executionAsyncId()
to match theasyncId
in the before hook.For example, in
trace
I at some point used:However, because of
runInAsyncIdScope
this is actually invalid code.Solution
runInAsyncIdScope
AsyncResource
andemitBefore
/emitAfter
to changeexecutionAsyncId()
andtriggerAsyncId()
.Note: We may want to just deprecate the API in node 8 and remove it in a future version.
For example, the DB resource example should be implemented as:
/cc @nodejs/async_hooks
The text was updated successfully, but these errors were encountered: