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

Test Host process crashes on Linux when a thread in a test method throws, but not on Windows #887

Open
odalet opened this issue Jun 24, 2021 · 3 comments

Comments

@odalet
Copy link
Contributor

odalet commented Jun 24, 2021

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)
@Evangelink
Copy link
Member

Hi @odalet,

Thanks for reporting this issue and sorry it took so much time to review it!

Your reproducer is not totally right and currently it's working mainly because the lambda is not yet triggered by the time you reach the assertion on Windows while it is on Linux.. By updating the repro as such:

[TestMethod]
public void Repro()
{
    var ok = false;
    var threadLambdaWasCalled = false;
    try
    {
        var thread = new Thread(() =>
        {
            ok = true;
            threadLambdaWasCalled = true;
            throw new Exception("TEST");
        });

        thread.Start();
    }
    catch
    {
        ok = false;
        Assert.Fail("Reached catch");
    }

    while (!threadLambdaWasCalled)
    {
        Thread.Sleep(100);
    }

    Thread.Sleep(100);

    Assert.IsTrue(ok);
}

You will have the same behavior in Windows/Linux.

On Windows, I still can see some difference between netfx and netcore.

We need to investigate furthermore to see what's the difference and how we can improve this behavior.

@odalet
Copy link
Contributor Author

odalet commented Dec 3, 2022

Thanks for your reply. Indeed, with your repro I end up with a similar crash in Linux and Windows (at least this is consistent!)
I updated my repro repo with your version of the test and also upgraded nugets to the current latest versions. That is:

<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />

And as expected, same behavior.

@Evangelink
Copy link
Member

Thanks for the confirmation.

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

No branches or pull requests

2 participants