From 120f2e26f5af50d803fed9bad663cad37556b3a3 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 3 Jul 2017 14:16:12 +0200 Subject: [PATCH 1/2] async_hooks: move restoreTmpHooks call to init This fixes an error that could occure by nesting async_hooks calls --- lib/async_hooks.js | 10 +++++----- .../test-async-hooks-enable-recursive.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-async-hooks-enable-recursive.js diff --git a/lib/async_hooks.js b/lib/async_hooks.js index a730738c0f9144..42d04f063081c2 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -335,11 +335,6 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) { throw new RangeError('triggerAsyncId must be an unsigned integer'); init(asyncId, type, triggerAsyncId, resource); - - // Isn't null if hooks were added/removed while the hooks were running. - if (tmp_active_hooks_array !== null) { - restoreTmpHooks(); - } } function emitHookFactory(symbol, name) { @@ -442,6 +437,11 @@ function init(asyncId, type, triggerAsyncId, resource) { fatalError(e); } processing_hook = false; + + // Isn't null if hooks were added/removed while the hooks were running. + if (tmp_active_hooks_array !== null) { + restoreTmpHooks(); + } } diff --git a/test/parallel/test-async-hooks-enable-recursive.js b/test/parallel/test-async-hooks-enable-recursive.js new file mode 100644 index 00000000000000..64b35b69bf3ac3 --- /dev/null +++ b/test/parallel/test-async-hooks-enable-recursive.js @@ -0,0 +1,19 @@ +'use strict'; + +const common = require('../common'); +const async_hooks = require('async_hooks'); +const crypto = require('crypto'); + +const nestedHook = async_hooks.createHook({ + init: common.mustCall() +}); + +async_hooks.createHook({ + init: common.mustCall((id, type) => { + nestedHook.enable(); + }, 2) +}).enable(); + +crypto.randomBytes(1, common.mustCall(() => { + crypto.randomBytes(1, common.mustCall()); +})); From ef7211bbb5187317e49244d1be0148b4474a9a67 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 11 Jul 2017 02:31:44 +0200 Subject: [PATCH 2/2] fixup: use fs instead of crypto --- test/parallel/test-async-hooks-enable-recursive.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-async-hooks-enable-recursive.js b/test/parallel/test-async-hooks-enable-recursive.js index 64b35b69bf3ac3..bcb0dcc0ce0a66 100644 --- a/test/parallel/test-async-hooks-enable-recursive.js +++ b/test/parallel/test-async-hooks-enable-recursive.js @@ -2,7 +2,7 @@ const common = require('../common'); const async_hooks = require('async_hooks'); -const crypto = require('crypto'); +const fs = require('fs'); const nestedHook = async_hooks.createHook({ init: common.mustCall() @@ -14,6 +14,6 @@ async_hooks.createHook({ }, 2) }).enable(); -crypto.randomBytes(1, common.mustCall(() => { - crypto.randomBytes(1, common.mustCall()); +fs.access(__filename, common.mustCall(() => { + fs.access(__filename, common.mustCall()); }));