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

Incorrect behavior in AsyncLocalStorage #642

Open
joonseokhu opened this issue Sep 11, 2024 · 1 comment
Open

Incorrect behavior in AsyncLocalStorage #642

joonseokhu opened this issue Sep 11, 2024 · 1 comment

Comments

@joonseokhu
Copy link

The result of this code below

const { AsyncLocalStorage } = require('node:async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

const store = { id: 2 };

try {
  asyncLocalStorage.run(store, () => {
    const store1 = asyncLocalStorage.getStore(); // Returns the store object
    console.log({ store1 })
    setTimeout(() => {
      const store2 = asyncLocalStorage.getStore(); // Returns the store object
      console.log({ store2 })
    }, 200);
    throw new Error();
  });
} catch (e) {
  const store3 = asyncLocalStorage.getStore(); // Returns undefined
  console.log({ store3 })
  // The error will be caught here
}

should be this:

{ store1: { id: 2 } }
{ store3: undefined }
{ store2: { id: 2 } }

but, the actual result from RunJS was this:

{ store1: { id: 2 } }
{ store3: undefined }
{ store2: undefined }
2

The code that I wrote was almost same as that in Nodejs Document:
https://nodejs.org/api/async_context.html#asynclocalstoragerunstore-callback-args

@lukehaas
Copy link
Owner

@joonseokhu, thanks for raising this. I've taken a quick look, and it seems that this may be an upstream bug in Electron.

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

2 participants