Skip to content

AsyncLocalStorage loses the store if used together with createHook if there is a async function in between #53037

Closed
@mcollina

Description

@mcollina

Consider the following code:

import { AsyncLocalStorage, createHook } from 'node:async_hooks';
import { notEqual } from 'assert/strict';

const hook = createHook({
  init () {},
})

// Commenting out this line will make the bug go away
hook.enable();

const asyncLocalStorage = new AsyncLocalStorage();

async function main () {
  // Commenting out this line will make the bug go away
  await 1
  asyncLocalStorage.enterWith({ foo: 'bar' });
}

await main()

console.log(executionAsyncResource());

// Should not be undefined
notEqual(asyncLocalStorage.getStore(), undefined);

Note that:

  1. disabling the hook solve the problem
  2. removing the await 1 promise solves the problem

Debugging the executionAsyncResource() in this case:

Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 12,
  [Symbol(trigger_async_id_symbol)]: 9
}

while in without the createHook().enable() call:

{ [Symbol(kResourceStore)]: { foo: 'bar' } }

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooksIssues and PRs related to the async hooks subsystem.async_local_storageAsyncLocalStorage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions