Skip to content

Comments

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

Open
milkpotatoes wants to merge 1 commit intonodejs:mainfrom
milkpotatoes:fix-ntf-tsfn-full
Open

n-api: fix dead lock if multi push events#58724
milkpotatoes wants to merge 1 commit intonodejs:mainfrom
milkpotatoes:fix-ntf-tsfn-full

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.

@legendecas legendecas moved this from Need Triage to In Progress in Node-API Team Project Jul 4, 2025
- 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>
@KevinEady KevinEady self-assigned this Aug 22, 2025
queue.pop();
popped_value = true;
if (size == max_queue_size && max_queue_size > 0) {
if (max_queue_size > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Why was size == max_queue_size removed? Can you clarify / elaborate?

Copy link
Author

Choose a reason for hiding this comment

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

This is a bug, due to thread awake could be slower than event loop thread gets the lock, tasks would be executing faster than send.
This will cause threads into sleeping unlimited.

  1. max_queue_size is set (.e.g, 3).
  2. queue is full.
  3. multi thread try to send task in one time, count of threads (.e.g, 3) is max_queue_size or more.
  4. threads will be waiting for signal in the time.
  5. task start to execute, pop the first data. At the time, queue size is 3, send a signal. The thread would not awake immediately. queue size--(by pop)
  6. unlock, and execute task, the task maybe executed faster than thread awake.
  7. execute next task, queue size will be 1.
  8. thread awake and push -> queue size: 2
  9. After that, other threads will never receive s signal until queue is full again or into release process.

code of node in current head (log added)

elplgfrn pr2

code of modules

hmhvnlgc xyk

run log

svm3bvhk 2ja

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.

Thread dead lock when napi_call_threadsafe_function in some case

6 participants