Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop in exception formatting when exceptions have cyclic references #3972

Conversation

Vijay-Braidable
Copy link

@Vijay-Braidable Vijay-Braidable commented Oct 26, 2024

The GetFormattedExceptionMessage method in ExceptionHelper.cs recursively traverses the inner exceptions of an exception to build a formatted error message string. However, it didn't handle the case where the inner exceptions have a cyclic reference, causing an infinite loop.

This change fixes that issue by keeping track of the exceptions seen during traversal in a HashSet. If an exception is encountered again, it breaks out of the loop and appends a "[Cyclic Exception Reference]" message to the output, avoiding the infinite recursion.

The key changes are:

  • Introduce a HashSet seenExceptions to keep track of exceptions encountered so far.
  • Check if the current exception has already been seen. If so, append a "[Cyclic Exception Reference]" message and break out of the loop.
  • If the current exception hasn't been seen, add it to the seenExceptions set and continue processing as before.
  • A new test class ExceptionHelperTests has also been added with two test methods:
    • GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely - Verifies that cyclic exceptions are handled correctly and don't cause infinite loops.
    • GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally - Ensures that the normal formatting still works as expected for non-cyclic exceptions.

This fixes #3971

… references

The GetFormattedExceptionMessage method in ExceptionHelper.cs recursively traverses
the inner exceptions of an exception to build a formatted error message string.
However, it did not handle the case where the inner exceptions have a cyclic
reference, causing an infinite loop.

This change fixes that issue by keeping track of the exceptions seen during traversal
in a HashSet. If an exception is encountered again, it breaks out of the loop and
appends a "[Cyclic Exception Reference]" message to the output, avoiding the infinite
recursion.

The key changes are:

Introduce a HashSet seenExceptions to keep track of exceptions
encountered so far.

Check if the current exception has already been seen. If so, append a
"[Cyclic Exception Reference]" message and break out of the loop.

If the current exception hasn't been seen, add it to the seenExceptions set
and continue processing as before.

A new test class ExceptionHelperTests has also been added with two test methods:

GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely -
Verifies that cyclic exceptions are handled correctly and don't cause infinite loops.

GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally -
Ensures that the normal formatting still works as expected for non-cyclic exceptions.

This fixes microsoft#1234

Files changed:

src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs
test/UnitTests/MSTestAdapter.UnitTests/Execution/ExceptionHelperTests.cs
@Vijay-Braidable
Copy link
Author

@microsoft-github-policy-service agree

Fix formatting
Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

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

You will also need to handle the StackTrace method helper.

Comment on lines +9 to +10
[TestClass]
public class ExceptionHelperTests
Copy link
Member

Choose a reason for hiding this comment

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

We are not using MSTest to test MSTest for some historical reason.

Suggested change
[TestClass]
public class ExceptionHelperTests
public sealed class ExceptionHelperTests : TestContainer

Comment on lines +12 to +13
[TestMethod]
public void GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[TestMethod]
public void GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely()
public void GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely()

Comment on lines +30 to +31
[TestMethod]
public void GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[TestMethod]
public void GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally()
public void GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally()

for (Exception? curException = ex;
curException != null;
curException = curException.InnerException)
{
if (!seenExceptions.Add(curException))
{
result.Append(" ---> [Cyclic Exception Reference]");
Copy link
Member

Choose a reason for hiding this comment

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

Please use localized message.

Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

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

You will also need to handle the StackTrace method helper.

@thomhurst
Copy link
Contributor

I still don't understand this. An exception can't have a cyclical inner exception reference without using reflection to hack it (unless someone can give me source code to prove me wrong?) And that would hang on the throw anyway so this wouldn't solve that?

@Evangelink
Copy link
Member

Good point! I assumed that the throw was good and it was hanging inside MSTest but that's the throw that's causing exception so nothing to do on MSTest side.

Thanks @thomhurst

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.

Throwing results in test never completing
4 participants