lukehaas / RunJS

RunJS is a JavaScript playground for macOS, Windows and Linux. Write code with instant feedback and access to Node.js and browser APIs.
https://runjs.app
2.03k stars 43 forks source link

Incorrect behavior in `AsyncLocalStorage` #642

Open joonseokhu opened 1 week ago

joonseokhu commented 1 week ago

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 commented 1 week ago

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