2017-03-10 00:13:34 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
|
|
|
ERR_ASYNC_CALLBACK,
|
|
|
|
ERR_INVALID_ASYNC_ID
|
|
|
|
} = require('internal/errors').codes;
|
2018-08-03 00:51:02 +02:00
|
|
|
const { validateString } = require('internal/validators');
|
2017-11-12 18:46:55 +01:00
|
|
|
const internal_async_hooks = require('internal/async_hooks');
|
|
|
|
|
|
|
|
// Get functions
|
2017-11-09 22:57:04 +01:00
|
|
|
// For userland AsyncResources, make sure to emit a destroy event when the
|
|
|
|
// resource gets gced.
|
2018-03-15 05:54:10 +01:00
|
|
|
const { registerDestroyHook } = internal_async_hooks;
|
2017-11-12 18:46:55 +01:00
|
|
|
const {
|
2018-02-11 22:35:59 +01:00
|
|
|
executionAsyncId,
|
|
|
|
triggerAsyncId,
|
2017-11-12 18:46:55 +01:00
|
|
|
// Private API
|
|
|
|
getHookArrays,
|
|
|
|
enableHooks,
|
|
|
|
disableHooks,
|
2017-11-22 11:25:03 +01:00
|
|
|
// Internal Embedder API
|
2018-02-11 22:35:59 +01:00
|
|
|
newAsyncId,
|
2017-11-22 13:54:38 +01:00
|
|
|
getDefaultTriggerAsyncId,
|
2017-11-12 18:46:55 +01:00
|
|
|
emitInit,
|
|
|
|
emitBefore,
|
|
|
|
emitAfter,
|
|
|
|
emitDestroy,
|
|
|
|
} = internal_async_hooks;
|
2017-08-02 09:34:59 +02:00
|
|
|
|
2017-11-12 18:46:55 +01:00
|
|
|
// Get symbols
|
|
|
|
const {
|
2018-02-11 22:35:59 +01:00
|
|
|
async_id_symbol, trigger_async_id_symbol,
|
2017-11-12 18:46:55 +01:00
|
|
|
init_symbol, before_symbol, after_symbol, destroy_symbol,
|
|
|
|
promise_resolve_symbol
|
|
|
|
} = internal_async_hooks.symbols;
|
2017-03-10 00:13:34 +01:00
|
|
|
|
2017-11-12 18:46:55 +01:00
|
|
|
// Get constants
|
|
|
|
const {
|
|
|
|
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
|
2018-03-15 05:54:10 +01:00
|
|
|
} = internal_async_hooks.constants;
|
2017-03-10 00:13:34 +01:00
|
|
|
|
2017-11-12 18:46:55 +01:00
|
|
|
// Listener API //
|
2017-03-10 00:13:34 +01:00
|
|
|
|
|
|
|
class AsyncHook {
|
2017-09-09 17:50:42 +02:00
|
|
|
constructor({ init, before, after, destroy, promiseResolve }) {
|
2017-05-18 10:09:50 +02:00
|
|
|
if (init !== undefined && typeof init !== 'function')
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.init');
|
2017-05-18 10:09:50 +02:00
|
|
|
if (before !== undefined && typeof before !== 'function')
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.before');
|
2017-05-18 10:09:50 +02:00
|
|
|
if (after !== undefined && typeof after !== 'function')
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.after');
|
2017-05-18 10:09:50 +02:00
|
|
|
if (destroy !== undefined && typeof destroy !== 'function')
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.destroy');
|
2017-09-09 17:50:42 +02:00
|
|
|
if (promiseResolve !== undefined && typeof promiseResolve !== 'function')
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_ASYNC_CALLBACK('hook.promiseResolve');
|
2017-03-10 00:13:34 +01:00
|
|
|
|
|
|
|
this[init_symbol] = init;
|
|
|
|
this[before_symbol] = before;
|
|
|
|
this[after_symbol] = after;
|
|
|
|
this[destroy_symbol] = destroy;
|
2017-09-09 17:50:42 +02:00
|
|
|
this[promise_resolve_symbol] = promiseResolve;
|
2017-03-10 00:13:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
|
|
|
// The set of callbacks for a hook should be the same regardless of whether
|
|
|
|
// enable()/disable() are run during their execution. The following
|
|
|
|
// references are reassigned to the tmp arrays if a hook is currently being
|
|
|
|
// processed.
|
|
|
|
const [hooks_array, hook_fields] = getHookArrays();
|
|
|
|
|
|
|
|
// Each hook is only allowed to be added once.
|
|
|
|
if (hooks_array.includes(this))
|
2017-06-02 23:49:55 +02:00
|
|
|
return this;
|
2017-03-10 00:13:34 +01:00
|
|
|
|
2017-06-06 18:57:10 +02:00
|
|
|
const prev_kTotals = hook_fields[kTotals];
|
|
|
|
|
2017-03-10 00:13:34 +01:00
|
|
|
// createHook() has already enforced that the callbacks are all functions,
|
|
|
|
// so here simply increment the count of whether each callbacks exists or
|
|
|
|
// not.
|
2018-03-07 19:59:23 +01:00
|
|
|
hook_fields[kTotals] = hook_fields[kInit] += +!!this[init_symbol];
|
2017-06-06 18:57:10 +02:00
|
|
|
hook_fields[kTotals] += hook_fields[kBefore] += +!!this[before_symbol];
|
|
|
|
hook_fields[kTotals] += hook_fields[kAfter] += +!!this[after_symbol];
|
|
|
|
hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol];
|
2017-09-09 17:50:42 +02:00
|
|
|
hook_fields[kTotals] +=
|
|
|
|
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
|
2017-03-10 00:13:34 +01:00
|
|
|
hooks_array.push(this);
|
2017-06-06 18:57:10 +02:00
|
|
|
|
2017-10-19 12:43:40 +02:00
|
|
|
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
|
2017-11-12 18:46:55 +01:00
|
|
|
enableHooks();
|
2017-10-19 12:43:40 +02:00
|
|
|
}
|
2017-06-06 18:57:10 +02:00
|
|
|
|
2017-03-10 00:13:34 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
const [hooks_array, hook_fields] = getHookArrays();
|
|
|
|
|
|
|
|
const index = hooks_array.indexOf(this);
|
|
|
|
if (index === -1)
|
2017-06-02 23:49:55 +02:00
|
|
|
return this;
|
2017-03-10 00:13:34 +01:00
|
|
|
|
2017-06-06 18:57:10 +02:00
|
|
|
const prev_kTotals = hook_fields[kTotals];
|
|
|
|
|
2018-03-07 19:59:23 +01:00
|
|
|
hook_fields[kTotals] = hook_fields[kInit] -= +!!this[init_symbol];
|
2017-06-06 18:57:10 +02:00
|
|
|
hook_fields[kTotals] += hook_fields[kBefore] -= +!!this[before_symbol];
|
|
|
|
hook_fields[kTotals] += hook_fields[kAfter] -= +!!this[after_symbol];
|
|
|
|
hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol];
|
2017-09-09 17:50:42 +02:00
|
|
|
hook_fields[kTotals] +=
|
|
|
|
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
|
2017-03-10 00:13:34 +01:00
|
|
|
hooks_array.splice(index, 1);
|
2017-06-06 18:57:10 +02:00
|
|
|
|
2017-10-19 12:43:40 +02:00
|
|
|
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
|
2017-11-12 18:46:55 +01:00
|
|
|
disableHooks();
|
2017-10-19 12:43:40 +02:00
|
|
|
}
|
2017-06-06 18:57:10 +02:00
|
|
|
|
2017-03-10 00:13:34 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createHook(fns) {
|
|
|
|
return new AsyncHook(fns);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Embedder API //
|
|
|
|
|
2017-11-09 22:57:04 +01:00
|
|
|
const destroyedSymbol = Symbol('destroyed');
|
|
|
|
|
2017-05-24 13:15:05 +02:00
|
|
|
class AsyncResource {
|
2017-11-09 22:57:04 +01:00
|
|
|
constructor(type, opts = {}) {
|
2018-08-03 00:51:02 +02:00
|
|
|
validateString(type, 'type');
|
2017-08-30 23:45:21 +02:00
|
|
|
|
2017-11-09 22:57:04 +01:00
|
|
|
if (typeof opts === 'number') {
|
|
|
|
opts = { triggerAsyncId: opts, requireManualDestroy: false };
|
|
|
|
} else if (opts.triggerAsyncId === undefined) {
|
2017-11-22 13:54:38 +01:00
|
|
|
opts.triggerAsyncId = getDefaultTriggerAsyncId();
|
2017-11-09 22:57:04 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 14:28:33 +02:00
|
|
|
// Unlike emitInitScript, AsyncResource doesn't supports null as the
|
|
|
|
// triggerAsyncId.
|
2017-11-09 22:57:04 +01:00
|
|
|
const triggerAsyncId = opts.triggerAsyncId;
|
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-03 22:43:41 +02:00
|
|
|
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId);
|
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-03 22:43:41 +02:00
|
|
|
}
|
2017-03-10 00:13:34 +01:00
|
|
|
|
2018-02-11 22:35:59 +01:00
|
|
|
this[async_id_symbol] = newAsyncId();
|
2017-09-26 15:50:10 +02:00
|
|
|
this[trigger_async_id_symbol] = triggerAsyncId;
|
2018-12-10 13:27:32 +01:00
|
|
|
// This prop name (destroyed) has to be synchronized with C++
|
2017-11-09 22:57:04 +01:00
|
|
|
this[destroyedSymbol] = { destroyed: false };
|
2017-06-05 09:35:06 +02:00
|
|
|
|
2017-11-12 18:46:55 +01:00
|
|
|
emitInit(
|
2017-09-26 15:50:10 +02:00
|
|
|
this[async_id_symbol], type, this[trigger_async_id_symbol], this
|
|
|
|
);
|
2017-11-09 22:57:04 +01:00
|
|
|
|
|
|
|
if (!opts.requireManualDestroy) {
|
|
|
|
registerDestroyHook(this, this[async_id_symbol], this[destroyedSymbol]);
|
|
|
|
}
|
2017-03-10 00:13:34 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 00:25:41 +01:00
|
|
|
runInAsyncScope(fn, thisArg, ...args) {
|
|
|
|
emitBefore(this[async_id_symbol], this[trigger_async_id_symbol]);
|
|
|
|
let ret;
|
|
|
|
try {
|
|
|
|
ret = Reflect.apply(fn, thisArg, args);
|
|
|
|
} finally {
|
|
|
|
emitAfter(this[async_id_symbol]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:13:34 +01:00
|
|
|
emitDestroy() {
|
2017-11-09 22:57:04 +01:00
|
|
|
this[destroyedSymbol].destroyed = true;
|
2017-11-12 18:46:55 +01:00
|
|
|
emitDestroy(this[async_id_symbol]);
|
2017-03-10 00:13:34 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncId() {
|
|
|
|
return this[async_id_symbol];
|
|
|
|
}
|
|
|
|
|
2017-06-14 12:39:53 +02:00
|
|
|
triggerAsyncId() {
|
2017-09-26 15:50:10 +02:00
|
|
|
return this[trigger_async_id_symbol];
|
2017-03-10 00:13:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Placing all exports down here because the exported classes won't export
|
|
|
|
// otherwise.
|
|
|
|
module.exports = {
|
|
|
|
// Public API
|
|
|
|
createHook,
|
2017-06-14 12:39:53 +02:00
|
|
|
executionAsyncId,
|
|
|
|
triggerAsyncId,
|
2017-03-10 00:13:34 +01:00
|
|
|
// Embedder API
|
2017-05-24 13:15:05 +02:00
|
|
|
AsyncResource,
|
2017-03-10 00:13:34 +01:00
|
|
|
};
|