From e3a630a14761ebf02f9484005566480dadfd1d17 Mon Sep 17 00:00:00 2001 From: "peco-engineer-bot[bot]" <3815206+peco-engineer-bot[bot]@users.noreply.github.com> Date: Wed, 9 Sep 2026 09:52:00 +0000 Subject: [PATCH] =?UTF-8?q?c-abi:=20thread=5Fstack=5Fsize(2=20MiB)=20is=20?= =?UTF-8?q?a=20cap,=20not=20a=20floor=20=E2=80=94=20it=20shrinks=20workers?= =?UTF-8?q?=20when=20RUST=5FMIN=5FSTACK=20is=20larger=20(#328)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: peco-engineer-bot[bot] <3815206+peco-engineer-bot[bot]@users.noreply.github.com> --- src/c_abi/mod.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/c_abi/mod.rs b/src/c_abi/mod.rs index 22df48ae..c988e110 100644 --- a/src/c_abi/mod.rs +++ b/src/c_abi/mod.rs @@ -87,6 +87,29 @@ static RUNTIME: OnceLock = OnceLock::new(); // from shrinking the kernel-owned workers below Tokio's supported default. const C_ABI_RUNTIME_THREAD_STACK_SIZE: usize = 2 * 1024 * 1024; +/// Resolve the worker-thread stack size for the C-ABI runtime from the ambient +/// `RUST_MIN_STACK` value (parsed to `Some(bytes)`, or `None` when unset or +/// unparseable). +/// +/// [`C_ABI_RUNTIME_THREAD_STACK_SIZE`] (2 MiB) is a **floor, not a cap**: a +/// too-small ambient `RUST_MIN_STACK` is clamped up to the floor (preserving +/// databricks/databricks-sql-kernel#326), while a host that deliberately asked +/// for *more* than 2 MiB keeps its larger stack rather than being silently +/// shrunk. +fn c_abi_runtime_thread_stack_size(ambient_rust_min_stack: Option) -> usize { + ambient_rust_min_stack + .unwrap_or(0) + .max(C_ABI_RUNTIME_THREAD_STACK_SIZE) +} + +/// Read and parse the ambient `RUST_MIN_STACK` env var into `Some(bytes)`, or +/// `None` when it is unset or not a valid `usize`. +fn ambient_rust_min_stack() -> Option { + std::env::var("RUST_MIN_STACK") + .ok() + .and_then(|v| v.parse::().ok()) +} + /// Fetch (initialising on first use) the process-wide C-ABI runtime. /// /// A multi-thread runtime so concurrent FFI calls from different host @@ -107,7 +130,7 @@ pub(crate) fn runtime() -> crate::kernel_error::Result<&'static tokio::runtime:: let built = tokio::runtime::Builder::new_multi_thread() .enable_all() .thread_name("kernel-c-abi") - .thread_stack_size(C_ABI_RUNTIME_THREAD_STACK_SIZE) + .thread_stack_size(c_abi_runtime_thread_stack_size(ambient_rust_min_stack())) .build() .map_err(|e| { crate::kernel_error::Error::internal() @@ -438,6 +461,38 @@ mod tests { const SMALL_STACK_CHILD: &str = "DATABRICKS_KERNEL_SMALL_STACK_CHILD"; + /// Floor-not-cap (the bug in #328): when the ambient `RUST_MIN_STACK` is + /// LARGER than the 2 MiB floor, the resolved worker stack size must + /// preserve the larger value rather than shrinking workers to 2 MiB. The + /// databricks-odbc partner runs with `RUST_MIN_STACK=3194880` and must not + /// have its C-ABI workers silently reduced. + #[test] + fn c_abi_runtime_stack_size_preserves_larger_rust_min_stack() { + let ambient = 3_194_880; // ulimit -s 3120 * 1024, the partner's value + assert!(ambient > C_ABI_RUNTIME_THREAD_STACK_SIZE); + assert_eq!( + c_abi_runtime_thread_stack_size(Some(ambient)), + ambient, + "a RUST_MIN_STACK larger than the 2 MiB floor must be preserved, not shrunk" + ); + } + + /// Floor preserved (the #326 behavior the fix must keep): a too-small or + /// unset ambient `RUST_MIN_STACK` is clamped UP to the 2 MiB floor. + #[test] + fn c_abi_runtime_stack_size_clamps_small_rust_min_stack_up_to_floor() { + assert_eq!( + c_abi_runtime_thread_stack_size(Some(16_384)), + C_ABI_RUNTIME_THREAD_STACK_SIZE, + "a RUST_MIN_STACK below the floor must clamp up to 2 MiB" + ); + assert_eq!( + c_abi_runtime_thread_stack_size(None), + C_ABI_RUNTIME_THREAD_STACK_SIZE, + "an unset RUST_MIN_STACK must default to the 2 MiB floor" + ); + } + /// The C ABI owns its runtime, so an embedding process's `RUST_MIN_STACK` /// setting must not be able to shrink the runtime workers below Tokio's /// supported default. Run the actual probe in a subprocess because