Skip to content

Commit

Permalink
fix: ignore TIMERWRAP in AsyncHooksContextManager (#1530)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
Flarna and dyladan authored Sep 21, 2020
1 parent a41f403 commit 3bc4d57
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
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

0 comments on commit 3bc4d57

Please sign in to comment.