Skip to content

Problems about the async_hooks module #471

Open
@Soulike

Description

@Soulike

Hi. I'm using GraalVM 21.1.0 with Node.js installed (by gu install nodejs) on macOS 11.4. I want to acquire triggerAsyncId and executionAsyncId using the async_hooks module for indicating the relationship between async contexts, but the execution results of Node.js and $GRAALVM/bin/node are different.

Code

const fs = require('fs');
const async_hooks = require('async_hooks');

async_hooks.createHook({
    init(asyncId, type, triggerAsyncId, resource)
    {
        fs.writeSync(1, `init: ${triggerAsyncId} -> ${asyncId}\n`);
    },
    before(asyncId)
    {
        fs.writeSync(1, `before: ${async_hooks.triggerAsyncId()} -> ${async_hooks.executionAsyncId()}\n`);
    },
}).enable();

let globalVar = 1;

async function setTimeoutAsync(timeout)
{
    return new Promise(resolve =>
    {
        setTimeout(() => resolve(), timeout);
    });
}

async function asyncFunc()
{
    globalVar = 2;
    await setTimeoutAsync(10);
    fs.writeSync(1, `${async_hooks.triggerAsyncId()} -> ${async_hooks.executionAsyncId()}\n`);
    globalVar = 3;
    await setTimeoutAsync(10);
    fs.writeSync(1, `${async_hooks.triggerAsyncId()} -> ${async_hooks.executionAsyncId()}\n`);
    globalVar = 4;
}

asyncFunc();

Output of Node.js 14.16.1

init: 1 -> 2
init: 1 -> 3
init: 1 -> 4
init: 1 -> 5
init: 3 -> 6
before: 1 -> 3
init: 4 -> 7
before: 1 -> 5
before: 4 -> 7
before: 3 -> 6
3 -> 6
init: 6 -> 8
init: 6 -> 9
init: 6 -> 10
init: 8 -> 11
before: 6 -> 8
init: 9 -> 12
before: 6 -> 10
before: 9 -> 12
before: 8 -> 11
8 -> 11

Output of $GRAALVM/bin/node (also 14.16.1)

init: 1 -> 2
init: 1 -> 3
init: 1 -> 4
init: 1 -> 5
init: 4 -> 6
before: 1 -> 5
before: 4 -> 6
0 -> 0
init: 0 -> 7
init: 0 -> 8
init: 0 -> 9
init: 8 -> 10
before: 0 -> 9
before: 8 -> 10
0 -> 0

The problem is that if async_hooks.triggerAsyncId() or async_hooks.executionAsyncId() returns 0, it's impossible for me to get the relationship between async contexts.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions