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

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks

@janvorli
Copy link
Member Author

janvorli commented Dec 9, 2025

Actually, let me test it on macOS arm64. The minimal stack frame is much smaller there and I want to check if we need to introduce the upper limit for the outerloop due to that.

@janvorli
Copy link
Member Author

@jkotas test on macOS arm64 proves we need an upper limit. With your test case, there is over 350,000 frames on the stack, so the logging took 16 secs.

@janvorli janvorli merged commit ba80e54 into dotnet:main Dec 10, 2025
99 checks passed
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