testfx
testfx copied to clipboard
Test Host process crashes on Linux when a thread in a test method throws, but not on Windows
Description
Some unit tests have the test host crash when run on a Linux machine, but not on Windows
Steps to reproduce
On a Debian 10 machine (also reproduced in WSL or with the Docker file supplied in the same repo as below):
git clone https://github.com/odalet/mstest-test
cd mstest-test
dotnet test MSTestTest.sln
Expected behavior
The test should 'gracefully' fail
Actual behavior
When on Linux, the test has the host process crash with this message:
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.Exception: TEST
at MSTestTest.UnitTests.<>c__DisplayClass0_0.<Repro>b__0() in /root/mstest-test/UnitTests.cs:line 20
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Test Run Aborted.
But running the same test on Windows produces the expected result:
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Failed Repro [24 ms]
Error Message:
Assert.IsTrue failed.
Stack Trace:
at MSTestTest.UnitTests.Repro() in C:\work\repositories\_odalet\mstest-test\UnitTests.cs:line 31
Remarks
I reproduced the issue with this code:
try
{
var thread = new Thread(() =>
{
ok = true;
throw new Exception("TEST");
});
thread.Start();
}
It's obviously wrong but models what I suppose happens in my real unit tests. What has the test host crash is, I think, the fact that the thread keeps running after the test method ends, then throws, and this exception is not caught by the Linux version of the test engine.
- At first I thought this bug was v2.2-specific because on my CI (that builds using a Debian based docker), reverting to v2.1.2 has my tests pass...
- But then, with the simple repro I crafted, I consistently crash on Linux, be it with v2.2 or v2.1 of MSTest nugets.
- I suppose that different versions incur different timings and that may explain why, in my real tests, the race condition that crashes in v2.2 simply does not occur in v2.1
Environment
- Debian 10
- Installed .NET SDK: 5.0.301 (but also has .NET Core 3.1 runtime)
- but also reproduced in a container using mcr.microsoft.com/dotnet/sdk:3.1.410-buster
- MSTest: 2.2.5-preview-20210605-01 (but also reproduced with v2.1.2)