From cadac312bbb354f3ebf3f56855879edbf3683a8c Mon Sep 17 00:00:00 2001 From: Akash Kumar <116457960+akashchamp@users.noreply.github.com> Date: Thu, 8 Oct 2026 03:31:57 +0530 Subject: [PATCH 1/2] jemalloc-sys: don't enable background threads on unsupported targets NO_BG_THREAD_TARGETS only produced a warning, so the background_threads feature still put background_thread:true into the default malloc_conf for musl. Return early with always_enabled set to false instead. Also add Apple targets to the list: jemalloc's configure never enables background thread support for darwin/ios, so the option makes malloc_init_hard() fail and every later allocation goes through the global init lock. Fixes #179 Signed-off-by: Akash Kumar <116457960+akashchamp@users.noreply.github.com> --- CHANGELOG.md | 5 +++++ jemalloc-sys/README.md | 3 ++- jemalloc-sys/build.rs | 12 ++++++++++++ jemalloc-sys/src/env.rs | 5 +++-- jemallocator/tests/background_thread_defaults.rs | 7 ++++++- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddd5ea6fe..7f64b2e72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Unreleased +- tikv-jemalloc-sys: the `background_threads` feature no longer puts + `background_thread:true` into the default `malloc_conf` on targets that + don't support background threads (musl, and now Apple targets). jemalloc + rejected the option there, its initialization never completed, and every + allocation took a global lock. - tikv-jemalloc-sys: expose the jemalloc 5.4.0 extent-allocation hook flags `EXTENT_ALLOC_FLAG_PINNED` and `EXTENT_ALLOC_FLAG_MASK`, documenting the low-bit protocol of the pointer returned from `extent_alloc_t` hooks. diff --git a/jemalloc-sys/README.md b/jemalloc-sys/README.md index b3fe497ae..52f7fdb57 100644 --- a/jemalloc-sys/README.md +++ b/jemalloc-sys/README.md @@ -94,7 +94,8 @@ This crate provides following cargo feature flags: process will be disabled regardless the state in parent process. See `stats.background_thread` for related stats. `opt.background_thread` can be used to set the default option. The background thread is only available on - selected pthread-based platforms. + selected pthread-based platforms; on targets where it is not supported (musl + and Apple targets) this feature has no effect. * `unprefixed_malloc_on_supported_platforms`: when disabled, configure `jemalloc` with `--with-jemalloc-prefix=_rjem_`. Enabling this causes symbols diff --git a/jemalloc-sys/build.rs b/jemalloc-sys/build.rs index 1cc133163..b6c4a4772 100644 --- a/jemalloc-sys/build.rs +++ b/jemalloc-sys/build.rs @@ -509,6 +509,18 @@ impl BackgroundThreadSupport { "`background_threads_runtime_support` not supported for `{}`", target ); + if always_enabled { + // jemalloc rejects `background_thread:true` on these targets + // and its initialization never completes, so don't put it in + // the default `malloc_conf`. + warning!( + "`background_threads` not supported for `{}`; not enabling background threads by default", + target + ); + } + return Some(Self { + always_enabled: false, + }); } Some(Self { always_enabled }) diff --git a/jemalloc-sys/src/env.rs b/jemalloc-sys/src/env.rs index 88703d359..22f212aeb 100644 --- a/jemalloc-sys/src/env.rs +++ b/jemalloc-sys/src/env.rs @@ -11,8 +11,9 @@ pub static UNSUPPORTED_TARGETS: &[&str] = &[ /// `jemalloc-sys` is not tested on these targets in CI: pub static UNTESTED_TARGETS: &[&str] = &["openbsd", "msvc"]; -/// `jemalloc`'s background_thread support is known not to work on these targets: -pub static NO_BG_THREAD_TARGETS: &[&str] = &["musl"]; +/// `jemalloc`'s background_thread support is known not to work on these targets +/// (on Apple targets jemalloc's `configure` never enables it): +pub static NO_BG_THREAD_TARGETS: &[&str] = &["musl", "apple"]; /// targets that don't support unprefixed `malloc` // “it was found that the `realpath` function in libc would allocate with libc malloc diff --git a/jemallocator/tests/background_thread_defaults.rs b/jemallocator/tests/background_thread_defaults.rs index 0fa7798c8..65244664a 100644 --- a/jemallocator/tests/background_thread_defaults.rs +++ b/jemallocator/tests/background_thread_defaults.rs @@ -19,5 +19,10 @@ fn background_threads_runtime_defaults() { return; } - assert_eq!(background_threads(), cfg!(feature = "background_threads")); + // `background_threads` is ignored on targets in `NO_BG_THREAD_TARGETS`. + let supported = !cfg!(target_env = "musl") && !cfg!(target_vendor = "apple"); + assert_eq!( + background_threads(), + cfg!(feature = "background_threads") && supported + ); } From 83f79d3acd2e1140ed4ce96babeb5f1dffbaf739 Mon Sep 17 00:00:00 2001 From: Akash Kumar <116457960+akashchamp@users.noreply.github.com> Date: Sat, 10 Oct 2026 01:08:13 +0530 Subject: [PATCH 2/2] jemalloc-sys: don't treat musl as lacking background thread support The missing pthread_create symbol that required musl in NO_BG_THREAD_TARGETS was fixed in jemalloc 5.2.0. Keep only Apple targets in the list and drop the musl test gates. Signed-off-by: Akash Kumar <116457960+akashchamp@users.noreply.github.com> --- CHANGELOG.md | 2 +- jemalloc-sys/README.md | 4 ++-- jemalloc-sys/src/env.rs | 4 ++-- jemallocator/tests/background_thread_defaults.rs | 2 +- jemallocator/tests/background_thread_enabled.rs | 1 - 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f64b2e72..c3a853191 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ - tikv-jemalloc-sys: the `background_threads` feature no longer puts `background_thread:true` into the default `malloc_conf` on targets that - don't support background threads (musl, and now Apple targets). jemalloc + don't support background threads, which now include Apple targets. jemalloc rejected the option there, its initialization never completed, and every allocation took a global lock. - tikv-jemalloc-sys: expose the jemalloc 5.4.0 extent-allocation hook flags diff --git a/jemalloc-sys/README.md b/jemalloc-sys/README.md index 52f7fdb57..7bc92a759 100644 --- a/jemalloc-sys/README.md +++ b/jemalloc-sys/README.md @@ -94,8 +94,8 @@ This crate provides following cargo feature flags: process will be disabled regardless the state in parent process. See `stats.background_thread` for related stats. `opt.background_thread` can be used to set the default option. The background thread is only available on - selected pthread-based platforms; on targets where it is not supported (musl - and Apple targets) this feature has no effect. + selected pthread-based platforms; on targets where it is not supported (Apple + targets) this feature has no effect. * `unprefixed_malloc_on_supported_platforms`: when disabled, configure `jemalloc` with `--with-jemalloc-prefix=_rjem_`. Enabling this causes symbols diff --git a/jemalloc-sys/src/env.rs b/jemalloc-sys/src/env.rs index 22f212aeb..ab23385c7 100644 --- a/jemalloc-sys/src/env.rs +++ b/jemalloc-sys/src/env.rs @@ -12,8 +12,8 @@ pub static UNSUPPORTED_TARGETS: &[&str] = &[ pub static UNTESTED_TARGETS: &[&str] = &["openbsd", "msvc"]; /// `jemalloc`'s background_thread support is known not to work on these targets -/// (on Apple targets jemalloc's `configure` never enables it): -pub static NO_BG_THREAD_TARGETS: &[&str] = &["musl", "apple"]; +/// (jemalloc's `configure` never enables it on Apple targets): +pub static NO_BG_THREAD_TARGETS: &[&str] = &["apple"]; /// targets that don't support unprefixed `malloc` // “it was found that the `realpath` function in libc would allocate with libc malloc diff --git a/jemallocator/tests/background_thread_defaults.rs b/jemallocator/tests/background_thread_defaults.rs index 65244664a..af5710641 100644 --- a/jemallocator/tests/background_thread_defaults.rs +++ b/jemallocator/tests/background_thread_defaults.rs @@ -20,7 +20,7 @@ fn background_threads_runtime_defaults() { } // `background_threads` is ignored on targets in `NO_BG_THREAD_TARGETS`. - let supported = !cfg!(target_env = "musl") && !cfg!(target_vendor = "apple"); + let supported = !cfg!(target_vendor = "apple"); assert_eq!( background_threads(), cfg!(feature = "background_threads") && supported diff --git a/jemallocator/tests/background_thread_enabled.rs b/jemallocator/tests/background_thread_enabled.rs index 76d286d6d..c61e706e3 100644 --- a/jemallocator/tests/background_thread_enabled.rs +++ b/jemallocator/tests/background_thread_enabled.rs @@ -2,7 +2,6 @@ //! library was compiled with background thread run-time support. #![cfg(feature = "background_threads_runtime_support")] #![cfg(not(feature = "unprefixed_malloc_on_supported_platforms"))] -#![cfg(not(target_env = "musl"))] use tikv_jemallocator::Jemalloc;