Repository navigation
Remove async_hooks runInAsyncIdScope API #14328
Description
Activity
- addedasync_hooksIssues and PRs related to the async hooks subsystem.Issues and PRs related to the async hooks subsystem.discussIssues opened for discussion and feedback.Issues opened for discussion and feedback.
on Jul 17, 2017 I'd like to voice an issue I encountered when trying to use
AsyncResource. Because it is a ES6 class, it is impossible for me to create a class that extends bothAsyncResourceand, say,EventEmitter. Is there a way to resolve this issue?@TimothyGu Short answer is that you can use
newUid,emitInit,emitBefore,emitAfterandemitDestroy. I will address it in a future issue aboutemitInitand friends.@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.
runInAsyncIdScope()is a bad API. It's should actually beinitTriggerIdScope()(i.e. it should dictate thetriggerIdpassed toinit(), nothing more). The pre-PR implementation did this, but I must have overlooked the functionality difference along the way. In short, this API shouldn't have existed in the first place.So,
setInitTriggerId()andrunInAsyncIdScope()don't address the same problem. Let's remove this API completely, and continue discussion about the need forsetInitTriggerId()in #14238.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
Reacted by Andreas MadsenI don't want to derail the original conversation of course, and I'd be happy to move this into a new issue.
@TimothyGu Please do.
@TimothyGu Please add the link to the new issue here when it's created.
- added a commit that references this issue
on Jan 8, 2018 - added a commit that references this issue
on Jan 19, 2018
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:
setInitTriggerIdandtriggerIdScope. (Remove async_hooks setTrigger API #14238)runInAsyncIdScope. (this)newUid,emitInit,emitBefore,emitAfterandemitDestroy(Remove async_hooks Sensitive/low-level Embedder API #15572).Note: There is some overlap between
emitInitandsetInitTriggerIdin terms ofinitTriggerId. Hopefully, that won't be an issue.Background
runInAsyncIdScope(asyncId, cb)creates a new scope with theasyncIdasthe
executionAsyncIdand with the currentexecutionAsyncIdasthe
triggerAsyncId. It does so without invoking the before and after hooks.runInAsyncIdScopewas not part of the originalasync_hooksEP butwas included in the
async_hooksPR.runInAsyncIdScopeis 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
emitBeforeandemitAfterare not used, thusthe before and after hooks are never invoked. This can be an issue if the user
depends on the
executionAsyncId()to match theasyncIdin the before hook.For example, in
traceI at some point used:However, because of
runInAsyncIdScopethis is actually invalid code.Solution
runInAsyncIdScopeAsyncResourceandemitBefore/emitAfterto 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