Node.js worker_threads hang under high CPU when using SharedArrayBuffer + Atomics.notify in clustered app
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
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
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
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
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?
Verified community troubleshooting
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.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Programming23% match
Node.js worker_threads leak file descriptors when using fs.watch on large directories leading to EMFILE and process OOM
Open0 answers - Servers21% match
Node.js + PM2 server exhausts ephemeral ports (TIME_WAIT) during traffic bursts causing upstream connection failures and 502s
Open0 answers - Servers21% match
PM2-managed Node.js behind NGINX exhausts ephemeral ports (TIME_WAIT spike) during traffic bursts causing intermittent 502s
Open1 answer - Networking21% match
Linux source‑based routing causes intermittent IPv6 HTTPS failures for routed clients
Solved1 answer - Databases21% match
PostgreSQL refuses new clients with 'too many connections' after scaling Node workers
Open1 answer