This project is read-only.
1
Vote

Alpaca Test reports Data Race when using Lambdas

description

Hi!
 
running the given sample from below I get a Data Race reported for the "LambdaCausesDataRace" test and no Data Race for the "ForeachLoopCausesNoDataRace" test. Why?
 
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Concurrency.TestTools.UnitTesting;
 
namespace MOCTestProject
{
public class AlpacaTest2
{

    [DataRaceTestMethod]
    public void ForeachLoopCausesNoDataRace()
    {
        var parameters = new[] { new object(), "test", 5 };
        string result1 = null;
        string result2 = null;
 
        Parallel.Invoke(
            () =>
            {
                result1 = ParametersToTypeListUsingForeachLoop(parameters);
            },
 
            () =>
            {
                result2 = ParametersToTypeListUsingForeachLoop(parameters);
            }
            );
 
        Assert.AreEqual("Object, String, Int32", result1);
        Assert.AreEqual("Object, String, Int32", result2);
    }
 
    private static string ParametersToTypeListUsingForeachLoop(object[] parameters)
    {
        List<string> result = new List<string>();
        foreach (var p in parameters)
        {
            result.Add(p == null ? null : p.GetType().Name);
        }
        return string.Join(", ", result);
    }
 
    [DataRaceTestMethod]
    public void LambdaCausesDataRace()
    {
        var parameters = new[] { new object(), "test", 5 };
        string result1 = null;
        string result2 = null;
 
        Parallel.Invoke(
            () =>
            {
                result1 = ParametersToTypeListUsingLambda(parameters);
            },
 
            () =>
            {
                result2 = ParametersToTypeListUsingLambda(parameters);
            }
            );
 
        Assert.AreEqual("Object, String, Int32", result1);
        Assert.AreEqual("Object, String, Int32", result2);
    }
 
    private static string ParametersToTypeListUsingLambda(object[] parameters)
    {
        return string.Join(", ", parameters.Select(p => p == null ? null : p.GetType().Name).ToArray());
    }
}
}
 
-Matthias

file attachments

comments

MatthiasFG wrote Apr 1, 2011 at 9:22 AM

Or here is a slightly different sample that causes the same issues:

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Concurrency.TestTools.UnitTesting;

namespace MOCTestProject
{
public class AlpacaTest3
{ 
    [DataRaceTestMethod]
    public void ForeachLoopCausesNoDataRace()
    {
        var input = new List<string>() { "1", "2", "3" };
        object result1 = null;
        object result2 = null;

        Parallel.Invoke(
            () =>
            {
                result1 = GetItemsUsingForeach(input);
            },

            () =>
            {
                result2 = GetItemsUsingForeach(input);
            }
            );

        Assert.IsNotNull(result1);
        Assert.IsNotNull(result2);
    }

    private static IEnumerable<string> GetItemsUsingForeach(IEnumerable<string> input)
    {
        foreach (var item in input)
        {
            if (item != null)
            {
                yield return item;
            }
        }
    }

    [DataRaceTestMethod]
    public void LambdaCausesDataRace()
    {
        var input = new List<string>() { "1", "2", "3"};
        object result1 = null;
        object result2 = null;

        Parallel.Invoke(
            () =>
            {
                result1 = GetItemsUsingLambda(input);
            },

            () =>
            {
                result2 = GetItemsUsingLambda(input);
            }
            );

        Assert.IsNotNull(result1);
        Assert.IsNotNull(result2);
    }

    private static IEnumerable<string> GetItemsUsingLambda(IEnumerable<string> input)
    {
        return input.Where(item => item != null);
    }
}
}

MatthiasFG wrote Apr 11, 2011 at 7:52 AM

push
Nothing new?

wrote May 3, 2011 at 9:57 PM

Associated with changeset 58005.

MatthiasFG wrote May 4, 2011 at 3:07 PM

Hi Joe!

I downloaded 58009 and build Alpaca and all the other projects and run it on my sample code.
But I still get the DataRace reports.
Did I made something wrong?

-Matthias

MatthiasFG wrote Aug 15, 2011 at 10:18 AM

looks like its an extension methos issue.
Can Alpaca/mchess handle extension methods?

MatthiasFG wrote Aug 15, 2011 at 10:22 AM

Looks like it's an extension method issues.
Can Alpaca/mchess handle extension methods?

wrote Feb 14, 2013 at 8:24 PM