Commit Graph

57 Commits

Author SHA1 Message Date
Andrey Pechkurov
6510a741c4 async_hooks: add store arg in AsyncLocalStorage
This commit introduces store as the first argument in
AsyncLocalStorage's run methods. The change is motivated by the
following expectation: most users are going to use a custom object
as the store and an extra Map created by the previous implementation
is an overhead for their use case.

Important note. This is a backwards incompatible change.
It was discussed and agreed an incompatible change is ok
since the API is still experimental and the modified
methods were only added within the last week so usage
will be minimal to none.

PR-URL: https://github.com/nodejs/node/pull/31930
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2020-02-27 15:00:23 -05:00
vdeturckheim
9c702922cd
async_hooks: introduce async-context API
Adding AsyncLocalStorage class to async_hooks
 module.
This API provide a simple CLS-like set
of features.

Co-authored-by: Andrey Pechkurov <apechkurov@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/26540
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2020-02-23 20:24:02 +01:00
legendecas
75311dbc2f
async_hooks: ensure event after been emitted on runInAsyncScope
The exception handler user-defined will not automatically emit after
for the async resource.

Also removes a duplicated case
`test-emit-after-uncaught-exception-runInAsyncScope.js`
which is identical to test-emit-after-uncaught-exception.js.

Refs: https://github.com/nodejs/node/pull/30965
PR-URL: https://github.com/nodejs/node/pull/31784
Fixes: https://github.com/nodejs/node/issues/31783
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2020-02-14 16:55:02 +01:00
Matteo Collina
9fdb6e6aaf
async_hooks: add executionAsyncResource
Remove the need for the destroy hook in the basic APM case.

Co-authored-by: Stephen Belanger <admin@stephenbelanger.com>
PR-URL: https://github.com/nodejs/node/pull/30959
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-02-11 20:59:09 +01:00
Anatoli Papirovski
4de31d517f async_hooks: remove internal only error checking
This error checking is mostly unnecessary and is just a Node core
developer nicety, rather than something that is needed for the
user-land. It can be safely removed without any practical
impact while making nextTick, timers, immediates and AsyncResource
substantially faster.

PR-URL: https://github.com/nodejs/node/pull/30967
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2020-01-11 19:20:45 -08:00
Anatoli Papirovski
503900b463 async_hooks: ensure proper handling in runInAsyncScope
We should never try to manually run emitAfter in case of an error,
the exception handler will do it for us, if we're going to recover.

PR-URL: https://github.com/nodejs/node/pull/30965
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-12-16 21:59:50 -08:00
Sebastien Ahkrin
eac3f0adc4
lib: replace Symbol global by the primordials Symbol
PR-URL: https://github.com/nodejs/node/pull/30737
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2019-12-08 13:38:58 +01:00
Michaël Zasso
1f9a5ae7aa
lib: use static Number properties from primordials
PR-URL: https://github.com/nodejs/node/pull/30686
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2019-11-30 13:45:38 +01:00
Michaël Zasso
0646eda4fc
lib: flatten access to primordials
Store all primordials as properties of the primordials object.
Static functions are prefixed by the constructor's name and prototype
methods are prefixed by the constructor's name followed by "Prototype".
For example: primordials.Object.keys becomes primordials.ObjectKeys.

PR-URL: https://github.com/nodejs/node/pull/30610
Refs: https://github.com/nodejs/node/issues/29766
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-11-25 10:28:15 +01:00
Anna Henningsen
6858c7e3e7
async_hooks: only emit after for AsyncResource if stack not empty
We clear the async id stack inside the uncaught exception handler and
emit `after` events in the process, so we should not emit `after`
a second time from the `runInAsyncScope()` code.

This should match the behaviour we have in C++.

Fixes: https://github.com/nodejs/node/issues/30080

PR-URL: https://github.com/nodejs/node/pull/30087
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
2019-10-26 15:19:23 +02:00
Anatoli Papirovski
cde3928a10 process: improve queueMicrotask performance
Optimize the hot code paths of queueMicrotask by
not creating unnecessary objects, not looking up
properties on frozen primordials, etc.

PR-URL: https://github.com/nodejs/node/pull/28093
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2019-06-10 07:49:40 +02:00
Anatoli Papirovski
d3d4e10107 async_hooks: improve AsyncResource performance
Accessing symbols is generally quite expensive and so is emitInit,
only do both when actually required.

PR-URL: https://github.com/nodejs/node/pull/27032
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-04-05 05:37:01 +02:00
Michaël Zasso
0817840f77 lib: force using primordials for JSON, Math and Reflect
Use the "no-restricted-globals" ESLint rule to lint for it.

PR-URL: https://github.com/nodejs/node/pull/27027
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2019-04-03 21:36:08 +08:00
Matteo Collina
9d064439e5 async_hooks: remove deprecated emitBefore and emitAfter
AsyncResource.emitBefore and AsyncResource.emitAfter have been
deprecated in https://github.com/nodejs/node/pull/18632. This PR removes
it all.
This commit also updates some embedder tests to use internal APIs.
The conditions are still possible for Node.js core developers but not
for end users.

PR-URL: https://github.com/nodejs/node/pull/26530
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
Reviewed-By: Yang Guo <yangguo@chromium.org>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-03-18 11:36:48 +01:00
Ruben Bridgewater
50dd555910
doc,lib,test: capitalize comment sentences
This activates the eslint capitalize comment rule for comments
above 50 characters.

PR-URL: https://github.com/nodejs/node/pull/24996
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-12-17 17:14:35 +01:00
Jon Moss
e570ae79f5 lib: extract validateString validator
Pulls out a common argument validator to `internal/validators`

PR-URL: https://github.com/nodejs/node/pull/22101
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-08-07 10:51:27 -04:00
Ruben Bridgewater
c6b6c92185
lib: always show ERR_INVALID_ARG_TYPE received part
This makes a effort to make sure all of these errors will actually
also show the received input.
On top of that it refactors a few tests for better maintainability.
It will also change the returned type to always be a simple typeof
instead of special handling null.

PR-URL: https://github.com/nodejs/node/pull/19445
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-03-25 01:45:37 +01:00
Daniel Bevenius
8c46fa6903 async_hooks: remove async_wrap from async_hooks.js
This commit removes the builtin async_wrap module from
lib/async_hooks.js.

The motivation for this is that lib/async_hooks.js requires
lib/internal/async_hooks which also binds async_wrap. Instead of
lib/async_hooks.js also binding async_wrap it now only has to require
the internal async_hooks and access it's exports.

There might be a very good reason for doing it the current way but the
reason is not obvious to me. Hopefully someone can shed some light on
this.

PR-URL: https://github.com/nodejs/node/pull/19368
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2018-03-18 12:08:38 +01:00
Daniel Bevenius
ddcc00bc1e async_hooks: don't set hook_fields[kTotals] to 0
This commit removes the setting of hook_field[kTotals] to szero in
AsyncHook's enable function.

As far as I can tell this would not be required if the setting of
this field is done with the assignment operator instead of using the
addition assignment operator.

PR-URL: https://github.com/nodejs/node/pull/19219
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-03-11 16:33:43 +01:00
Michaël Zasso
1e8d110e64 lib: port errors to new system
This is a first batch of updates that touches non-underscored modules in
lib.

PR-URL: https://github.com/nodejs/node/pull/19034
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2018-03-05 19:51:30 +01:00
Daniel Bevenius
f2defcac4d src: fix error message in async_hooks constructor
There are two minor issues in the AsyncHook constructor, if the object
passed in has an after and/or destroy property that are not functions
the errors thrown will still be:
TypeError [ERR_ASYNC_CALLBACK]: before must be a function

This commit updates the code and adds a unit test.

PR-URL: https://github.com/nodejs/node/pull/19000
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matheus Marchini <matheus@sthima.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
2018-02-28 07:31:02 +01:00
Anatoli Papirovski
e9ac80bb39
async_hooks: clean up usage in internal code
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: https://github.com/nodejs/node/pull/18720
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-02-16 14:23:14 -05:00
Ruben Bridgewater
ba944b1697
lib: provide proper deprecation code
Refs: https://github.com/nodejs/node/pull/18513

PR-URL: https://github.com/nodejs/node/pull/18694
Refs: https://github.com/nodejs/node/pull/18513
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2018-02-12 13:59:09 +01:00
Ali Ijaz Sheikh
523a1550a3 async_hooks: deprecate unsafe emit{Before,After}
The emit{Before,After} APIs in AsyncResource are problematic.

* emit{Before,After} are named to suggest that the only thing they do
  is emit the before and after hooks. However, they in fact, mutate
  the current execution context.
* They must be properly nested. Failure to do so by user code leads
  to catastrophic (unrecoverable) exceptions. It is very easy for the
  users to forget that they must be using a try/finally block around
  the code that must be surrounded by these operations. Even the
  example provided in the official docs makes this mistake. Failing
  to use a finally can lead to a catastrophic crash if the callback
  ends up throwing.

This change provides a safer `runInAsyncScope` API as an alternative
and deprecates emit{Before,After}.

PR-URL: https://github.com/nodejs/node/pull/18513
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
2018-02-09 13:03:34 -08:00
Andreas Madsen
f3f1a9349a
async_hooks: rename initTriggerId
rename initTriggerId to defaultTriggerAsyncId such it matches the rest
of our naming.

PR-URL: https://github.com/nodejs/node/pull/17273
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-12-19 18:04:43 +01:00
Andreas Madsen
1cc6b993b9
async_hooks: remove deprecated API
PR-URL: https://github.com/nodejs/node/pull/17147
Refs: https://github.com/nodejs/node/pull/16972
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-11-22 11:25:03 +01:00
Andreas Madsen
07a4fa3dbe
async_hooks: deprecate undocumented API
PR-URL: https://github.com/nodejs/node/pull/16972
Refs: https://github.com/nodejs/node/issues/14328
Refs: https://github.com/nodejs/node/issues/15572
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-11-20 12:03:06 +01:00
Sebastian Mayr
22901d81c9
async_hooks: add destroy event for gced AsyncResources
In cases where libraries create AsyncResources which may be emitting
more events depending on usage, the only way to ensure that destroy is
called properly is by calling it when the resource gets garbage
collected.

Fixes: https://github.com/nodejs/node/issues/16153
PR-URL: https://github.com/nodejs/node/pull/16998
Fixes: https://github.com/nodejs/node/issues/16153
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-11-16 19:58:00 +01:00
Andreas Madsen
7c079d1261
async_hooks: skip runtime checks when disabled
PR-URL: https://github.com/nodejs/node/pull/15454
Ref: https://github.com/nodejs/node/pull/14387
Ref: https://github.com/nodejs/node/pull/14722
Ref: https://github.com/nodejs/node/issues/14717
Ref: https://github.com/nodejs/node/issues/15448
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-10-19 12:45:21 +02:00
Brian White
7cff6e80bf
async_hooks: fix reference in code comment
PR-URL: https://github.com/nodejs/node/pull/15748
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
2017-10-03 06:24:54 -07:00
Andreas Madsen
3a69ef55e0
async_hooks: consistent internal naming
PR-URL: https://github.com/nodejs/node/pull/15569
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-09-26 15:50:10 +02:00
Anna Henningsen
b605b15346
async_hooks: support promise resolve hook
Add a `promiseResolve()` hook.

PR-URL: https://github.com/nodejs/node/pull/15296
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-09-23 15:52:03 +02:00
James M Snell
d8a0364ad5 async_hooks,doc: some async_hooks improvements
Update docs and type checking for AsyncResource type

PR-URL: https://github.com/nodejs/node/pull/15103
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2017-09-15 09:06:13 -07:00
Trevor Norris
062beb08df
async_hooks: don't abort unnecessarily
* id values of -1 are allowed. They indicate that the id was never
  correctly assigned to the async resource. These will appear in any
  call graph, and will only be apparent to those using the async_hooks
  module, then reported in an issue.
* ids < -1 are still not allowed and will cause the application to
  exit the process; because there is no scenario where this should ever
  happen.
* Add asyncId range checks to emitAfterScript().
* Fix emitBeforeScript() range checks which should have been || not &&.
* Replace errors with entries in internal/errors.
* Fix async_hooks tests that check for exceptions to match new
  internal/errors entries.

NOTE: emit{Before,After,Destroy}() must continue to exit the process
because in the case of an exception during hook execution the state of
the application is unknowable. For example, an exception could cause a
memory leak:

    const id_map = new Map();

    before(id) {
      id_map.set(id, /* data object or similar */);
    },
    after(id) {
      throw new Error('id never dies!');
      id_map.delete(id);
    }

Allowing a recoverable exception may also cause an abort because of a
stack check in Environment::AsyncHooks::pop_ids() that verifies the
async id and pop'd ids match. This case would be more difficult to debug
than if fatalError() (lib/async_hooks.js) was called immediately.

    try {
      async_hooks.emitBefore(null, NaN);
    } catch (e) { }
    // do something
    async_hooks.emitAfter(5);

It also allows an edge case where emitBefore() could be called twice and
not have the pop_ids() CHECK fail:

    try {
      async_hooks.emitBefore(5, NaN);
    } catch (e) { }
    async_hooks.emitBefore(5);
    // do something
    async_hooks.emitAfter(5);

There is the option of allowing mismatches in the stack and ignoring the
check if no async hooks are enabled, but I don't believe going this far
is necessary.

PR-URL: https://github.com/nodejs/node/pull/14722
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-08-14 23:21:01 +02:00
Trevor Norris
94369d8017
async_hooks: improve comments and function names
* Reword several of the comments to be more descriptive.
* Rename functions to better indicate what they are doing.
* Change AsyncHooks::uid_fields_ to be a fixed array instead of a
  pointer.
* Define regex early so line ends before 80 columns.
* Remove obsolete comments.
* Rename AsyncHooks::InitScope::uid_fields_ to uid_fields_ptr_ because
  using the same name as AsyncHooks::uid_fields_ was confusing.
* Place variables that are used to store the new set of hooks if another
  hook is enabled/disabled during hook execution into an object to act
  as a namespace.

PR-URL: https://github.com/nodejs/node/pull/14722
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-08-14 23:20:59 +02:00
Anna Henningsen
d731369b1d
async_hooks: remove deprecated APIs
These APIs were introduced during the lifetime of Node 8 in an
experimental API and should safely be removable in Node 9+.

PR-URL: https://github.com/nodejs/node/pull/14414
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2017-07-27 20:02:01 +02:00
Rich Trott
50f45fffc1 lib: modify destructuring for indentation
ESLint 4.3.0 flags an extra indentation issue that affects two of our
destructuring assignments in `lib`. This is a whitespace-only update to
comply prior to updating.

PR-URL: https://github.com/nodejs/node/pull/14417
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-07-25 12:21:26 -07:00
Andreas Madsen
31417b6882
async_hooks: make AsyncResource match emitInit
AsyncResource previously called emitInitNative. Since AsyncResource is
just an abstraction on top of the emitEventScript functions, it should
call emitInitScript instead.

PR-URL: https://github.com/nodejs/node/pull/14152
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-07-13 14:15:06 +02:00
Andreas Madsen
628485ea01
async_hooks: rename internal emit functions
There are two categories of emit functions in async_hooks, those used by
c++ (native) and those used by JavaScript (script). Previously these
were named N for native and S for script. Finally, there was an odd case
where emitInitN was called just init. This makes it more explicit by
using the names emitInitNative and emitInitScript. The other emit
functions are also renamed.

PR-URL: https://github.com/nodejs/node/pull/14152
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-07-13 14:13:22 +02:00
Andreas Madsen
f94fd0c0f3
async_hooks: fix nested hooks mutation
In some cases restoreTmpHooks is called too early, this causes
active_hooks_array to change during execution of the init hooks.

PR-URL: https://github.com/nodejs/node/pull/14143
Ref: https://github.com/nodejs/node/pull/14054#issuecomment-313915193
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-07-13 11:59:18 +02:00
Ruben Bridgewater
8a830350b2
async_hooks: move restoreTmpHooks call to init
This fixes an error that could occure by nesting async_hooks calls

PR-URL: https://github.com/nodejs/node/pull/14054
Ref: https://github.com/nodejs/node/pull/13755#issuecomment-312616004
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
2017-07-13 11:57:44 +02:00
Rich Trott
0d22858d67 lib: remove excess indentation
In anticipation of stricter linting for indentation, remove instances of
extra indentation that will be flagged by the new rules.

PR-URL: https://github.com/nodejs/node/pull/14090
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2017-07-07 13:18:19 -07:00
Andreas Madsen
84f9261efd
async_hooks: require parameter in emitBefore
Using asyncId as the default triggerAsyncId is wrong. The triggerAsyncId
can actually never be the asyncId.

PR-URL: https://github.com/nodejs/node/pull/14050
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-07-06 23:51:15 +02:00
Andreas Madsen
4e27aa9603
async_hooks: use common emitBefore and emitAfter
Timers and nextTick have special emitBefore and emitAfter functions for
historic reasons. These function are not needed any more, thus the
public emitBefore and emitAfter function can be used.

PR-URL: https://github.com/nodejs/node/pull/14050
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-07-06 23:50:08 +02:00
Andreas Madsen
0fd4c73e5c
async_hooks: fix default nextTick triggerAsyncId
In the case where triggerAsyncId is null it should default to the
current executionAsyncId. This worked but as a side-effect the resource
object was changed too.

This fix also makes the null check more strict. EmitInitS is not a
documented API, thus there is no reason to be flexible in its input.

Ref: https://github.com/nodejs/node/issues/13548#issuecomment-310985270
PR-URL: https://github.com/nodejs/node/pull/14026
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-07-05 15:51:16 +02:00
Ruben Bridgewater
5c6c02996b
async_hooks: reduce duplication with factory
PR-URL: https://github.com/nodejs/node/pull/13755
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
2017-07-05 15:50:59 +02:00
Andreas Madsen
de762b71f2
async_hooks: rename currentId and triggerId
currentId is renamed to executionAsyncId
triggerId is renamed to triggerAsyncId
AsyncResource.triggerId is renamed to AsyncResource.triggerAsyncId
AsyncHooksGetCurrentId is renamed to AsyncHooksGetExecutionAsyncId
AsyncHooksGetTriggerId is renamed to AsyncHooksGetTriggerAsyncId

PR-URL: https://github.com/nodejs/node/pull/13490
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2017-06-14 12:39:53 +02:00
Trevor Norris
a2fdb76677
async_wrap: use kTotals to enable PromiseHook
Keep a total of enabled hook callbacks in kTotals. This value is used to
track whether node::PromiseHook (src/async-wrap.cc) should be enabled or
disabled.

Don't enable node::PromiseHook, using enablePromiseHook(), until a hook
has been added. Then, using disablePromiseHook(), disable
node::PromiseHook when all hooks have been disabled.

Need to use a native test in order to check the internal field of the
Promise and check for a PromiseWrap.

PR-URL: https://github.com/nodejs/node/pull/13509
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-06-10 11:38:05 +02:00
Trevor Norris
b9379095e1
Revert "async_hooks: only set up hooks if used"
This reverts commit 410b141764.

PR-URL: https://github.com/nodejs/node/pull/13509
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-06-10 11:37:41 +02:00
Anna Henningsen
02aea0690d
async_hooks: minor refactor to callback invocation
Re-use the `init` function wherever possible, and move
`try { … } catch` blocks that result in fatal errors to a larger
scope.

Also make the argument order for `init()` consistent in the codebase.

PR-URL: https://github.com/nodejs/node/pull/13419
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-06-08 23:58:48 +02:00