-
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: fix id assignment in fast-path promise hook
Native side of fast-path promise hook was not calling JS fastPromiseHook function when there were no async ids previously assigned to the promise. Because of that already created promises could not get id assigned in situations when an async hook without a before listener function is enabled after their creation. As the result executionAsyncId could return wrong id when called within promise's .then(). Refs: #34512 PR-URL: #34548 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
- Loading branch information
1 parent
bd55236
commit e9aebc3
Showing
2 changed files
with
69 additions
and
38 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
25 changes: 25 additions & 0 deletions
25
test/parallel/test-async-hooks-enable-before-promise-resolve.js
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,25 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
// This test ensures that fast-path PromiseHook assigns async ids | ||
// to already created promises when the native hook function is | ||
// triggered on before event. | ||
|
||
let initialAsyncId; | ||
const promise = new Promise((resolve) => { | ||
setTimeout(() => { | ||
initialAsyncId = async_hooks.executionAsyncId(); | ||
async_hooks.createHook({ | ||
after: common.mustCall(() => {}, 2) | ||
}).enable(); | ||
resolve(); | ||
}, 0); | ||
}); | ||
|
||
promise.then(common.mustCall(() => { | ||
const id = async_hooks.executionAsyncId(); | ||
assert.notStrictEqual(id, initialAsyncId); | ||
assert.ok(id > 0); | ||
})); |