async_hooks: use parent promise as triggerId · nodejs/node@135f4e6 · GitHub
Skip to content

Commit 135f4e6

Browse files
JiaLiPassionjasnell
authored andcommitted
async_hooks: use parent promise as triggerId
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: #13302 PR-URL: #13367 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 9db02dc commit 135f4e6

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

src/async-wrap.cc

Lines changed: 17 additions & 0 deletions
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const initHooks = require('./init-hooks');
6+
const { checkInvocations } = require('./hook-checks');
7+
8+
const p = new Promise(common.mustCall(function executor(resolve, reject) {
9+
resolve(5);
10+
}));
11+
12+
p.then(function afterresolution(val) {
13+
assert.strictEqual(val, 5);
14+
return val;
15+
});
16+
17+
// init hooks after chained promise is created
18+
const hooks = initHooks();
19+
hooks._allowNoInit = true;
20+
hooks.enable();
21+
22+
23+
process.on('exit', function onexit() {
24+
hooks.disable();
25+
hooks.sanityCheck('PROMISE');
26+
27+
// Because the init event was never emitted the hooks listener doesn't
28+
// know what the type was. Thus check for Unknown rather than PROMISE.
29+
const as = hooks.activitiesOfTypes('PROMISE');
30+
const unknown = hooks.activitiesOfTypes('Unknown');
31+
assert.strictEqual(as.length, 0);
32+
assert.strictEqual(unknown.length, 1);
33+
34+
const a0 = unknown[0];
35+
assert.strictEqual(a0.type, 'Unknown');
36+
assert.strictEqual(typeof a0.uid, 'number');
37+
checkInvocations(a0, { before: 1, after: 1 }, 'when process exits');
38+
});

test/async-hooks/test-promise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function onexit() {
4646
const a1 = as[1];
4747
assert.strictEqual(a1.type, 'PROMISE', 'promise request');
4848
assert.strictEqual(typeof a1.uid, 'number', 'uid is a number');
49-
assert.strictEqual(a1.triggerId, 1, 'parent uid 1');
49+
assert.strictEqual(a1.triggerId, a0.uid);
5050
// We expect a destroy hook as well but we cannot guarentee predictable gc.
5151
checkInvocations(a1, { init: 1, before: 1, after: 1 }, 'when process exits');
5252
}
Lines changed: 42 additions & 0 deletions

0 commit comments

Comments
 (0)