2

We are using the Visual Studio test framework and C# to write unit tests, and we recently ran into a problem where vstest.console.exe sometimes hangs after executing all the tests. All tests pass, but then vstest.console.exe just sits there forever waiting for something, instead of exiting.

This is likely related to recent changes where we started using a third party library. When running the tests in debug, we can see that multiple threads created by that library are still hanging around after all tests have already passed.

To understand the problem better, I tried to reproduce the issue as shown in the test code below. However, it seems that vstest.console.exe is perfectly happy to exit after completing a test like this. It doesn't seem to care about any "wild" threads that are still running. So my question is this: what is causing vstest to wait or not wait for threads after executing all tests? In other words, how should I modify the test below to make vstest hang after completing it?

[TestMethod]
public void FreezeVSTest()
{
    var thread = new Thread(
        new ThreadStart(
            delegate
            {
                while (true)
                {
                    Thread.Sleep(1);
                }
            }));
    thread.Start();
}

(Note that I'm not asking how to make the test hang, I know that adding a thread.Join() would achieve that. The question is about vstest itself hanging after the test passes.)

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • 1
    [Background threads](https://learn.microsoft.com/en-us/dotnet/standard/threading/foreground-and-background-threads) like yours is not a problem, foreground will cause a freeze. In either way infinite loop without signal when you are closing is a bad idea. Can you be more specific regarding what test you have, what library, etc.? – Sinatr Dec 11 '20 at 09:03
  • @Sinatr: the goal of this question is not to find a fix for the exact issue we have directly, but rather to understand the principles of the problem. I'm hoping to get a better understanding of what situations cause vstest to hang, and hopefully that insight that will lead to possible solutions. – Wim Coenen Dec 11 '20 at 09:12
  • But if you must know, the library is from Essential Objects. We invoke it in a test to convert HTML to PDF. – Wim Coenen Dec 11 '20 at 09:13
  • 2
    @Sinatr: This is not a background thread. Maybe the example doesn't cause the problem because the thread variable itself runs out of scope? Try keeping the thread instance in a static member. – PMF Dec 11 '20 at 09:16
  • @PMF: I tested it, but storing a reference to the thread in a static member did not make a difference. – Wim Coenen Dec 11 '20 at 09:18
  • @Sinatr: I tried adding "thread.IsBackground=false", but it did not make a difference. – Wim Coenen Dec 11 '20 at 09:19
  • @PMF, right. Should have tested it myself first. I guess it could be some async stuff in library, which prevents non-blocking test. Therefore the first question: what is this exactly? And to answer we need to see [mcve]. – Sinatr Dec 11 '20 at 09:34
  • @Sinatr: again, my question is not about "here's a specific case where vstest hangs, how do I fix it". The question is how can cause this type of issue myself, to understand more about what circumstances can lead to such a symptom. – Wim Coenen Dec 11 '20 at 09:50
  • In principle if you write a console application, which doesn't block but prevents process from exit, then using it in test should hang up the test, despite it will finish. [It could be antivirus](https://stackoverflow.com/q/20750084/1997232), so check environment too. – Sinatr Dec 11 '20 at 09:50

0 Answers0