Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/c_abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ static RUNTIME: OnceLock<tokio::runtime::Runtime> = 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>) -> 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<usize> {
std::env::var("RUST_MIN_STACK")
.ok()
.and_then(|v| v.parse::<usize>().ok())
}

/// Fetch (initialising on first use) the process-wide C-ABI runtime.
///
/// A multi-thread runtime so concurrent FFI calls from different host
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Loading