Skip to content

RUM-10417: Handle potential StackOverflowError#2990

Merged
kikoveiga merged 12 commits into
developfrom
kikoveiga/RUM-10417/fix-potential-exception
Nov 26, 2025
Merged

RUM-10417: Handle potential StackOverflowError#2990
kikoveiga merged 12 commits into
developfrom
kikoveiga/RUM-10417/fix-potential-exception

Conversation

@kikoveiga

@kikoveiga kikoveiga commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

What does this PR do?

Wraps the onRequestIntercepted method in TracingInterceptor.kt inside a try - catch block that logs an error in case a StackOverflowError is caught.

Motivation

If a customer implements a custom TracedRequestListener and keeps retrying a request with onRequestIntercepted, this will run forever, eventually leading to a StackOverflowError.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@kikoveiga kikoveiga self-assigned this Nov 8, 2025
@kikoveiga kikoveiga changed the title Kikoveiga/rum 10417/fix potential exception RUM-10417: Handle potential StackOverflowException Nov 8, 2025
@kikoveiga kikoveiga changed the title RUM-10417: Handle potential StackOverflowException RUM-10417: Handle potential StackOverflowError Nov 10, 2025
@kikoveiga kikoveiga marked this pull request as ready for review November 11, 2025 10:53
@kikoveiga kikoveiga requested review from a team as code owners November 11, 2025 10:53
@kikoveiga kikoveiga requested a review from satween November 11, 2025 10:53
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Nov 11, 2025

Copy link
Copy Markdown

🎯 Code Coverage
Patch Coverage: 100.00%
Total Coverage: 71.32% (-0.01%)

View detailed report

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: ff8e241 | Docs | Datadog PR Page | Was this helpful? Give us feedback!

@codecov-commenter

codecov-commenter commented Nov 11, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.21%. Comparing base (8f7122f) to head (ff8e241).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2990      +/-   ##
===========================================
- Coverage    71.26%   71.21%   -0.05%     
===========================================
  Files          859      859              
  Lines        31449    31457       +8     
  Branches      5306     5306              
===========================================
- Hits         22411    22400      -11     
- Misses        7537     7557      +20     
+ Partials      1501     1500       -1     
Files with missing lines Coverage Δ
...datadog/android/okhttp/trace/TracingInterceptor.kt 82.67% <100.00%> (+0.11%) ⬆️

... and 25 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

tracedRequestListener.onRequestIntercepted(request, span, response, throwable)
try {
tracedRequestListener.onRequestIntercepted(request, span, response, throwable)
} catch (e: StackOverflowError) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While this prevents crashing host app, it still allows having some loops before hitting stack overflow.

Maybe we can even prevent making loops by adding a tag to the Request with a {Interceptor}.toString() or System.identityHashCode (it should be a Set then since the same request may go through multiple interceptors) and reading it back to check if we are in the loop. WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One more note: normally Error classes are non-recoverable JVM errors, it is not advised to continue execution when such is thrown.

https://docs.oracle.com/javase/8/docs/api/java/lang/Error.html

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure we want to make such changes. This PR is mostly to help customer to identify a problem. We want to explain why the stackoverflow error is thrown, not preventing recursive calls. Some customers might like to have them. But I agree that the error should not be swallowed by catch block and should be re-thrown.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have added the re-throwing of the error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can indeed re-throw it. My point is that after getting such error it is not possible to continue normal execution, because stack may not recover to the state (free capacity) enough to continue reliably. Even if may continue here, if stack is not unrolled, similar error may pop up in random place further.

@kikoveiga kikoveiga requested a review from 0xnm November 25, 2025 15:48
0xnm
0xnm previously approved these changes Nov 25, 2025
@kikoveiga kikoveiga force-pushed the kikoveiga/RUM-10417/fix-potential-exception branch from 36741e6 to ff8e241 Compare November 26, 2025 12:12
@kikoveiga kikoveiga requested a review from 0xnm November 26, 2025 15:00
@kikoveiga kikoveiga merged commit 9794bcb into develop Nov 26, 2025
26 of 27 checks passed
@kikoveiga kikoveiga deleted the kikoveiga/RUM-10417/fix-potential-exception branch November 26, 2025 15:24
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.

4 participants