-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async_hooks: proper id stacking for Promises
Until now, the async_hooks PromiseHook did not register the Promise’s async id and trigger id on the id stack, so inside the `.then()` handler those ids would be invalid. To fix this, add push and pop calls to its `before` and `after` parts, respectively. Some care needs to be taken for the cases that the Promise hook is being disabled or enabled during the execution of a Promise handler; in the former case, actually removing the hook is delayed by adding another task to the microtask queue, in the latter case popping the id off the async id stack is skipped if the ids don’t match. Fixes: #13583 PR-URL: #13585 Reviewed-By: Trevor Norris <[email protected]>
- Loading branch information
Showing
7 changed files
with
97 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
const hook = async_hooks.createHook({ | ||
init: common.mustCall(2), | ||
before: common.mustCall(1), | ||
after: common.mustNotCall() | ||
}).enable(); | ||
|
||
Promise.resolve(1).then(common.mustCall(() => { | ||
hook.disable(); | ||
|
||
Promise.resolve(42).then(common.mustCall()); | ||
|
||
process.nextTick(common.mustCall()); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
Promise.resolve(1).then(common.mustCall(() => { | ||
async_hooks.createHook({ | ||
init: common.mustCall(), | ||
before: common.mustCall(), | ||
after: common.mustCall(2) | ||
}).enable(); | ||
|
||
process.nextTick(common.mustCall()); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
common.crashOnUnhandledRejection(); | ||
|
||
const promiseAsyncIds = []; | ||
|
||
async_hooks.createHook({ | ||
init: common.mustCallAtLeast((id, type, triggerId) => { | ||
if (type === 'PROMISE') { | ||
// Check that the last known Promise is triggering the creation of | ||
// this one. | ||
assert.strictEqual(promiseAsyncIds[promiseAsyncIds.length - 1] || 1, | ||
triggerId); | ||
promiseAsyncIds.push(id); | ||
} | ||
}, 3), | ||
before: common.mustCall((id) => { | ||
assert.strictEqual(id, promiseAsyncIds[1]); | ||
}), | ||
after: common.mustCall((id) => { | ||
assert.strictEqual(id, promiseAsyncIds[1]); | ||
}) | ||
}).enable(); | ||
|
||
Promise.resolve(42).then(common.mustCall(() => { | ||
assert.strictEqual(async_hooks.executionAsyncId(), promiseAsyncIds[1]); | ||
assert.strictEqual(async_hooks.triggerAsyncId(), promiseAsyncIds[0]); | ||
Promise.resolve(10); | ||
})); |