Skip to content

n-api: fix dead lock if multi push events #58724

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

milkpotatoes
Copy link

@milkpotatoes milkpotatoes commented Jun 16, 2025

fix #58723

  • add waiting_task_size to record waiting push events
  • compare max_queue_size before queue.size which would be faster

@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 Jun 16, 2025
Hardanish-Singh

This comment was marked as spam.

@mhdawson
Copy link
Member

@milkpotatoes would it be possible to add a test case? That would help use see the problem more easily as well as make sure its not regressed in the future.

- add waiting_task_size to record waiting push events
- compare max_queue_size before queue.size which would be faster

Signed-off-by: milkpotatoes <chenshulin5@huawei.com>
@@ -518,6 +521,7 @@ class ThreadSafeFunction : public node::AsyncResource {
// means we don't need the mutex to read them.
void* context;
size_t max_queue_size;
size_t waiting_task_size;
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it adds an extra variable when we could also just call Signal() unconditionally with the same effect? This class already has a very large state space (one of the worst examples I've seen), I'd try to avoid another piece of state even if its usage is localized to managing cond

Copy link
Author

Choose a reason for hiding this comment

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

Yes, this approach seems acceptable. However, it may introduce unnecessary signal calls, which could incur additional performance overhead.

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: Need Triage
Development

Successfully merging this pull request may close these issues.

Thread dead lock when napi_call_threadsafe_function in some case
5 participants