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

[WIP] node-api: fix data race and use-after-free in napi_threadsafe_function #55877

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mika-fischer
Copy link
Contributor

Other threads can still hold a valid handle to the tsfn after
finalization if finalization was triggered by

  • release with napi_tsfn_abort, or
  • environment shutdown

Handle this by:

  • protecting finalization itself with the mutex
  • if necessary, delay deletion after finalization to when thread_count
    drops to 0
  • releasing all resources as soon as possible before deletion

Fixes: #55706

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/node-api

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API. labels Nov 16, 2024
@mika-fischer
Copy link
Contributor Author

I still need to see how to integrate the test from here https://github.com/mika-fischer/node-bug-napi-tsfn

@mhdawson
Copy link
Member

@gabrielschulhof you mentioned you'd take a look at this issue in our weekly meeting last week. If you can take a look at this PR it would be great.

@gabrielschulhof
Copy link
Contributor

LGTM. @mika-fischer, please add the test when you get a chance!

using node::AsyncResource::CallbackScope;
};

enum State : unsigned char { OPEN, CLOSING, CLOSED };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow these guidelines for the enum names: https://google.github.io/styleguide/cppguide.html#Enumerator_Names
The Node.js guidelines https://github.com/nodejs/node/blob/main/doc/contributing/cpp-style-guide.md are based on the Google C++ Style Guide.

@gabrielschulhof gabrielschulhof changed the title node-api: fix data race and use-after-free in napi_threadsafe_function [WIP] node-api: fix data race and use-after-free in napi_threadsafe_function Jan 31, 2025
@gabrielschulhof gabrielschulhof marked this pull request as ready for review January 31, 2025 17:02
@gabrielschulhof
Copy link
Contributor

Marking as ready for review so the tests will run.

gabrielschulhof and others added 3 commits January 31, 2025 09:03
Other threads can still hold a valid handle to the tsfn after
finalization if finalization was triggered by
- release with napi_tsfn_abort, or
- environment shutdown

Handle this by:
- protecting finalization itself with the mutex
- if necessary, delay deletion after finalization to when thread_count
  drops to 0
- releasing all resources as soon as possible before deletion

Fixes: nodejs#55706
@mika-fischer
Copy link
Contributor Author

Marking as ready for review so the tests will run.

@gabrielschulhof Thanks for pushing this further, and sorry for not following up!

FYI The test case only triggers reliably under valgrind. Otherwise it crashes only occasionally. I don't know if anthing special is needed or if all tests are run with valgrind anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API.
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

napi_threadsafe_function is very hard to use safely
5 participants