Ask a questionAsk a question
 

AnswerParallel.For loop unit test crashes VsTestHost

  • Wednesday, May 21, 2008 5:34 PMmegabitee Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    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

  • Sunday, May 25, 2008 4:20 AMStephen Toub - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Snippet

    static 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

  • Sunday, May 25, 2008 4:20 AMStephen Toub - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Snippet

    static 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.

     

     

     

     

     

  • Friday, January 30, 2009 6:41 PMunbornchikkenMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    It works for me! Thank you.