Skip to content

Commit

Permalink
async_hooks: use parent promise as triggerId in init hook
Browse files Browse the repository at this point in the history
async_hooks init callback will be triggered when promise newly created,
in previous version, the parent promise which pass from chrome V8 PromiseHook
is ignored, so we can't tell the promise is a pure new promise or a
chained promise.

In this commit, we use the parent promise's id as triggerId to
trigger the init callback.

Fixes: nodejs#13302
  • Loading branch information
JiaLiPassion committed Jun 2, 2017
1 parent e824c75 commit 979894c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,23 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
PromiseWrap* wrap = Unwrap<PromiseWrap>(promise);
if (type == PromiseHookType::kInit || wrap == nullptr) {
bool silent = type != PromiseHookType::kInit;
// set parent promise's async Id as this promise's triggerId
if (parent->IsPromise()) {
// parent promise exists, current promise
// is a chained promise, so we set parent promise's id as
// current promise's triggerId
Local<Promise> parent_promise = parent.As<Promise>();
auto parent_wrap = Unwrap<PromiseWrap>(parent_promise);

if (parent_wrap == nullptr) {
// create a new PromiseWrap for parent promise with silent parameter
parent_wrap = new PromiseWrap(env, parent_promise, true);
parent_wrap->MakeWeak(parent_wrap);
}
// get id from parentWrap
double trigger_id = parent_wrap->get_id();
env->set_init_trigger_id(trigger_id);
}
wrap = new PromiseWrap(env, promise, silent);
wrap->MakeWeak(wrap);
} else if (type == PromiseHookType::kResolve) {
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function onexit() {
const a1 = as[1];
assert.strictEqual(a1.type, 'PROMISE', 'promise request');
assert.strictEqual(typeof a1.uid, 'number', 'uid is a number');
assert.strictEqual(a1.triggerId, 1, 'parent uid 1');
assert.strictEqual(a1.triggerId, 2, 'parent triggerId 2');
// We expect a destroy hook as well but we cannot guarentee predictable gc.
checkInvocations(a1, { init: 1, before: 1, after: 1 }, 'when process exits');
}

0 comments on commit 979894c

Please sign in to comment.