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>
This commit is contained in:
Trevor Norris 2017-08-03 14:43:41 -06:00 committed by Anna Henningsen
parent d441c7ffe8
commit 062beb08df
No known key found for this signature in database
GPG key ID: D8B9F5AEAE84E4CF
9 changed files with 124 additions and 51 deletions

View file

@ -1,8 +1,8 @@
'use strict';
require('../common');
// This tests that AsyncResource throws an error if bad parameters are passed
const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const { AsyncResource } = async_hooks;
@ -14,16 +14,28 @@ async_hooks.createHook({
assert.throws(() => {
return new AsyncResource();
}, /^TypeError: type must be a string with length > 0$/);
}, common.expectsError({
code: 'ERR_ASYNC_TYPE',
type: TypeError,
}));
assert.throws(() => {
new AsyncResource('');
}, /^TypeError: type must be a string with length > 0$/);
}, common.expectsError({
code: 'ERR_ASYNC_TYPE',
type: TypeError,
}));
assert.throws(() => {
new AsyncResource('type', -4);
}, /^RangeError: triggerAsyncId must be an unsigned integer$/);
}, common.expectsError({
code: 'ERR_INVALID_ASYNC_ID',
type: RangeError,
}));
assert.throws(() => {
new AsyncResource('type', Math.PI);
}, /^RangeError: triggerAsyncId must be an unsigned integer$/);
}, common.expectsError({
code: 'ERR_INVALID_ASYNC_ID',
type: RangeError,
}));