Repository navigation
fix(c-abi): give runtime workers an explicit stack size (isolate from host RUST_MIN_STACK / ulimit -s) - #326
Conversation
Give kernel-owned Tokio workers an explicit 2 MiB stack so an embedding process cannot undersize them via ambient RUST_MIN_STACK. Add a subprocess regression that reproduces the HTTP-client stack overflow at 16 KiB. Fixes #325 Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
|
Updated the title and description after digging into the partner's core dump — no code change, so the approval still stands. Flagging it here because the original root-cause section was wrong in a way worth correcting for the record. The claim was that the databricks-odbc 0.4.0 crash was caused by a small ambient
What their limit actually is: None of that weakens the fix — it broadens it. Two other edits:
One open question on the value itself: 2 MiB restores Tokio's default, which is the defensible choice, but we don't know the real high-water mark for session open — the partner overflowed at ~21 KiB usable, which tells us nothing about the ceiling. Worth treating 2 MiB as a floor rather than a proven-sufficient number. |
|
Measured the actual peak stack usage so the 2 MiB value is an informed choice rather than an inherited default. No code changes — I instrumented externally by interposing Run against the shipped v0.4.0 RPM on Amazon Linux 2023, doing a real
Peak worker usage for session open + a query is ~67 KB — about 3% of 2 MiB. So 2 MiB is comfortably sized, with roughly 30x headroom. Not a number to feel nervous about. One finding worth surfacing, because it slightly reframes the PR: the workers in the unpatched shipped v0.4.0 already get 1.99 MB, and that is unchanged at Two consequences:
Given ~67 KB of real usage against a 2 MiB floor, I see no argument for raising the value. Worth keeping as-is. |
|
Correction to my earlier edit on this PR: I was wrong to remove the I could not symbolize the shipped stripped v0.4.0 driver the usual way — a from-source rebuild at the same pins produces a different The partner's 23 driver frames resolve to, outermost → innermost:
So the original description had the location right: reqwest building the kernel HTTP client on a Two things this does NOT change:
Still not reproduced on my side: no proxy env, |
Summary
whether that comes from
RUST_MIN_STACKor a lowulimit -s.RUST_MIN_STACK=16384.Root cause
The C ABI runtime used
Builder::new_multi_thread()withoutthread_stack_size.Tokio therefore created its workers through Rust's default thread builder, so the
worker stack size was whatever the host process dictated —
RUST_MIN_STACKwhenset, otherwise
RLIMIT_STACK(ulimit -s).The runtime is owned by the C ABI, so it now owns its stack-size choice as well.
Two MiB preserves Tokio's documented normal default for environments that do not
override it, and is taken regardless of the ambient limit (verified: an explicit
2 MiB request yields a full 2 MiB stack even under
ulimit -s 64).What is demonstrated here
A small ambient
RUST_MIN_STACKshrinks the kernel workers until session creationoverflows them. The new regression reproduces that directly: it aborts before this
change with
thread 'kernel-c-abi' has overflowed its stack.The databricks-odbc 0.4.0 partner crash — related, but NOT
RUST_MIN_STACKThis work was prompted by a partner SIGSEGV on a
kernel-c-abiworker insideSQLDriverConnectW(databricks-odbc 0.4.0, Amazon Linux 2023). Analysis of theircore dump shows the trigger was not
RUST_MIN_STACK:0x30c000(3120 KiB) with a 1-pagePROT_NONEguard. There are no small (sub-1 MiB) thread stacks. ARUST_MIN_STACKof 16–32 KiB would have produced mappings of that size; none exist.RUST_MIN_STACKis unset in their build image (verified by running the actualqueta/build-env-dockerimage).ulimit -s 3120, established by bisection in that same image:it reproduces the core's mapping byte-for-byte — a
---pguard page directlybelow a
0x30c000rw-pstack. The image's own default is 8192 KiB, so thelow limit comes from how the test is invoked, not from the image.
Measured on the shipped v0.4.0 build: the
kernel-c-abiworkers already get1.99 MiB, unchanged at
ulimit -s 8192and atulimit -s 3120. Tokio 1.53.1leaves
thread_stack_size: None, so Rust's std default (2 MiB) applies, and thatdefault is not derived from
RLIMIT_STACK— onlyRUST_MIN_STACKoverrides it.So this change would not by itself have prevented the partner crash: their
workers already had 2 MiB, and the crashing thread still had only ~21 KiB of
usable stack inside its 3120 KiB mapping (a normal ~21 KiB frame chain at the
mapping floor, with the 3.02 MiB above it never written). Whatever produced that
~21 KiB window is host-side and still unexplained — nothing in the kernel, the ODBC
driver, or the vendored Arrow framework sets a stack size — and it is tracked
separately.
What this change does buy, and why it is still worth landing:
RUST_MIN_STACKfrom shrinking kernel-owned workers(the new regression proves this path).
silently move it.
Is 2 MiB the right number?
Measured peak stack usage across a real
SQLDriverConnect+SELECT 1+ fetch, bypainting each thread's stack and scanning for the deepest disturbed byte while the
threads were still alive: the busiest
kernel-c-abiworker peaked at 66.6 KiB,the next two at 66.4 KiB, one at 31.2 KiB, and the remaining ~18 workers at
0–4.8 KiB. The ODBC caller thread peaked at 58.0 KiB.
Peak worker usage is therefore ~67 KiB — about 3% of 2 MiB, roughly 30x
headroom. 2 MiB is comfortably sized and needs no adjustment.
Test plan
thread 'kernel-c-abi' has overflowed its stack.cargo test --lib c_abi::tests::c_abi_runtime_ignores_ambient_rust_min_stack -- --exact --nocapturecargo test(all non-ignored unit, integration, and doc tests pass)cargo clippy --all-targets -- -D warningscargo fmt --allfull 2 MiB worker stack under both
ulimit -s 3120andulimit -s 64.LD_PRELOADpthread_createinterposer that paints each stack and scans it while thethreads are live; confirms 2 MiB has ~30x headroom.
Closes #325