Skip to content

Fix uncaught StackOverflowError in StackTraces.getStackTrace - #12233

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 4 commits into
masterfrom
dougqh/fix-stacktraces-soe-in-error-capture
Aug 21, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 4 commits into
masterfrom
dougqh/fix-stacktraces-soe-in-error-capture

Conversation

@dougqh

@dougqh dougqh commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Widens the failure-handling in StackTraces.safeGetMessage and StackTraces.getStackTrace to also catch StackOverflowError, and adds a deeper fallback chain in getStackTrace for when even the getStackTrace()-array reconstruction fails.

Motivation

Intended to improve the case where a StackOverflowError is being reported. In that situation, StackTraces.getStackTrace is likely to trigger a fresh StackOverflowError.

Additional Notes

  • StackTraces.getStackTrace caught printStackTrace() failures with catch (Exception ...), so a second StackOverflowError thrown while formatting an already stack-constrained throwable escaped uncaught instead of falling back to the manual getStackTrace()-based reconstruction.
  • Widened the catches in safeGetMessage and getStackTrace to catch (Exception | StackOverflowError ...) — deliberately not the broader Throwable, so other Errors (e.g. OutOfMemoryError) still propagate instead of being silently swallowed.
  • Added a further fallback inside getStackTrace: if the getStackTrace()-array reconstruction itself throws, fall back to <class name>: <message>, and if even that throws, fall back to just the class name.
  • Found via Error Tracking issue 03773be6-9b34-11f1-930e-da7ad0900002 (service ci360-engage-optimize): a StackOverflowError in the customer's app was caught by our instrumentation with almost no stack margin left, and capturing its trace triggered a second overflow that propagated out of our error-handling code.

Jira: APMLP-1767

Test plan

  • Added TestThrowables.throwingStackOverflowOnPrintStackTrace(), throwingStackOverflowEverywhereExceptGetMessage(), and throwingStackOverflowEverywhere(), with unit tests asserting getStackTrace falls back gracefully at each level instead of throwing
  • ./gradlew :dd-trace-core:test --tests "datadog.trace.core.util.StackTracesTest" passes
  • ./gradlew :dd-trace-core:spotlessApply

🤖 Generated with Claude Code

dougqh and others added 2 commits August 18, 2026 15:42
printStackTrace() can itself throw a second StackOverflowError when
formatting a throwable caught with little remaining stack margin.
The existing fallback only caught Exception, so the second
StackOverflowError (an Error) escaped uncaught. Widen the catches to
Throwable so it falls through to the manual getStackTrace()-based
reconstruction instead.

APMLP-1767

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Widen safeGetMessage's catch to Throwable, and try t.getMessage()
in the innermost fallback of StackTraces.getStackTrace before giving
up on just the class name, so a bit more diagnostic context survives
even when both printStackTrace() and getStackTrace() throw.

APMLP-1767

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dougqh
dougqh marked this pull request as ready for review August 18, 2026 19:55
@dougqh
dougqh requested a review from a team as a code owner August 18, 2026 19:55
@dougqh
dougqh requested a review from mhlidd August 18, 2026 19:55
@dd-octo-sts dd-octo-sts Bot added the tag: ai generated Largely based on code generated by an AI or LLM label Aug 18, 2026
@dd-octo-sts

dd-octo-sts Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Hi! 👋 Thanks for your pull request! 🎉

To help us review it, please make sure to:

  • Add at least one type, and one component or instrumentation label to the pull request

If you need help, please check our contributing guidelines.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4cf9c0427c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread dd-trace-core/src/main/java/datadog/trace/core/util/StackTraces.java Outdated
Comment thread dd-trace-core/src/main/java/datadog/trace/core/util/StackTraces.java Outdated
@datadog-prod-us1-4

This comment has been minimized.

@datadog-prod-us1-4 datadog-prod-us1-4 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.

Datadog Autotest: FAIL

A recovered long stack trace still enters a final truncation block that catches only Exception, allowing another StackOverflowError to escape under the same low-stack condition this change targets.

Open Bits AI session

🤖 Datadog Autotest · Commit 4cf9c04 · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

Comment thread dd-trace-core/src/main/java/datadog/trace/core/util/StackTraces.java Outdated
@dd-octo-sts

dd-octo-sts Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.84 s 14.76 s [-0.4%; +1.5%] (no difference)
startup:insecure-bank:tracing:Agent 13.55 s 13.71 s [-1.9%; -0.4%] (maybe better)
startup:petclinic:appsec:Agent 16.84 s 16.69 s [+0.1%; +1.7%] (maybe worse)
startup:petclinic:iast:Agent 16.40 s 16.99 s [-7.8%; +0.9%] (no difference)
startup:petclinic:profiling:Agent 16.72 s 16.60 s [-0.3%; +1.7%] (no difference)
startup:petclinic:sca:Agent 16.75 s 16.69 s [-0.6%; +1.3%] (no difference)
startup:petclinic:tracing:Agent 16.07 s 16.16 s [-1.4%; +0.3%] (no difference)

Commit: f6a2f4a8 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

dougqh added 2 commits August 18, 2026 16:41
catch (Throwable) also swallowed ThreadDeath and unrelated fatal
Errors; narrow to the two cases this fix actually needs to recover
from. Also extend the same recovery to the truncate() call so a
second StackOverflowError while the stack is still constrained
during truncation doesn't escape uncaught.

Addresses codex review comments on #12233.
@dougqh
dougqh requested a review from a team as a code owner August 21, 2026 14:38
@dougqh
dougqh requested review from jordan-wong and removed request for a team August 21, 2026 14:38
@dougqh
dougqh enabled auto-merge August 21, 2026 14:38

@datadog-prod-us1-4 datadog-prod-us1-4 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.

Datadog Autotest: PASS

More details

The code now catches StackOverflowError at each stack trace formatting step. Other Error types still pass to the caller.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Datadog Autotest · Commit f6a2f4a · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

@dougqh dougqh added comp: core Tracer core type: bug fix Bug fix labels Aug 21, 2026
@dougqh
dougqh added this pull request to the merge queue Aug 21, 2026
@dd-octo-sts

dd-octo-sts Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-08-21 19:22:27 UTC ℹ️ Start processing command /merge


2026-08-21 19:22:32 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in master is approximately 2h (p90).


2026-08-21 20:16:19 UTC ℹ️ MergeQueue: This merge request was merged

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 21, 2026
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit c162404 into master Aug 21, 2026
592 of 596 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the dougqh/fix-stacktraces-soe-in-error-capture branch August 21, 2026 20:16
@github-actions github-actions Bot added this to the 1.66.0 milestone Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: core Tracer core tag: ai generated Largely based on code generated by an AI or LLM type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants