FULL AI ANALYSIS

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

Generated Oct 22, 2026, 9:30:52 AM

Analysis 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.

Possible 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%

Incorrect lifetime management of SharedArrayBuffer leading to stale references across cluster forks causing undefined behavior.

20%

Suggested fixes

1

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.

2

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).

3

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.

4

Diagnostic step 4

As a mitigation, replace blocking Atomics.wait used in the main event loop with an async loop using setImmediate to periodically check shared state, or use a hybrid: Atomics.wait with a bounded timeout combined with periodic rechecks to prevent permanent hangs.