Skip to content

Commit af444fc

Browse files
committed
[squash] do not emit init event
1 parent 029b68f commit af444fc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/async-wrap.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,17 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
296296
PromiseWrap* wrap = Unwrap<PromiseWrap>(promise);
297297
if (type == PromiseHookType::kInit ||
298298
wrap == nullptr) {
299+
uint32_t stored_init_field;
300+
if (type != PromiseHookType::kInit) {
301+
// Do not emit an init event.
302+
stored_init_field = env->async_hooks()->fields()[AsyncHooks::kInit];
303+
env->async_hooks()->fields()[AsyncHooks::kInit] = 0;
304+
}
299305
wrap = new PromiseWrap(env, promise);
300306
wrap->MakeWeak(wrap);
307+
if (type != PromiseHookType::kInit) {
308+
env->async_hooks()->fields()[AsyncHooks::kInit] = stored_init_field;
309+
}
301310
} else if (type == PromiseHookType::kResolve) {
302311
// TODO(matthewloring): need to expose this through the async hooks api.
303312
}

test/parallel/test-async-wrap-promise-after-enabled.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const p = new Promise((resolve) => resolve(1));
1313
p.then(() => seenEvents.push('then'));
1414

1515
const hooks = async_hooks.createHook({
16+
init: common.mustNotCall(),
17+
1618
before: common.mustCall((id) => {
1719
assert.ok(id > 1);
1820
seenEvents.push('before');
@@ -23,8 +25,10 @@ const hooks = async_hooks.createHook({
2325
seenEvents.push('after');
2426
hooks.disable();
2527
})
26-
}).enable();
28+
});
2729

2830
setImmediate(() => {
2931
assert.deepStrictEqual(seenEvents, ['before', 'then', 'after']);
3032
});
33+
34+
hooks.enable(); // After `setImmediate` in order to not catch its init event.

0 commit comments

Comments
 (0)