Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore TIMERWRAP in AsyncHooksContextManager #1530

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ export class AsyncHooksContextManager extends AbstractAsyncHooksContextManager {
* Init hook will be called when userland create a async context, setting the
* context as the current one if it exist.
* @param uid id of the async context
* @param type the resource type
*/
private _init(uid: number) {
private _init(uid: number, type: string) {
// ignore TIMERWRAP as they combine timers with same timeout which can lead to
// false context propagation. TIMERWRAP has been removed in node 11
// every timer has it's own `Timeout` resource anyway which is used to propagete
// context.
if (type === 'TIMERWRAP') return;

const context = this._stack[this._stack.length - 1];
if (context !== undefined) {
this._contexts.set(uid, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,34 @@ for (const contextManagerClass of [
});
assert.strictEqual(contextManager.active(), Context.ROOT_CONTEXT);
});

it('should work with timers using the same timeout', done => {
let cnt = 3;
function countDown() {
cnt--;
if (cnt === 0) done();
if (cnt < 0) throw new Error('too many calls to countDown()');
}

const time1 = 2;
const time2 = time1 + 1;
const rootCtx = contextManager.active();
const innerCtx = rootCtx.setValue(Symbol('test'), 23);
contextManager.with(innerCtx, () => {
setTimeout(() => {
assert.strictEqual(contextManager.active(), innerCtx);
countDown();
}, time1);
});
setTimeout(() => {
assert.strictEqual(contextManager.active(), rootCtx);
countDown();
}, time1);
setTimeout(() => {
assert.strictEqual(contextManager.active(), rootCtx);
countDown();
}, time2);
});
});

describe('.bind(function)', () => {
Expand Down