diff options
author | Andreas Madsen <amwebdk@gmail.com> | 2017-10-19 12:43:40 +0200 |
---|---|---|
committer | Andreas Madsen <amwebdk@gmail.com> | 2017-10-19 12:45:21 +0200 |
commit | 7c079d12612ddebc68a24eb3c55efecd8b8a6186 (patch) | |
tree | 99780ecfb4bbdad3a6ecc7685384eb3debca79d3 /lib/async_hooks.js | |
parent | f8ef2e2bf95dd65cb06a7f5e581e856c302f0793 (diff) | |
download | android-node-v8-7c079d12612ddebc68a24eb3c55efecd8b8a6186.tar.gz android-node-v8-7c079d12612ddebc68a24eb3c55efecd8b8a6186.tar.bz2 android-node-v8-7c079d12612ddebc68a24eb3c55efecd8b8a6186.zip |
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>
Diffstat (limited to 'lib/async_hooks.js')
-rw-r--r-- | lib/async_hooks.js | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/lib/async_hooks.js b/lib/async_hooks.js index acf4b101e6..64bac55d2c 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -60,7 +60,7 @@ const active_hooks = { // async execution. These are tracked so if the user didn't include callbacks // for a given step, that step can bail out early. const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve, - kExecutionAsyncId, kTriggerAsyncId, kAsyncIdCounter, + kCheck, kExecutionAsyncId, kTriggerAsyncId, kAsyncIdCounter, kInitTriggerAsyncId } = async_wrap.constants; // Symbols used to store the respective ids on both AsyncResource instances and @@ -156,8 +156,10 @@ class AsyncHook { hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol]; hooks_array.push(this); - if (prev_kTotals === 0 && hook_fields[kTotals] > 0) + if (prev_kTotals === 0 && hook_fields[kTotals] > 0) { enablePromiseHook(); + hook_fields[kCheck] += 1; + } return this; } @@ -180,8 +182,10 @@ class AsyncHook { hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol]; hooks_array.splice(index, 1); - if (prev_kTotals > 0 && hook_fields[kTotals] === 0) + if (prev_kTotals > 0 && hook_fields[kTotals] === 0) { disablePromiseHook(); + hook_fields[kCheck] -= 1; + } return this; } @@ -243,6 +247,15 @@ function triggerAsyncId() { return async_id_fields[kTriggerAsyncId]; } +function validateAsyncId(asyncId, type) { + // Skip validation when async_hooks is disabled + if (async_hook_fields[kCheck] <= 0) return; + + if (!Number.isSafeInteger(asyncId) || asyncId < -1) { + fatalError(new errors.RangeError('ERR_INVALID_ASYNC_ID', type, asyncId)); + } +} + // Embedder API // @@ -337,10 +350,16 @@ function setInitTriggerId(triggerAsyncId) { function emitInitScript(asyncId, type, triggerAsyncId, resource) { + validateAsyncId(asyncId, 'asyncId'); + if (triggerAsyncId !== null) + validateAsyncId(triggerAsyncId, 'triggerAsyncId'); + if (async_hook_fields[kCheck] > 0 && + (typeof type !== 'string' || type.length <= 0)) { + throw new errors.TypeError('ERR_ASYNC_TYPE', type); + } + // Short circuit all checks for the common case. Which is that no hooks have // been set. Do this to remove performance impact for embedders (and core). - // Even though it bypasses all the argument checks. The performance savings - // here is critical. if (async_hook_fields[kInit] === 0) return; @@ -354,18 +373,6 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) { async_id_fields[kInitTriggerAsyncId] = 0; } - if (!Number.isSafeInteger(asyncId) || asyncId < -1) { - throw new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId); - } - if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) { - throw new errors.RangeError('ERR_INVALID_ASYNC_ID', - 'triggerAsyncId', - triggerAsyncId); - } - if (typeof type !== 'string' || type.length <= 0) { - throw new errors.TypeError('ERR_ASYNC_TYPE', type); - } - emitInitNative(asyncId, type, triggerAsyncId, resource); } @@ -411,15 +418,8 @@ function emitBeforeScript(asyncId, triggerAsyncId) { // Validate the ids. An id of -1 means it was never set and is visible on the // call graph. An id < -1 should never happen in any circumstance. Throw // on user calls because async state should still be recoverable. - if (!Number.isSafeInteger(asyncId) || asyncId < -1) { - fatalError( - new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId)); - } - if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) { - fatalError(new errors.RangeError('ERR_INVALID_ASYNC_ID', - 'triggerAsyncId', - triggerAsyncId)); - } + validateAsyncId(asyncId, 'asyncId'); + validateAsyncId(triggerAsyncId, 'triggerAsyncId'); pushAsyncIds(asyncId, triggerAsyncId); @@ -429,10 +429,7 @@ function emitBeforeScript(asyncId, triggerAsyncId) { function emitAfterScript(asyncId) { - if (!Number.isSafeInteger(asyncId) || asyncId < -1) { - fatalError( - new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId)); - } + validateAsyncId(asyncId, 'asyncId'); if (async_hook_fields[kAfter] > 0) emitAfterNative(asyncId); @@ -442,10 +439,7 @@ function emitAfterScript(asyncId) { function emitDestroyScript(asyncId) { - if (!Number.isSafeInteger(asyncId) || asyncId < -1) { - fatalError( - new errors.RangeError('ERR_INVALID_ASYNC_ID', 'asyncId', asyncId)); - } + validateAsyncId(asyncId, 'asyncId'); // Return early if there are no destroy callbacks, or invalid asyncId. if (async_hook_fields[kDestroy] === 0 || asyncId <= 0) |