Skip to content

fix(c-abi): give runtime workers an explicit stack size (isolate from host RUST_MIN_STACK / ulimit -s) - #326

Merged
vikrantpuppala merged 1 commit into
mainfrom
fix/c-abi-runtime-stack-size
Sep 9, 2026
Merged

vikrantpuppala merged 1 commit into
mainfrom
fix/c-abi-runtime-stack-size

Conversation

@vikrantpuppala

@vikrantpuppala vikrantpuppala commented Sep 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Give the C ABI's process-wide Tokio runtime an explicit 2 MiB worker stack.
  • Stop kernel-owned workers from inheriting the embedding process's stack sizing —
    whether that comes from RUST_MIN_STACK or a low ulimit -s.
  • Add a subprocess regression that builds the HTTP client on a C-ABI worker with
    RUST_MIN_STACK=16384.

Root cause

The C ABI runtime used Builder::new_multi_thread() without thread_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_STACK when
set, 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_STACK shrinks the kernel workers until session creation
overflows 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_STACK

This work was prompted by a partner SIGSEGV on a kernel-c-abi worker inside
SQLDriverConnectW (databricks-odbc 0.4.0, Amazon Linux 2023). Analysis of their
core dump shows the trigger was not RUST_MIN_STACK:

  • All 32 thread-stack mappings in the core are 0x30c000 (3120 KiB) with a 1-page
    PROT_NONE guard. There are no small (sub-1 MiB) thread stacks. A
    RUST_MIN_STACK of 16–32 KiB would have produced mappings of that size; none exist.
  • RUST_MIN_STACK is unset in their build image (verified by running the actual
    queta/build-env-docker image).
  • Their limit is ulimit -s 3120, established by bisection in that same image:
    it reproduces the core's mapping byte-for-byte — a ---p guard page directly
    below a 0x30c000 rw-p stack. The image's own default is 8192 KiB, so the
    low limit comes from how the test is invoked, not from the image.

Measured on the shipped v0.4.0 build: the kernel-c-abi workers already get
1.99 MiB
, unchanged at ulimit -s 8192 and at ulimit -s 3120. Tokio 1.53.1
leaves thread_stack_size: None, so Rust's std default (2 MiB) applies, and that
default is not derived from RLIMIT_STACK — only RUST_MIN_STACK overrides 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:

  • It stops a small ambient RUST_MIN_STACK from shrinking kernel-owned workers
    (the new regression proves this path).
  • It pins the worker stack size so a future tokio/std default change cannot
    silently move it.

Is 2 MiB the right number?

Measured peak stack usage across a real SQLDriverConnect + SELECT 1 + fetch, by
painting each thread's stack and scanning for the deepest disturbed byte while the
threads were still alive: the busiest kernel-c-abi worker 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

  • Confirmed the new regression aborts before the fix with
    thread 'kernel-c-abi' has overflowed its stack.
  • cargo test --lib c_abi::tests::c_abi_runtime_ignores_ambient_rust_min_stack -- --exact --nocapture
  • cargo test (all non-ignored unit, integration, and doc tests pass)
  • cargo clippy --all-targets -- -D warnings
  • cargo fmt --all
  • Measured in the partner's own build image that an explicit 2 MiB request yields a
    full 2 MiB worker stack under both ulimit -s 3120 and ulimit -s 64.
  • Measured real peak worker stack usage (~67 KiB) via an LD_PRELOAD
    pthread_create interposer that paints each stack and scans it while the
    threads are live; confirms 2 MiB has ~30x headroom.

Closes #325

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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

@vikrantpuppala vikrantpuppala changed the title fix(c-abi): isolate runtime stacks from RUST_MIN_STACK fix(c-abi): give runtime workers an explicit stack size (isolate from host RUST_MIN_STACK / ulimit -s) Sep 8, 2026
@vikrantpuppala

Copy link
Copy Markdown
Collaborator Author

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 RUST_MIN_STACK. Two independent measurements say otherwise:

  1. The core has no small thread stacks. All 32 thread-stack mappings are 0x30c000 (3120 KiB) with a 1-page PROT_NONE guard. A RUST_MIN_STACK of 16–32 KiB would have produced mappings of that size — none exist.
  2. RUST_MIN_STACK is unset in their image. I pulled and ran the actual queta/build-env-docker image to check.

What their limit actually is: ulimit -s 3120, found by bisection in that same image. It reproduces the core's layout byte-for-byte (---p guard directly below a 0x30c000 rw-p stack). The image's own default is 8192 KiB, so the low limit comes from how the test is invoked, not from the image itself.

None of that weakens the fix — it broadens it. RUST_MIN_STACK and a low ulimit -s are two paths into the same defect (workers inheriting host stack sizing), and thread_stack_size closes both. I verified in their image that an explicit 2 MiB request yields a full 2 MiB stack under ulimit -s 3120 and under a hostile ulimit -s 64. Hence the broader title.

Two other edits:

  • Dropped the "symbolizes to std::env::var_os / hyper-util proxy discovery" line. I couldn't reproduce trustworthy symbolization of that stripped v0.4.0 binary — offsets shift with the kernel pin, and my own attempt produced confident but wrong names. Happy to restore it if it came from a build at 9ecd357.
  • Recorded one still-unexplained detail: the crashing thread's usable stack was only ~21 KiB inside its 3120 KiB mapping (a normal ~21 KiB frame chain at the mapping floor, 3.02 MiB above it never written). Nothing in the kernel, the ODBC driver, or the vendored Arrow framework sets a stack size, so that sizing decision is host-side — and an explicit thread_stack_size makes us immune to it either way.

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.

@vikrantpuppala

Copy link
Copy Markdown
Collaborator Author

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 pthread_create via LD_PRELOAD, painting each new thread's stack with a known byte pattern, and scanning for the deepest disturbed byte while the threads are still alive (tokio workers never exit, so an at-exit hook reports nothing).

Run against the shipped v0.4.0 RPM on Amazon Linux 2023, doing a real SQLDriverConnect + SELECT 1 + fetch:

thread stack high-water
kernel-c-abi (busiest) 1.99 MB 66.6 KB
kernel-c-abi (next two) 1.99 MB 66.4 KB
kernel-c-abi (next) 1.99 MB 31.2 KB
remaining ~18 workers 1.99 MB 0–4.8 KB
ODBC caller thread 8 MB 58.0 KB

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 ulimit -s 8192 and at ulimit -s 3120 (the partner's limit). Reason: tokio 1.53.1 leaves thread_stack_size: None, so Rust's std default of 2 MiB applies, and that default is not derived from RLIMIT_STACK — only RUST_MIN_STACK overrides it.

Two consequences:

  1. The fix's real value is narrower but still genuine: it stops RUST_MIN_STACK from shrinking workers (which the new regression proves), and it pins the size so a future tokio/std default change cannot silently move it. It does not change behaviour for a host that merely has a low ulimit -s.
  2. It therefore would not have prevented the partner's crash on its own — their workers already had 2 MiB, and the crashing thread still only had ~21 KiB of usable stack. Whatever produced that ~21 KiB window remains unexplained and host-side. I'll correct the description accordingly.

Given ~67 KB of real usage against a 2 MiB floor, I see no argument for raising the value. Worth keeping as-is.

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Sep 9, 2026
Merged via the queue into main with commit 91b4ed8 Sep 9, 2026
17 checks passed
@vikrantpuppala

Copy link
Copy Markdown
Collaborator Author

Correction to my earlier edit on this PR: I was wrong to remove the std::env::var_os / hyper-util proxy-discovery line from the root-cause section. It was correct, and I have now independently confirmed it. Restoring it here for the record.

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 .text size (0x1c85d40 vs the shipped 0x1d98079, different release toolchain), so offsets do not transfer. Instead I fingerprinted each frame by the string literals its function references, read straight out of the shipped binary's .rodata. That works with no symbols at all.

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 kernel-c-abi worker, down into proxy env discovery.

Two things this does NOT change:

  1. The stack arithmetic. Summing each frame's prologue sub $imm,%rsp plus return address across all 23 frames gives 16,760 bytes, which with the libc malloc frames on top matches the ~24 KB dense region measured at the stack floor in all three of the partner's cores. It is an ordinary 23-frame chain — no recursion (23 distinct functions), no frame ≥ 64 KB anywhere in the binary.
  2. Whether this PR fixes the partner crash. It does not — measured, their workers already get 3120 KiB unpatched, more than thread_stack_size(2 MiB) grants. This PR remains worth landing as hardening (blocks a small RUST_MIN_STACK, pins the size against std/tokio default drift, and 2 MiB has ~30x headroom against the measured ~67 KB peak).

Still not reproduced on my side: no proxy env, http_proxy set, and a ~34 KB no_proxy list all connect cleanly in the partner's own build image against the shipped RPM.

This branch was previously deployed

1 inactive deployment
azure-prod — ad46fb5c Deployed Sep 8, 2026 by vikrantpuppala via review #551
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.

fix(c-abi): isolate runtime worker stacks from RUST_MIN_STACK

1 participant