Skip to content

Stop capturing the static TLS block with every thread's stack - #165

Closed
fran6co wants to merge 1 commit into
getsentry:getsentryfrom
fran6co:fran6co/cap-stack-capture
Closed

fran6co wants to merge 1 commit into
getsentry:getsentryfrom
fran6co:fran6co/cap-stack-capture

Conversation

@fran6co

@fran6co fran6co commented Jul 31, 2026

Copy link
Copy Markdown

Problem

ProcessReaderLinux::Thread::InitializeStackFromSP bounds a thread's stack capture with the thread pointer, which is a big improvement on capturing the whole mapping. On glibc, though, the static TLS block sits immediately below the thread pointer, at the high-address end of that same mapping — so every thread is captured as live stack + the entire static TLS block.

The block scales with the number of loaded DSOs, not with anything about the thread.

I byte-parsed a 144.9 MB minidump from a production application:

region bytes share
header, directory, module list, thread list, contexts 1.09 MB 0.8%
thread stacks (818 threads) 143.8 MB 99.2%

Per thread that is ~175 KB, of which around 700 bytes was live stack — the rest zeroes. The contributors are ordinary libraries: libglog's thread_local LogMessageData is 30,448 B on its own (it holds a 30,001-byte message buffer), plus ~4 KB each from libcudart and eight libnpp*. The intra-application spread was ±3%, which is the signature of a fixed per-thread constant rather than of stack usage.

Fix

Cap the captured length for non-main threads. The region starts at the stack pointer and grows towards the stack base, so shortening it discards the TLS block before it discards any frame. The main thread already keeps its full stack via the enclosing tid != reader->ProcessID() check.

Why 64 KB is safe

The deepest live non-main stack I measured across four applications was 12.4 KB, so the cap has ~5x headroom. Verified that a capped thread still walks correctly end to end — minidump-stackwalk on a dump whose crashing thread was capped (65,536 B kept of a 113,072 B span):

 0  libc.so.6 + 0x9ec0c        <- abort
 1  libc.so.6 + 0x4527d
 2  libc.so.6 + 0x288fe
 3  libstdc++.so.6 + 0xa5a3f   <- std::terminate
 4  libstdc++.so.6 + 0xa5a54
 5  libstdc++.so.6 + 0xbb390
 6  <app> + 0xcb0eae           <- the frame that threw
 7  libstdc++.so.6 + 0xecdb3   <- thread proc
 8  libc.so.6 + 0x9cb83        <- start_thread

Measured effect

application threads before after
A 818 144.9 MB 54.0 MB
B 185 37.5 MB 12.4 MB
C 123 15.5 MB 8.2 MB

Beyond file size this matters for how long a dump takes to write, which is load-bearing when the client is waiting on it.

🤖 Generated with Claude Code

InitializeStackFromSP bounds a thread's stack capture with the thread
pointer, which is a big improvement on capturing the whole mapping. On
glibc, though, the static TLS block sits immediately below the thread
pointer, at the high-address end of the same mapping, so every thread is
still captured as its live stack plus that entire block.

The block scales with the number of loaded DSOs, not with anything about
the thread. On one real application it is ~175 KB per thread, of which
around 700 bytes is live stack - glog's thread_local LogMessageData alone
contributes 30,448 bytes, and CUDA's runtime and NPP libraries several
kilobytes each. Byte-parsing a 144.9 MB minidump from that application:
143.8 MB, 99.2% of the file, was thread stacks across 818 threads, and
almost all of it zeroes.

Cap the captured length. The region starts at the stack pointer and grows
towards the stack base, so shortening it drops the TLS block before it
drops any frame; the deepest live non-main stack measured across four
applications was 12.4 KB, against a 64 KB cap. The main thread already
keeps its full stack via the enclosing tid check.

Measured effect: 144.9 MB -> 54.0 MB, and 37.5 MB -> 12.4 MB on a second
application. Backtraces are unaffected - a capped thread still walks from
the faulting frame to thread entry.
@fran6co
fran6co force-pushed the fran6co/cap-stack-capture branch from f3026a1 to 80ae102 Compare July 31, 2026 15:38
// Truncating the length drops the static TLS block, which sits at the
// stack base end of the region, rather than any frame.
constexpr LinuxVMSize kMaxCapturedStackSize = 64 * 1024;
stack_region_size = std::min(stack_region_size, kMaxCapturedStackSize);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The 64 KB stack cap applied to non-main threads can truncate the stack trace of a crashing thread, potentially hiding the root cause of the crash.
Severity: HIGH

Suggested Fix

The 64 KB stack cap should not be applied to the specific thread that is crashing. The logic should be modified to check if the thread being processed is the one that triggered the exception and, if so, bypass the stack truncation to ensure the full stack trace is always captured for crash analysis.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: snapshot/linux/process_reader_linux.cc#L211

Potential issue: When a non-main thread crashes, its stack is captured for the minidump.
The logic at `process_reader_linux.cc:211` unconditionally caps this captured stack at
64 KB for all non-main threads. If a crashing thread's call stack is deeper than 64 KB,
which can occur with recursion or complex library calls, the stack trace will be
truncated. This results in an incomplete crash report that may be missing the crucial
frames needed to diagnose the root cause of the crash, severely hindering debugging
efforts.

Did we get this right? 👍 / 👎 to inform future reviews.

@fran6co

fran6co commented Jul 31, 2026

Copy link
Copy Markdown
Author

This is not a good fix as pointed out by the review

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.

1 participant