Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class ThreadSafeFunction : public node::AsyncResource {
napi_status Push(void* data, napi_threadsafe_function_call_mode mode) {
node::Mutex::ScopedLock lock(this->mutex);

while (queue.size() >= max_queue_size && max_queue_size > 0 &&
while (max_queue_size > 0 && queue.size() >= max_queue_size &&
!is_closing) {
if (mode == napi_tsfn_nonblocking) {
return napi_queue_full;
Expand Down Expand Up @@ -385,7 +385,7 @@ class ThreadSafeFunction : public node::AsyncResource {
data = queue.front();
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

cond->Signal(lock);
}
size--;
Expand Down