Skip to content
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

async_hooks: clean up usage in internal code #18720

Closed
wants to merge 3 commits into from

Conversation

apapirovski
Copy link
Member

@apapirovski apapirovski commented Feb 11, 2018

Instead of exposing internals of async_hooks & async_wrap throughout
the code base, create necessary helper functions within the internal
async_hooks that allow easy usage by Node.js internals. This stops
every single internal user of async_hooks from importing a ton of
functions, constants and internal Aliased Buffers from C++ async_wrap.

Adds functions initHooksExist, afterHooksExist, and destroyHooksExist
to determine whether the related emit methods need to be triggered.

Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side
as an alternative to always calling C++.

Moves async_id_symbol and trigger_async_id_symbol to internal
async_hooks as they are never used in C++.

Adjusts usage throughout the codebase, as well as in a couple of tests.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

async_hooks

@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Feb 11, 2018
@apapirovski apapirovski added async_wrap async_hooks Issues and PRs related to the async hooks subsystem. labels Feb 11, 2018
@apapirovski
Copy link
Member Author

/cc @addaleax, @ofrobots, @AndreasMadsen — I would appreciate a review from someone involved with async hooks. Want to make sure I'm not missing anything, in particular with moving the symbols to internal/async_hooks (if they're meant to be quasi-public — as they were via process.binding — then they should probably be exported from async_hooks to be honest).

@ofrobots
Copy link
Contributor

ofrobots commented Feb 14, 2018 via email

@apapirovski
Copy link
Member Author

@ofrobots No worries :) Hope the summit was productive, disappointed I had to miss it.

Copy link
Contributor

@ofrobots ofrobots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM w/ nits. Thanks for doing all this cleanup!

return async_id_fields[kExecutionAsyncId];
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: executionAsyncId and triggerAsyncId should probably stay together. Either both move or neither.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. I'll move both. (I wanted to avoid requiring both files during bootstrap.)

@@ -345,6 +365,18 @@ function emitDestroyScript(asyncId) {
}


function clearAsyncIdStack() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: needs a comment that needs to stay in sync with the C++ copy in env-inl.h (and vice versa).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Will update!

@@ -270,6 +272,11 @@ function getDefaultTriggerAsyncId() {
}


function clearDefaultTriggerAsyncId() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It might be worth moving the copy of this operation in env-inl.h into a standalone create_default_trigger_async_id function so that we can attach a comment in both places to keep the copies in sync.

Aside: I've wanted to cleanup the magical -1 and 0 hard-coded constants for async id, but that should probably happen in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it makes sense to move it off into a function in C++ since it seems to only be done from the constructor. Perhaps a constant makes the most sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment that says that this should never be used except for exception handling?

@apapirovski
Copy link
Member Author

@ofrobots Updated now. Thanks for the feedback!

@apapirovski
Copy link
Member Author

@addaleax Would you mind reviewing this? Looking to land today but there's one commit that hasn't been reviewed. Thanks!

@apapirovski
Copy link
Member Author

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me :)

emitInit
} = require('internal/async_hooks');
// Grab the constants necessary for working with internal arrays.
const { kInit, kAsyncIdCounter } = async_wrap.constants;
// Symbols for storing async id state.
const async_id_symbol = Symbol('asyncId');
Copy link
Member

@AndreasMadsen AndreasMadsen Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have separate symbols for this? 😂 I think they should just be async_id_symbol and trigger_async_id_symbol from async_hooks.

Copy link
Member Author

@apapirovski apapirovski Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny enough, it's actually necessary because of enroll and unenroll. Since they could use an object that already has an asyncId associated, it would overwrite it or misrepresent it in emit.

init_symbol, before_symbol, after_symbol, destroy_symbol,
promise_resolve_symbol
},
enableHooks,
disableHooks,
clearDefaultTriggerAsyncId,
clearAsyncIdStack,
hasAsyncIdStack,
// Internal Embedder API
newUid,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You would be truly loved if you renamed this to newAsyncId :)

Instead of exposing internals of async_hooks & async_wrap throughout
the code base, create necessary helper methods within the internal
async_hooks that allows easy usage by Node.js internals. This stops
every single internal user of async_hooks from importing a ton of
functions, constants and internal Aliased Buffers from C++ async_wrap.

Adds functions initHooksExist, afterHooksExist, and destroyHooksExist
to determine whether the related emit methods need to be triggered.

Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side
as an alternative to always calling C++.

Moves async_id_symbol and trigger_async_id_symbol to internal
async_hooks as they are never used in C++.

Adjusts usage throughout the codebase, as well as in a couple of tests.
@apapirovski
Copy link
Member Author

apapirovski commented Feb 16, 2018

Lite CI before landing: https://ci.nodejs.org/job/node-test-pull-request-lite/175/

apapirovski added a commit that referenced this pull request Feb 16, 2018
Instead of exposing internals of async_hooks & async_wrap throughout
the code base, create necessary helper methods within the internal
async_hooks that allows easy usage by Node.js internals. This stops
every single internal user of async_hooks from importing a ton of
functions, constants and internal Aliased Buffers from C++ async_wrap.

Adds functions initHooksExist, afterHooksExist, and destroyHooksExist
to determine whether the related emit methods need to be triggered.

Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side
as an alternative to always calling C++.

Moves async_id_symbol and trigger_async_id_symbol to internal
async_hooks as they are never used in C++.

Renames newUid to newAsyncId for added clarity of its purpose.

Adjusts usage throughout the codebase, as well as in a couple of tests.

PR-URL: #18720
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@apapirovski
Copy link
Member Author

Landed in e9ac80b

@apapirovski apapirovski deleted the patch-async-should-fns branch February 16, 2018 20:03
@MylesBorins
Copy link
Contributor

MylesBorins commented Feb 21, 2018

Should this be backported to v9.x-staging? If yes please follow the guide and raise a backport PR, if not let me know or add the dont-land-on label.

needs to come with #18823

MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
Instead of exposing internals of async_hooks & async_wrap throughout
the code base, create necessary helper methods within the internal
async_hooks that allows easy usage by Node.js internals. This stops
every single internal user of async_hooks from importing a ton of
functions, constants and internal Aliased Buffers from C++ async_wrap.

Adds functions initHooksExist, afterHooksExist, and destroyHooksExist
to determine whether the related emit methods need to be triggered.

Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side
as an alternative to always calling C++.

Moves async_id_symbol and trigger_async_id_symbol to internal
async_hooks as they are never used in C++.

Renames newUid to newAsyncId for added clarity of its purpose.

Adjusts usage throughout the codebase, as well as in a couple of tests.

PR-URL: nodejs#18720
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@MylesBorins
Copy link
Contributor

Should this be backported to v8.x-staging? If yes please follow the guide and raise a backport PR, if not let me know or add the dont-land-on label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants