Skip to content

[2.x] fix: Fix StackOverflowError when reporting self-referencing exceptions#8508

Merged
eed3si9n merged 1 commit intosbt:developfrom
MkDev11:fix/circular-exception-stackoverflow-7509
Jan 13, 2026
Merged

[2.x] fix: Fix StackOverflowError when reporting self-referencing exceptions#8508
eed3si9n merged 1 commit intosbt:developfrom
MkDev11:fix/circular-exception-stackoverflow-7509

Conversation

@MkDev11
Copy link
Copy Markdown
Contributor

@MkDev11 MkDev11 commented Jan 13, 2026

Fix StackOverflowError when reporting self-referencing exceptions

Problem

When an exception has a circular reference in its cause chain (e.g., exception.getCause() == exception), sbt throws a StackOverflowError while trying to report the exception. This happens because StackTrace.trimmedLines traverses the cause chain without checking for circular references.

This is problematic because some libraries or frameworks may create exceptions with self-referencing causes, and sbt should be resilient to such edge cases.

Solution

This PR adds circular reference detection to StackTrace.trimmedLines using an IdentityHashMap-backed Set, similar to how the JDK handles this in Throwable.printStackTrace().

When a circular reference is detected, instead of recursing infinitely, the method now appends a [CIRCULAR REFERENCE: ...] message to indicate the cycle.

Changes

  • Modified StackTrace.trimmedLines to track seen exceptions using IdentityHashMap
  • Added check to detect circular references before processing each cause
  • When circular reference is detected, append informative message instead of recursing
  • Added unit tests for:
    • Normal exceptions
    • Exceptions with causes
    • Self-referencing exceptions
    • Circular exception chains

Example Output

For a self-referencing exception, the output now looks like:

java.lang.RuntimeException: self-referencing exception
	at ...
[CIRCULAR REFERENCE: java.lang.RuntimeException: self-referencing exception]

Fixes #7509


Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=94194147

Add circular reference detection to StackTrace.trimmedLines using an
IdentityHashMap-backed Set, similar to how the JDK handles this in
Throwable.printStackTrace().

When a circular reference is detected, the method now appends a
[CIRCULAR REFERENCE: ...] message instead of recursing infinitely.

Fixes sbt#7509
@MkDev11
Copy link
Copy Markdown
Contributor Author

MkDev11 commented Jan 13, 2026

@eed3si9n all checks passed, please review the PR and let me know your feedback. thanks.

@eed3si9n eed3si9n changed the title Fix StackOverflowError when reporting self-referencing exceptions [2.x] fix: Fix StackOverflowError when reporting self-referencing exceptions Jan 13, 2026
Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

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

Thanks!

@eed3si9n eed3si9n merged commit 02dcab8 into sbt:develop Jan 13, 2026
14 checks passed
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.

sbt throws a StackOverflowError when reporting a self-referencing exception.

2 participants