Skip to content
This repository was archived by the owner on Aug 24, 2026. It is now read-only.

Fix exception when retrying test - #191

Merged
David Barbet (dibarbet) merged 1 commit into
microsoft:mainfrom
dibarbet:fix_exception
Aug 8, 2025
Merged

Fix exception when retrying test#191
David Barbet (dibarbet) merged 1 commit into
microsoft:mainfrom
dibarbet:fix_exception

Conversation

@dibarbet

@dibarbet David Barbet (dibarbet) commented Aug 8, 2025

Copy link
Copy Markdown
Member

Also don't swallow any xunit harness exception that occurs in the actual xunit infrastructure.

@dibarbet David Barbet (dibarbet) changed the title Fix exception when retrying test and don't swallow Fix exception when retrying test Aug 8, 2025
return false;
}

return _messageSink.OnMessage(message);

@dibarbet David Barbet (dibarbet) Aug 8, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is the fix. the newish xunit runner is expecting that if we call _messageSink.OnMessage(message) for an ITestCaseStarting message (adds to a cache), we also call it for ITestCaseFinished (removes from cache).

Otherwise if we don't call it for ITestCaseFinished we get an exception when calling it for ITestCaseStarting when retrying a test

protected override XunitTestRunner CreateTestRunner(ITest test, IMessageBus messageBus, Type testClass, object?[] constructorArguments, MethodInfo testMethod, object?[]? testMethodArguments, string skipReason, IReadOnlyList<BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
{
if (Process.GetCurrentProcess().ProcessName == "devenv")
if (SharedData.Exception is not null)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is related to the exception handling, but slightly speculative. There is code in IdeTestAssemblyRunner to set SharedData.Exception when there is an exception in the harness. It then eventually calls into this to run the test cases again to report the harness exception for each test case.

However - it appears as though if devenv is still running (aka it wasn't a devenv crash that caused this exception), then we'd never report the harness failure.

I moved this check up to report the harness failure even if we're still running devenv.

This check isn't enough by itself though - as the exception we were hitting would just be thrown again later on as we tried to report the test case starting message for the ErrorReportingIdeTestRunner

catch
using (var assemblyRunner = new IdeTestAssemblyRunner(TestAssembly, testCases, DiagnosticMessageSink, executionMessageSink, executionOptions))
{
await assemblyRunner.RunAsync();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is a bunch of exception handling code in places below this to report test failures if the harness fails.

However the exception handling code attempts to report actual test case failures via the xunit API. If those throw, then the exception bubbles all the way here, and would previously just silently catch it (though there are some logs that contain the exception).

Now, if we get here the actual test runner reports an unhandled exception and doesn't succeed.

This also matches what the xunit samples do, e.g. https://github.com/xunit/samples.xunit/blob/28d3683f74b104d33544efe5d1ae45ce9b0ad8c5/v2/AssemblyFixtureExample/XunitExtensions/XunitTestFrameworkExecutorWithAssemblyFixture.cs#L14

@dibarbet
David Barbet (dibarbet) marked this pull request as ready for review August 8, 2025 00:18
@dibarbet

Copy link
Copy Markdown
Member Author

Same known issue in CI (verified tests)

@dibarbet
David Barbet (dibarbet) merged commit 81c1d61 into microsoft:main Aug 8, 2025
3 of 5 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants