|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const tick = require('./tick');
|
|
|
|
const async_hooks = require('async_hooks');
|
|
|
|
const { AsyncResource } = async_hooks;
|
|
|
|
|
|
|
|
const initHooks = require('./init-hooks');
|
|
|
|
const { checkInvocations } = require('./hook-checks');
|
|
|
|
|
|
|
|
const hooks = initHooks();
|
|
|
|
hooks.enable();
|
|
|
|
|
|
|
|
common.expectsError(
|
|
|
|
() => new AsyncResource(), {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
|
|
type: TypeError,
|
|
|
|
});
|
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>
8 years ago
|
|
|
assert.throws(() => {
|
|
|
|
new AsyncResource('invalid_trigger_id', null);
|
|
|
|
}, common.expectsError({
|
|
|
|
code: 'ERR_INVALID_ASYNC_ID',
|
|
|
|
type: RangeError,
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
new AsyncResource('default_trigger_id').triggerAsyncId(),
|
|
|
|
async_hooks.executionAsyncId()
|
|
|
|
);
|
|
|
|
|
|
|
|
// create first custom event 'alcazares' with triggerAsyncId derived
|
|
|
|
// from async_hooks executionAsyncId
|
|
|
|
const alcaTriggerId = async_hooks.executionAsyncId();
|
|
|
|
const alcaEvent = new AsyncResource('alcazares', alcaTriggerId);
|
|
|
|
const alcazaresActivities = hooks.activitiesOfTypes([ 'alcazares' ]);
|
|
|
|
|
|
|
|
// alcazares event was constructed and thus only has an `init` call
|
|
|
|
assert.strictEqual(alcazaresActivities.length, 1);
|
|
|
|
const alcazares = alcazaresActivities[0];
|
|
|
|
assert.strictEqual(alcazares.type, 'alcazares');
|
|
|
|
assert.strictEqual(typeof alcazares.uid, 'number');
|
|
|
|
assert.strictEqual(alcazares.triggerAsyncId, alcaTriggerId);
|
|
|
|
checkInvocations(alcazares, { init: 1 }, 'alcazares constructed');
|
|
|
|
|
|
|
|
assert.strictEqual(typeof alcaEvent.asyncId(), 'number');
|
|
|
|
assert.notStrictEqual(alcaEvent.asyncId(), alcaTriggerId);
|
|
|
|
assert.strictEqual(alcaEvent.triggerAsyncId(), alcaTriggerId);
|
|
|
|
|
|
|
|
alcaEvent.emitBefore();
|
|
|
|
checkInvocations(alcazares, { init: 1, before: 1 },
|
|
|
|
'alcazares emitted before');
|
|
|
|
alcaEvent.emitAfter();
|
|
|
|
checkInvocations(alcazares, { init: 1, before: 1, after: 1 },
|
|
|
|
'alcazares emitted after');
|
|
|
|
alcaEvent.emitBefore();
|
|
|
|
checkInvocations(alcazares, { init: 1, before: 2, after: 1 },
|
|
|
|
'alcazares emitted before again');
|
|
|
|
alcaEvent.emitAfter();
|
|
|
|
checkInvocations(alcazares, { init: 1, before: 2, after: 2 },
|
|
|
|
'alcazares emitted after again');
|
|
|
|
alcaEvent.emitDestroy();
|
|
|
|
tick(1, common.mustCall(tick1));
|
|
|
|
|
|
|
|
function tick1() {
|
|
|
|
checkInvocations(alcazares, { init: 1, before: 2, after: 2, destroy: 1 },
|
|
|
|
'alcazares emitted destroy');
|
|
|
|
|
|
|
|
// The below shows that we can pass any number as a trigger id
|
|
|
|
const pobTriggerId = 111;
|
|
|
|
const pobEvent = new AsyncResource('poblado', pobTriggerId);
|
|
|
|
const pobladoActivities = hooks.activitiesOfTypes([ 'poblado' ]);
|
|
|
|
const poblado = pobladoActivities[0];
|
|
|
|
assert.strictEqual(poblado.type, 'poblado');
|
|
|
|
assert.strictEqual(typeof poblado.uid, 'number');
|
|
|
|
assert.strictEqual(poblado.triggerAsyncId, pobTriggerId);
|
|
|
|
checkInvocations(poblado, { init: 1 }, 'poblado constructed');
|
|
|
|
pobEvent.emitBefore();
|
|
|
|
checkInvocations(poblado, { init: 1, before: 1 },
|
|
|
|
'poblado emitted before');
|
|
|
|
|
|
|
|
pobEvent.emitAfter();
|
|
|
|
checkInvocations(poblado, { init: 1, before: 1, after: 1 },
|
|
|
|
'poblado emitted after');
|
|
|
|
|
|
|
|
// after we disable the hooks we shouldn't receive any events anymore
|
|
|
|
hooks.disable();
|
|
|
|
alcaEvent.emitDestroy();
|
|
|
|
tick(1, common.mustCall(tick2));
|
|
|
|
|
|
|
|
function tick2() {
|
|
|
|
checkInvocations(
|
|
|
|
alcazares, { init: 1, before: 2, after: 2, destroy: 1 },
|
|
|
|
'alcazares emitted destroy a second time after hooks disabled');
|
|
|
|
pobEvent.emitDestroy();
|
|
|
|
tick(1, common.mustCall(tick3));
|
|
|
|
}
|
|
|
|
|
|
|
|
function tick3() {
|
|
|
|
checkInvocations(poblado, { init: 1, before: 1, after: 1 },
|
|
|
|
'poblado emitted destroy after hooks disabled');
|
|
|
|
}
|
|
|
|
}
|