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

napi_make_callback() with null async_context doesn't reuse current context #35188

Closed
Flarna opened this issue Sep 14, 2020 · 2 comments
Closed
Labels
addons Issues and PRs related to native addons. async_hooks Issues and PRs related to the async hooks subsystem. node-api Issues and PRs related to the Node-API.

Comments

@Flarna
Copy link
Member

Flarna commented Sep 14, 2020

  • Version: 14.10.1, 12.18.3, 10.22.0
  • Platform: all
  • Subsystem: napi, async_hooks

What steps will reproduce the bug?

Call back sync to javascript from native using napi_make_callback() with async_context set to NULL.
The simplest way to reproduce this is to add following code in test/node-api/test_make_callback_recurse/test.js

const { executionAsyncId } = require("async_hooks")
const eId = executionAsyncId();
makeCallback("foo", common.mustCall(function() {
  assert.strictEqual(executionAsyncId(), eId); // executionAsyncId is 0 here
}));

According to docs I would expect the test to pass as they tell However NULL is also allowed, which indicates the current async context (if any) is to be used for the callback..

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

Always

What is the expected behavior?

I would expect that the current async id is not changed.

What do you see instead?

async_id 0 is used

Additional information

I'm not sure here if the docs should be corrected or the implementation.
Maybe we should even disallow to pass NULL to napi_make_callback().

Relevant place in source:

node/src/node_api.cc

Lines 732 to 734 in 9d12c14

if (node_async_context == nullptr) {
static node::async_context empty_context = { 0, 0 };
node_async_context = &empty_context;

Please note also that the c++ NAPI wrapper uses NULL as default argument for MakeCallback (see https://github.com/nodejs/node-addon-api/blob/6562e6b0ab604fd55b9c2d0cf954c9ce93e4bdee/napi.h#L1056-L1065).

@Flarna Flarna added addons Issues and PRs related to native addons. async_hooks Issues and PRs related to the async hooks subsystem. node-api Issues and PRs related to the Node-API. labels Sep 14, 2020
@Flarna
Copy link
Member Author

Flarna commented Sep 15, 2020

fyi @nodejs/n-api I'm happy to create a PR to correct this but I'm not sure if it is a doc issue or a implemenation issue.

@mhdawson
Copy link
Member

We discussed in the meeting today, and we think that it's most likely a bug versus doc issue. PR would be welcome.

MylesBorins pushed a commit that referenced this issue Sep 29, 2020
Calling napi_make_callback() with no async_context is not resulting in
using the current async context instead an empty context (id 0) is
used.

Using NULL is like using node::Makecallback without async_context which
is deprecated since Node.js 10 (DEP0099).

PR-URL: #35321
Fixes: #35188
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
joesepi pushed a commit to joesepi/node that referenced this issue Jan 8, 2021
Calling napi_make_callback() with no async_context is not resulting in
using the current async context instead an empty context (id 0) is
used.

Using NULL is like using node::Makecallback without async_context which
is deprecated since Node.js 10 (DEP0099).

PR-URL: nodejs#35321
Fixes: nodejs#35188
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addons Issues and PRs related to native addons. async_hooks Issues and PRs related to the async hooks subsystem. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants