Skip to content

fork: take the meta-data lock before the arena reserve lock in the fork handlers - #33

Merged
Jarred-Sumner merged 1 commit into
bun-dev3-v2from
jarred/fork-lock-order
Sep 3, 2026
Merged

Jarred-Sumner merged 1 commit into
bun-dev3-v2from
jarred/fork-lock-order

Conversation

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

A meta-data allocation holds theap_meta_lock and can need a fresh arena, which takes arena_reserve_lock (mi_arenas_try_alloc). _mi_process_fork_prepare acquired them in the opposite order, so a fork() racing a meta allocation that had to reserve an arena could deadlock. Nothing under the reserve lock allocates meta data, so meta → reserve is the consistent order; the prepare/parent handlers now use it.

…rk handlers

A meta-data allocation (`_mi_meta_zalloc*`) holds `theap_meta_lock` and can need a fresh arena,
which takes `arena_reserve_lock` (`mi_arenas_try_alloc`). The fork prepare handler acquired
them the other way round, so a `fork()` racing a meta allocation that had to reserve an arena
could deadlock. Nothing under the reserve lock allocates meta data, so meta -> reserve is the
one consistent order; the handlers now use it.

Also `mi_theap_zalloc_csize` returned non-zeroed memory for sizes above MI_SMALL_SIZE_MAX
(it called `mi_theap_malloc` on that branch; upstream f60b4cf).
@Jarred-Sumner
Jarred-Sumner merged commit 6a64e1b into bun-dev3-v2 Sep 3, 2026
14 of 15 checks passed
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Sep 3, 2026
Bumps oven-sh/mimalloc from 942b834 to 6a64e1b (12 commits).

- Allocate the per-bin abandoned-page bitmaps on first abandon
(oven-sh/mimalloc#32)
- A heap being released abandons its pages unmapped
- Order the heap delete walk against concurrent frees
- Take the meta-data lock before the arena reserve lock in the fork
handlers (oven-sh/mimalloc#33)
- Tests for heap release on many threads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant