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

asyncLocalStorage shared in different modules #32578

Closed
jacksonfu opened this issue Mar 31, 2020 · 4 comments
Closed

asyncLocalStorage shared in different modules #32578

jacksonfu opened this issue Mar 31, 2020 · 4 comments

Comments

@jacksonfu
Copy link

  • Version:v13.12.0
  • Platform: linux
  • Subsystem:

What steps will reproduce the bug?

I want to call function named test defined in other.js, and the caller script is test.js.

// test.js
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

var other = require('./other');

async function testAsync(id){
    var res = await other.test();
    console.log(asyncLocalStorage.getStore());
    return res;
}

async function localStorge(id_) {
    var store = { id: id_ };
    asyncLocalStorage.enterWith(store);
    console.log("store:", asyncLocalStorage.getStore());
    var res = await testAsync(id_);
}

localStorge(1);
localStorge(2);

And the callee script showed as follow.

// other.js
const {AsyncLocalStorage} = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

async function test() {
    console.log(asyncLocalStorage.getStore()); // maybe it should be same as {id: x} in test.js, but get undefined
    return "suc";
}

module.exports = {
    test,
};

How often does it reproduce? Is there a required condition?

always.

What is the expected behavior?

In other.js, it should be {id: x} the same as test.js.
Maybe different modules could shared the same AsyncLocalStorage. And the value of the callee script depends on the caller script.

What do you see instead?

The output is always undefined.

Additional information

In zone.js, different modules can share the same context using Zone.current.name.

@puzpuzpuz many thanks!!!

@jacksonfu
Copy link
Author

Maybe I should export the instance asyncLocalStorage in other.js. But I want to know if AsyncLocalStorage can be like Zone in zone.js.

@jacksonfu jacksonfu changed the title asyncLocalStorage lost in different modules asyncLocalStorage shared in different modules Mar 31, 2020
@legendecas
Copy link
Member

Each AsyncLocalStorage doesn't share the same underlying storage. You may export the instantiated asyncLocalStorage in test.js and use this exported asyncLocalStorage in other js files.

@legendecas
Copy link
Member

legendecas commented Mar 31, 2020

Maybe I should export the instance asyncLocalStorage in other.js. But I want to know if AsyncLocalStorage can be like Zone in zone.js.

Zone has a global static current zone property. This concept is not added to AsyncLocalStorage in purpose IIRC.

@himself65
Copy link
Member

himself65 commented Mar 31, 2020

maybe you would like this #32062

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants