RunJS icon indicating copy to clipboard operation
RunJS copied to clipboard

Incorrect behavior in `AsyncLocalStorage`

Open joonseokhu opened this issue 1 year ago • 1 comments

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

joonseokhu avatar Sep 11 '24 00:09 joonseokhu

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

lukehaas avatar Sep 11 '24 08:09 lukehaas