Skip to content

AsyncLocalStorage getStore unexpected behaviour #2546

Description

@NoelAbrahams
  • Node.js Version: 13.11.0
  • OS: Windows 10
  • Scope: Code
  • Module: async_hooks

Sample Code

import { AsyncLocalStorage } from 'async_hooks';
const asynLocalStorage = new AsyncLocalStorage();

httpServer.on('request', (request, response) => {

    // Storage map
    const key = 'KEY-1';
    const store = new Map();
    store.set(key, { foo: 'foo' });

    // Run in context
    asyncLocalStorage.run(store, () => {

        request.on('end', () => {

            // asyncLocalStorage.enterWith(store);
            const local = asyncLocalStorage.getStore(); 
            console.log((local?.get('KEY-1'))); // Line-B: This is undefined 
        });

        const local = asyncLocalStorage.getStore();
        console.log(local?.get('KEY-1')); // Line-A: This is fine

        // Other work (omitted)
    });
});

Expected outcome
The call togetStore should return and log the stored map (Line-B)

Actual
The value is undefined

Does anyone have an idea why the call to getStore is not preserving the context?

Related
#2537

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions