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

async_hooks: not able to preserve continuation local storage inside Promises #13583

Closed
gms1 opened this issue Jun 9, 2017 · 3 comments · Fixed by #13585
Closed

async_hooks: not able to preserve continuation local storage inside Promises #13583

gms1 opened this issue Jun 9, 2017 · 3 comments · Fixed by #13585
Labels
async_hooks Issues and PRs related to the async hooks subsystem. promises Issues and PRs related to ECMAScript promises.

Comments

@gms1
Copy link

gms1 commented Jun 9, 2017

Hi,

maybe related to: (#13367),
the init-hook (now?) reports a triggerId of 0 for type 'PROMISE' sometimes

code to reproduce:

const asyncHooks = require('async_hooks');

let beforeId;
asyncHooks
    .createHook({
    init: (id, type, triggerId) => {
        process._rawDebug(`init: id:${id} type:${type} triggerId:${triggerId}`);
    },
    before: (id) => {
        process._rawDebug(`before: id:${id}`);
        beforeId = id;
    }
})
    .enable();

function tracePromise(prefix) {
    process._rawDebug(`promise : ${prefix}: currId:${asyncHooks.currentId()} lastId:${beforeId}`);
}

setImmediate(() => {
    doTrace = true;
    return new Promise((resolve, reject) => {
        tracePromise('P1: EXECUTOR FUNC');
        resolve();
    })
        .then(() => {
        tracePromise('P1: THEN');
        return new Promise((resolve, reject) => {
            tracePromise('P2: EXECUTOR FUNC');
            resolve();
        })
            .then(() => {
            tracePromise('P2: THEN');
            return new Promise((resolve, reject) => {
                tracePromise('P3: EXECUTOR FUNC');
                resolve();
            })
                .then(() => {
                tracePromise('P3: THEN');
            });
        });
    });
});

output from above:

gms@sirius:~/work/HOT/node-async-context/test (master)$ node promise.js 
init: id:2 type:Immediate triggerId:1
before: id:2
init: id:3 type:PROMISE triggerId:2
promise : P1: EXECUTOR FUNC: currId:2 lastId:2
init: id:4 type:PROMISE triggerId:3
before: id:4
promise : P1: THEN: currId:0 lastId:4
init: id:5 type:PROMISE triggerId:0
promise : P2: EXECUTOR FUNC: currId:0 lastId:4
init: id:6 type:PROMISE triggerId:5
before: id:6
promise : P2: THEN: currId:0 lastId:6
init: id:7 type:PROMISE triggerId:0
promise : P3: EXECUTOR FUNC: currId:0 lastId:6
init: id:8 type:PROMISE triggerId:7
init: id:9 type:PROMISE triggerId:6
before: id:8
promise : P3: THEN: currId:0 lastId:8
init: id:10 type:PROMISE triggerId:8
before: id:10
before: id:9

Guenter

@addaleax addaleax added async_hooks Issues and PRs related to the async hooks subsystem. promises Issues and PRs related to ECMAScript promises. labels Jun 9, 2017
@gms1
Copy link
Author

gms1 commented Jun 9, 2017

for comparison the output using nodejs 8.0 showing similar behaviour:

gms@sirius:~/work/HOT/node-async-context/test (master)$ node -v
v8.0.0
gms@sirius:~/work/HOT/node-async-context/test (master)$ node promise.js 
init: id:2 type:Immediate triggerId:1
before: id:2
init: id:3 type:PROMISE triggerId:2
promise : P1: EXECUTOR FUNC: currId:2 lastId:2
init: id:4 type:PROMISE triggerId:2
before: id:4
promise : P1: THEN: currId:0 lastId:4
init: id:5 type:PROMISE triggerId:0
promise : P2: EXECUTOR FUNC: currId:0 lastId:4
init: id:6 type:PROMISE triggerId:0
before: id:6
promise : P2: THEN: currId:0 lastId:6
init: id:7 type:PROMISE triggerId:0
promise : P3: EXECUTOR FUNC: currId:0 lastId:6
init: id:8 type:PROMISE triggerId:0
init: id:9 type:PROMISE triggerId:0
before: id:8
promise : P3: THEN: currId:0 lastId:8
init: id:10 type:PROMISE triggerId:0
before: id:10
before: id:9

@addaleax
Copy link
Member

addaleax commented Jun 9, 2017

Can you check that #13427 does what you want it to do?

@gms1
Copy link
Author

gms1 commented Jun 9, 2017

Thank you for your fast response!
After checking out your 'async-hooks-promise-pushpop' branch and generating node, I am getting this output:

gms@sirius:~/work/HOT/node-async-context/test (master)$ node -v
v9.0.0-pre
gms@sirius:~/work/HOT/node-async-context/test (master)$ /usr/local/node/bin/node promise-then-promise.js 
init: id:2 type:Immediate triggerId:1
before: id:2
init: id:3 type:PROMISE triggerId:2
promise : P1: EXECUTOR FUNC: currId:2 lastId:2
init: id:4 type:PROMISE triggerId:3
before: id:4
promise : P1: THEN: currId:4 lastId:4
init: id:5 type:PROMISE triggerId:4
promise : P2: EXECUTOR FUNC: currId:4 lastId:4
init: id:6 type:PROMISE triggerId:5
before: id:6
promise : P2: THEN: currId:6 lastId:6
init: id:7 type:PROMISE triggerId:6
promise : P3: EXECUTOR FUNC: currId:6 lastId:6
init: id:8 type:PROMISE triggerId:7
init: id:9 type:PROMISE triggerId:6
before: id:8
promise : P3: THEN: currId:8 lastId:8
init: id:10 type:PROMISE triggerId:8
before: id:10
before: id:9

So the currentId() and the last id reported by the before-hook are now always the same
and using the triggerId it should be possible to preserve the continuation local storage:

P1: EXECUTOR FUNC: currId:2
P1: THEN: currId:4 => id:4 type:PROMISE triggerId:3 => id:3 type:PROMISE triggerId:2
P2: EXECUTOR FUNC: currId:4 lastId:4 => ...
P2: THEN: currId:6 => id:6 type:PROMISE triggerId:5 => id:5 type:PROMISE triggerId:4 ...
P3: EXECUTOR FUNC: currId:6 => ...
P3: THEN: currId:8 => id:8 type:PROMISE triggerId:7 => id:7 type:PROMISE triggerId:6 ...

Many Thanks!
Guenter

addaleax added a commit to addaleax/node that referenced this issue Jun 16, 2017
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: nodejs#13583
PR-URL: nodejs#13585
Reviewed-By: Trevor Norris <[email protected]>
addaleax added a commit that referenced this issue Jun 17, 2017
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]>
addaleax added a commit that referenced this issue Jun 21, 2017
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]>
addaleax added a commit that referenced this issue Jun 24, 2017
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]>
rvagg pushed a commit that referenced this issue Jun 29, 2017
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]>
addaleax added a commit that referenced this issue Jul 11, 2017
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem. promises Issues and PRs related to ECMAScript promises.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants