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

GraalVM NodeJS AsyncLocalStorage context gets lost after await #772

Closed
PepeBotella25 opened this issue Oct 3, 2023 · 1 comment
Closed
Assignees
Labels
bug Something isn't working

Comments

@PepeBotella25
Copy link
Member

PepeBotella25 commented Oct 3, 2023

Hi. I'm using GraalVM 22.3.2 with Node.js (I'm targeting Nodejs 16.x.) installed by gu install nodejs.

It seems that AsyncLocalStorage context get lost after await statements (see the code below).
However it's available using "regular/old" Promise methods such as: .then, .finally
Also, in the example, changing .finally for try/finally make the context unavailable in the finally block.

Code:

const http = require("http");
const { AsyncLocalStorage } = require("async_hooks");

const logWithId = (msg) => {
    const { id } = asyncLocalStorage.getStore() || { id: "N/A" };
    console.log(`${msg} ${id}`);
};

let idSeq = 0;
const asyncLocalStorage = new AsyncLocalStorage();

const server = http.createServer((req, res) => {
    const promise = new Promise((resolve) => setTimeout(resolve, Math.random() * 1000));

    asyncLocalStorage.run({ id: idSeq++ }, () => {
        logWithId("start");

        const handler = async () => {
            await promise.then(() => logWithId("then"));
            logWithId("await"); // <--- Here the context gets lost
        };

        handler().finally(() => {
            logWithId("finish");
            res.end();
        });
    });
});

server.listen(8080);

const get = () => new Promise((resolve) => http.get("http://localhost:8080", null, resolve));

Promise
    .all([get(), get(), get(), get()])
    .finally(() => server.close());

Output in Node 16 (expected):

start 0
start 1
start 2
start 3
then 3
await 3
finish 3
then 2
await 2
finish 2
then 0
await 0
finish 0
then 1
await 1
finish 1

Output of $GRAALVM/bin/node 16:

start 0
start 1
start 2
start 3
then 0
await N/A
finish 0
then 3
await N/A
finish 3
then 1
await N/A
finish 1
then 2
await N/A
finish 2

This is also reproducible at least in:
graalvm 21.3.5
graalvm 22.3.3
graalvm 22.3.1

@PepeBotella25 PepeBotella25 changed the title GraalVM NodeJS AsyncLocalStorage context get lost after await GraalVM NodeJS AsyncLocalStorage context gets lost after await Oct 3, 2023
@iamstolis iamstolis self-assigned this Oct 4, 2023
@iamstolis iamstolis added the bug Something isn't working label Oct 4, 2023
@iamstolis
Copy link
Member

Fixed by c5a9c0a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants