Parallel.For loop unit test crashes VsTestHost
this is an example of a small unit test which runs a Parallel.For loop 200,000 times. many times this unit test causes VsTestHost to crash. we are having this problem in our project and have narrowed the problem down to this example. I can send or attach a small project if desired.
using
System;using
System.Text;using
System.Collections.Generic;using
System.Linq;using
Microsoft.VisualStudio.TestTools.UnitTesting;using
System.Threading;namespace
ParallelCrashTest{
/// <summary>
/// Summary description for ParallelCrash
/// </summary>
[TestClass]
public class ParallelCrash
{
[TestMethod]
public void ParrelCrashTest()
{
for (int x = 0; x < 200000; x++)
{
Parallel.For(0, 9, delegate(int y)
{
});
}
}
}
}
Answers
Thanks for the bug report. This is a known issue with the CTP bits having to do with aborting worker threads, and we haven't made a fix available publicly. As a workaround, you could try adding the following code to your unit test project:
Code Snippetstatic void UnitTestWorkaround(Action a)
{
using (var tm = new TaskManager())
Task.Create(delegate { a(); }, tm).Wait();
}
Then instead of the code you currently have, you could surround it with a call to UnitTestWorkaround, where the delegate passed to UnitTestWorkaround includes your code, e.g.:
Code Snippet[TestMethod]
public void ParrelCrashTest()
{
UnitTestWorkaround(() => {
for (int x = 0; x < 200000; x++)
{
Parallel.For(0, 9, delegate(int y)
{
});
}
});
}
Please let me know if this "fixes" (or doesn't) the problem for you.
All Replies
Thanks for the bug report. This is a known issue with the CTP bits having to do with aborting worker threads, and we haven't made a fix available publicly. As a workaround, you could try adding the following code to your unit test project:
Code Snippetstatic void UnitTestWorkaround(Action a)
{
using (var tm = new TaskManager())
Task.Create(delegate { a(); }, tm).Wait();
}
Then instead of the code you currently have, you could surround it with a call to UnitTestWorkaround, where the delegate passed to UnitTestWorkaround includes your code, e.g.:
Code Snippet[TestMethod]
public void ParrelCrashTest()
{
UnitTestWorkaround(() => {
for (int x = 0; x < 200000; x++)
{
Parallel.For(0, 9, delegate(int y)
{
});
}
});
}
Please let me know if this "fixes" (or doesn't) the problem for you.
It works for me! Thank you.

