Skip to content

AsyncLocalStore looses context in MakeCallback #43038

Open
@Flarna

Description

@Flarna

Version

18.1.0, 14.19.1 (and most likely a lot others)

Platform

Tested on Microsoft Windows NT 10.0.22000.0 x64 but very likely platform independent.

Subsystem

async_hooks

What steps will reproduce the bug?

Use node::MakeCallback() with a receiver different to the AsyncResource.

See e.g. snappy 6.x which uses Nan::AsyncWorker.

In this setup the complete callback is not called with the AsyncResource as target and this results in setting the wrong object as current resource in MakeCallback.

Calling callbacks with undefined/null/global as target is not that uncommon.

The before/after callbacks are called with the correct asyncId but AsyncLocalStore relies on the current resource.

const { executionAsyncId, AsyncLocalStorage } = require("async_hooks")
const snappy = require("snappy")
const als = new AsyncLocalStorage()

als.run(15, () => {
  snappy.compress("Hello World!", () => {
    const cbId = executionAsyncId()
    console.log(`compressed: ${cbId}, als: ${als.getStore()}`)
  })
})

How often does it reproduce? Is there a required condition?

Always if MakeCallback is used with a receiver different then the AsyncResource object.

What is the expected behavior?

no context loss

What do you see instead?

context loss

Additional information

No response

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