Skip to content

Deadlock: all threads permanently blocked due to lock ordering violation between allocator and thread pool (macOS arm64, Bun 1.3.5 SEA) #26762

Closed

Description

@l30t

What version of Bun is running?

1.3.5 (Single Executable Application / --compile build)

What platform is your computer?

Darwin 25.2.0 arm64 arm (macOS 26.2, Apple Silicon)

What steps can reproduce the bug?

What steps can reproduce the bug?

We have not been able to produce a minimal reproduction yet. The deadlock occurs non-deterministically in a long-running Node.js-compatible CLI application compiled with bun build --compile. The application:

  • Runs an event loop that spawns child processes (via child_process), performs HTTP requests, and reads/writes files
  • Uses Bun's built-in thread pool for async I/O
  • Typically runs for 10–60 minutes before the deadlock triggers (when it does)

The deadlock appears to be a race condition in the interaction between Bun's GeneralPurposeAllocator and the thread pool completion/drain mechanism. It likely requires specific timing where a pool thread holds the allocator lock while the main thread is draining completed tasks.

What is the expected behavior?

The process should continue executing. Async tasks should complete and return results to the event loop.

What do you see instead?

The process freezes permanently. All threads are blocked on lock acquisition with zero CPU usage. The process never recovers and must be killed with kill -9.

Externally, the application's TUI shows a tool execution stuck at "Pending..." indefinitely. No new log output is produced. lsof shows no outbound network connections — only a dangling LISTEN socket.

Additional information

Detailed Analysis

We attached lldb and used macOS sample to capture the full thread state of the frozen process. Below is the complete analysis.

Thread State Summary

Thread Name State Blocked On
#1 Main thread __ulock_wait2 Lock A (allocator, heap)
#2–#13 "Bun Pool" workers (x12) __ulock_wait2 Lock B (pool batch, BSS global)
#14 HTTP client kevent64 Idle (no work to do)
#15 File watcher __ulock_wait2 Lock (likely allocator-related)

Every thread capable of making progress is blocked. The HTTP client thread is idle in kevent64 but cannot receive work because the main thread is deadlocked.

The Two Locks

Lock A — Allocator lock (heap-resident, os_unfair_lock):

Address: 0x4e936ac2890 (heap object + 0x890)
Value:   0x00004002 (locked, with waiters)
Holder:  A pool thread (inferred from deadlock cycle)
Waiter:  Main thread

Lock B — Thread pool batch lock (BSS-resident, futex-style):

Address: 0x10434ff74 (__DATA.__bss + 609076)
Value:   0x00000001 (locked)
Holder:  Main thread's drain context (inferred)
Waiters: All 12 Bun Pool threads + file watcher

Deadlock Cycle

Main Thread                          Pool Thread
───────────                          ───────────
Draining completed async tasks       Executing async task
  │                                    │
  ├─ Acquires drain locks             ├─ Acquires Lock A (allocator)
  │  (obj+0x210, +0x88, +0xE0, +0x68) │  (to allocate/free memory)
  │                                    │
  ├─ Calls deallocator (free)         ├─ Tries to acquire Lock B
  │  which needs Lock A ──► BLOCKED   │  (to post completion) ──► BLOCKED
  │                                    │
  └─ Waiting on Lock A                └─ Waiting on Lock B
     (held by pool thread)               (requires main thread to drain)
  1. The main thread enters the drain path to process completed async tasks. It acquires several drain-related locks, then calls into the allocator to free task memory. The allocator's os_unfair_lock (Lock A) is already held by a pool thread → main thread blocks.

  2. A pool thread holds Lock A (acquired during memory allocation for task results) and tries to acquire Lock B (the pool batch lock) to post its completion or pick up new work. Lock B is effectively held by the drain context → pool thread blocks.

  3. The remaining 11 pool threads all try to acquire Lock B for their own work dispatch and also block.

  4. Result: permanent deadlock. No thread can make progress.

Call Stacks (from lldb)

Main thread:

frame #0: __ulock_wait2                    ← waiting for Lock A
frame #1: _os_unfair_lock_lock_slow
frame #2: <unnamed symbol> +144            ← allocator free/dealloc
frame #3: <unnamed symbol> +120            ← list node removal
frame #4: <unnamed symbol> +152            ← drain path (holds drain locks)
frame #5: <unnamed symbol> +76             ← flush/drain entry (acquired lock at obj+0x210)
frame #6: <unnamed symbol> +12             ← JS callback dispatch
frame #7: <unnamed symbol> +108            ← event loop tick
frame #8: <JIT code>

Each of the 12 "Bun Pool" threads:

frame #0: __ulock_wait2                    ← waiting for Lock B
frame #1: <unnamed symbol> +80             ← futex wait wrapper
frame #2: <unnamed symbol> +136            ← CAS-based lock acquisition
frame #3: <unnamed symbol> +148            ← work dispatch
frame #4: <unnamed symbol> +1136           ← pool worker main loop ("Bun Pool")

The thread name "Bun Pool" was confirmed by finding the string literal at offset +120 in the worker entry function.

Key lldb Evidence

# Lock A value (allocator lock on heap):
(lldb) x/1wx 0x4e936ac2890
0x4e936ac2890: 0x00004002       # bit 14 set = locked with waiters

# Lock B value (pool batch lock in BSS):
(lldb) x/1wx 0x10434ff74
0x10434ff74: 0x00000001         # locked

# Lock B is a global in BSS:
(lldb) image lookup --address 0x10434ff74
Address: droid[0x103c8bf74] (__DATA.__bss + 609076)

Drain Entry Disassembly

The drain entry function acquires a lock at obj+0x210, sets a "draining" flag at obj+0x21c, then proceeds to process completions — which eventually calls into the allocator:

add  x0, x0, #0x210           ; lock at object+0x210
bl   os_unfair_lock_lock       ; acquire drain lock
ldrb w8, [x19, #0x21c]        ; check "already draining" flag
strb w8=1, [x19, #0x21c]      ; set draining flag
bl   os_unfair_lock_unlock     ; release 0x210
bl   <drain_tasks>             ; process completions → eventually needs allocator lock

macOS sample Output (summary)

Sort by top of stack:
    __ulock_wait2  (in libsystem_kernel.dylib)        <majority of samples>
    kevent64       (in libsystem_kernel.dylib)        <HTTP client idle>

All sampled time was spent in __ulock_wait2, confirming every thread was blocked on lock acquisition with zero useful work being done.

Root Cause

This is a lock ordering violation between two subsystems in the Bun runtime:

  1. Zig's GeneralPurposeAllocator — uses os_unfair_lock on macOS for thread-safe allocation/deallocation
  2. Bun's thread pool completion mechanism — uses a futex-style lock (CAS + __ulock_wait2) for task dispatch and completion posting

The two subsystems acquire their respective locks in inconsistent orders depending on the code path:

  • Drain path (main thread): drain locks → allocator lock
  • Pool worker path: allocator lock → pool batch lock (→ implicitly needs drain to release)

This creates a cycle that, under the right timing, results in a permanent deadlock.

Relationship to Previous Issues

This may be related to #9844 ("await Promise.all hangs when awaiting multiple processes"), which exhibited similar symptoms on macOS arm64:

  • All threads blocked on __ulock_wait / __psynch_cvwait
  • Introduced in Bun v1.0.34, reported fixed in v1.1.5
  • The reporter's sample output showed the same pattern of pool threads stuck in lock wait

Our deadlock occurs on Bun 1.3.5, suggesting either a regression of the same underlying issue or a different code path triggering the same class of lock ordering bug.

Suggested Fix

  1. Replace GeneralPurposeAllocator with a lock-free allocator (e.g., mimalloc or jemalloc) that does not hold a global lock during free/dealloc. This would eliminate Lock A from the cycle entirely.
  2. Defer deallocation outside the drain critical section — instead of freeing task memory while holding drain locks, queue freed pointers and release them after the drain locks are dropped.
  3. Use a separate allocator for thread pool task data that doesn't share a lock with the main event loop's allocator.
  4. Establish and enforce a lock ordering discipline — e.g., always acquire the allocator lock before any pool synchronization lock, never the reverse.

Activity

  1. added a commit that references this issue on Apr 13, 2026
    ccbaed9
  2. added a commit that references this issue on Apr 25, 2026
    3b96c08
  3. added a commit that references this issue on Apr 26, 2026
  4. added a commit that references this issue on May 14, 2026
    8db2ef5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions