Programming

Node.js worker_threads hang under high CPU when using SharedArrayBuffer + Atomics.notify in clustered app

pixelpelle
1mo ago
Environment

Environment

Node.js 18.15 on x86_64 Linux, app uses cluster to spawn worker processes, each worker creates multiple worker_threads and shares a SharedArrayBuffer for counters and notifications using Atomics.wait/notify. High-concurrency traffic from HTTP clients triggers CPU near 100% on some cores and intermittent deadlocks observed.

Symptoms

Symptoms

Under sustained load, some worker threads stop responding: HTTP requests queue, event loop appears stalled in worker process, Node inspector shows threads blocked waiting on Atomics. CPU stays high; kill -USR2 stack dump shows threads in Atomics.wait; master process remains responsive.

Observations

Observations

Heap and event loop metrics don't show leaks; replacing Atomics.wait with simple busy-loop reduces deadlocks but increases CPU. Reproduced with a small stress test: repeating increment+notify sequences occasionally never wake Atomic waiters. Using postMessage between threads avoids the hang but has higher latency.

Already attempted

Already attempted

Increased worker thread count, throttled incoming requests, switched to message-passing for some signals, upgraded Node minor version — issue persists intermittently. Tried removing cluster and running single process with worker_threads — hang still appears but less frequent.

Question

Question

How to safely coordinate high-frequency notifications across worker_threads/SAB without deadlocks on Node.js? Is Atomics.wait/notify unreliable under cluster/high CPU, and what patterns avoid hangs?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
feliciafix Answer
user
Posted 1mo ago • 0 confirmations

If using Atomics.wait/notify, the most common mistake is a lost wakeup: notify happens before wait is sleeping or using a different index. First, ensure every waiter uses the exact same TypedArray and index and implement a sequence counter so notifyers loop until the expected sequence is observed. Second, never block the thread that's responsible for notifying; blocking native work can starve notifications under CPU pressure. If reliability matters more than raw latency, switch to worker.postMessage() between threads or use Atomics.wait with a bounded timeout and fallback async checks. Verify by creating a small stress test reproducing the hang; if it still happens capture thread stacks with --inspect and reduce to a minimal repro before changing Node versions. Rollback by reverting to message-passing if SAB changes worsen behavior.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Race conditions / lost wakeups due to calling Atomics.notify before waiter calls Atomics.wait or incorrect index offsets in shared buffer. 90%
Notifier thread gets CPU-starved (blocked in native/JS work) and cannot call Atomics.notify, leaving waiters blocked during high CPU. 70%
Node.js runtime bug in worker_threads/Atomics under heavy load causing missed notifications (less likely but possible across versions). 25%

Summary

Blocking Atomics.wait is correct API but can deadlock if notifications targeted to wrong index, if waiters are interrupted, or if the main thread that should call Atomics.notify is itself blocked. High CPU can starve notifier threads if busy loops or blocking native code exist. The observed stacks in Atomics.wait suggest genuine waits not being notified; likely causes include incorrect use of indexes/offsets, race conditions where notify occurs before waiter enters wait (lost wakeups), or worker_threads being starved by CPU saturation or a bug in bindings. Message-passing avoids shared-memory races, which explains why it reduces hangs.

Suggested Fixes

Diagnostic step 1
Audit atomic usage: ensure Atomics.wait and Atomics.notify use the exact same typed array and index, and design protocol so notifiers loop and notify until expected state observed to avoid lost wakeups.
Diagnostic step 2
Avoid blocking the thread that should call Atomics.notify. Prefer worker.postMessage for coordination where possible; if SAB is required, make notify logic resilient (e.g., use sequence counters and retry loops with timeouts).
Diagnostic step 3
Create a minimal repro that reproduces the hang; run under --inspect to capture thread stacks and report Node versions if a runtime bug is suspected. Gradually increase load to identify threshold.

Continue troubleshooting

Matched from the title, description and tags