Skip to content

fix(sys): disable mimalloc v3 FIXED_SLOT TLS on macOS#67

Merged
Brooooooklyn merged 1 commit into
napi-rs:mainfrom
shulaoda:fix/macos-mi-has-tls-slot
May 16, 2026
Merged

fix(sys): disable mimalloc v3 FIXED_SLOT TLS on macOS#67
Brooooooklyn merged 1 commit into
napi-rs:mainfrom
shulaoda:fix/macos-mi-has-tls-slot

Conversation

@shulaoda
Copy link
Copy Markdown
Contributor

mimalloc v3 on macOS defaults to MI_TLS_MODEL_FIXED_SLOT, which stores the per-thread theap pointer in TCB[108]/[109] via inline assembly, bypassing pthread_setspecific. The slot is hardcoded and shared across all mimalloc instances in the same process.

In Node.js processes that dlopen multiple napi addons each statically linking mimalloc-safe (e.g. a CLI binding plus a separately loaded rolldown binding), each instance writes its own theap to the same TCB slot, corrupting the other's heap metadata. On tokio runtimes the most visible symptom is BlockingTask's Option discriminant byte getting clobbered, panicking with "blocking task ran twice".

Define MI_HAS_TLS_SLOT=0 for macOS targets when the v3 source tree is selected. This makes the platform cascade in prim.h skip the fixed-slot branch and fall through to MI_TLS_MODEL_DYNAMIC_PTHREADS, which uses pthread_getspecific — each dylib gets its own pthread_key, so multiple mimalloc instances coexist safely. The cost is an indirect function call per default-heap access (~3 ns -> ~10 ns); negligible in practice.

See c_src/mimalloc3/include/mimalloc/prim.h:345-349 caveat and the "@apple: it would be great to get 2 official slots for custom allocators" comment at line 374.

@shulaoda shulaoda force-pushed the fix/macos-mi-has-tls-slot branch from 34caa61 to a866ae2 Compare May 15, 2026 16:01
@Brooooooklyn Brooooooklyn marked this pull request as ready for review May 16, 2026 03:42
@shulaoda shulaoda force-pushed the fix/macos-mi-has-tls-slot branch from a866ae2 to ab9477f Compare May 16, 2026 04:02
@Brooooooklyn Brooooooklyn merged commit d2d5331 into napi-rs:main May 16, 2026
19 checks passed
This was referenced May 16, 2026
Boshen pushed a commit to oxc-project/oxc that referenced this pull request May 17, 2026
See napi-rs/mimalloc-safe#67 &
napi-rs/mimalloc-safe#68

If `oxlint` and `oxfmt` plan to adopt `mimalloc v3` in the future, this
upgrade would be highly worthwhile and could bring meaningful
performance and memory management improvements.
shulaoda added a commit to rolldown/rolldown that referenced this pull request May 17, 2026
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.

2 participants