Skip to content

Conversation

@janvorli
Copy link
Member

@janvorli janvorli commented Dec 5, 2025

The current algorithm used for the stack overflow stactrace pretty printing isn't able to find repetitions that don't start at the first frame. That leads to huge stack traces when there are few non-repeated frames on the top of the stack, e.g. when a recursion in managed method calls to some common chain of methods and the stack overflow occurs down that chain and not in the recursive part.

This change fixes that. When no repeated sequence is found at the top of the stack, it tries to search from the next frame and so on until a repeated sequence is identified.

Close #118218

The current algorithm used for the stack overflow stactrace pretty
printing isn't able to find repetitions that don't start at the first
frame. That leads to huge stack traces when there are few non-repeated
frames on the top of the stack, e.g. when a recursion in managed
method calls to some common chain of methods and the stack overflow
occurs down that chain and not in the recursive part.

This change fixes that. When no repeated sequence is found at the top of
the stack, it tries to search from the next frame and so on until
a repeated sequence is identified.

Close dotnet#118218
@janvorli janvorli added this to the 11.0.0 milestone Dec 5, 2025
@janvorli janvorli requested a review from jkotas December 5, 2025 21:18
@janvorli janvorli self-assigned this Dec 5, 2025
Copilot AI review requested due to automatic review settings December 5, 2025 21:18
@janvorli
Copy link
Member Author

janvorli commented Dec 5, 2025

With this change, the stack trace from the test code from #118218 looks like this:

Stack overflow.
   at System.RuntimeFieldInfoStub.FromPtr(IntPtr)
   at System.Buffers.Text.FormattingHelpers.CountDigits(UInt32)
   at System.Number.UInt32ToDecStr_NoSmallNumberCheck(UInt32)
   at System.Number.UInt32ToDecStr(UInt32)
   at System.Number.FormatInt32(Int32, Int32, System.String, System.IFormatProvider)
   at System.Int32.ToString(System.IFormatProvider)
   at System.IO.TextWriter.Write(Int32)
   at System.IO.TextWriter.WriteLine(Int32)
   at System.IO.TextWriter+SyncTextWriter.WriteLine(Int32)
   at System.Console.WriteLine(Int32)
Repeated 15721 times:
--------------------------------
   at Program.<<Main>$>g__M|0_0(Int32)
--------------------------------
   at Program.<Main>$(System.String[])

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the stack overflow stack trace pretty printing algorithm to detect repetitions that don't start at the first frame. Previously, the algorithm could only identify repeated sequences starting from the top of the stack, leading to excessively long stack traces when non-repeated frames appeared at the beginning.

Key changes:

  • Refactored repetition detection from inline (during stack walk) to post-processing analysis
  • Removed member variables (m_commonStartIndex, m_largestCommonStartLength, m_largestCommonStartRepeat) in favor of local variables in PrintStackTrace
  • Implemented nested loop algorithm that searches for repeated sequences starting from different offsets in the stack

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
largestCommonLength = 0;
largestCommonRepeat = 0;

for (int i = largestCommonStartOffset; i < m_frames.Count(); i++)
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a quadratic algorithm. What's the worst-case delay that it can lead to in real-world apps?

Copy link
Member

Choose a reason for hiding this comment

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

I believe there are O(N log N) algorithms to do this if this one is too slow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pretty printing of stackoverflow stacktrace does not always work

2 participants