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

Targets may be run more than once when running in parallel #626

Closed
adamralph opened this issue Jul 25, 2021 · 1 comment · Fixed by #627
Closed

Targets may be run more than once when running in parallel #626

adamralph opened this issue Jul 25, 2021 · 1 comment · Fixed by #627
Labels
bug Something isn't working
Milestone

Comments

@adamralph
Copy link
Owner

adamralph commented Jul 25, 2021

Version(s)

From 2.0.0 to 3.7.1 inclusive.

To reproduce

Steps to reproduce the behaviour:

  1. Create a console app targetting netcoreapp3.0 or later
  2. Create some targets with diamond dependencies, e.g.
    var names = new List<string>();
    
    foreach (var name in Enumerable.Range(1, 10).Select(i => i.ToString(CultureInfo.InvariantCulture)))
    {
        Target(name, names.TakeLast(2), () => { });
        names.Add(name);
    }
    
    Target("default", DependsOn(names.Last()));
    
    RunTargetsAndExit(args);
  3. Run with the --parallel option.

Expected behaviour

Actual behaviour

Workarounds

Target netcoreapp2.x or earlier, or do not use the --parallel option.

Additional context

Thanks to @alibresco for raising #619 which led to discovery of the bug!

It is due to the behaviour of ConcurrentDictionary.GetOrAdd:

If you call GetOrAdd simultaneously on different threads, valueFactory may be called multiple times, but only one key/value pair will be added to the dictionary.

Although this remark is also present in versions of the documentation for frameworks for netcoreapp2.x and earlier, I was unable to reproduce the bug in those versions, suggesting that the chance of valueFactory being called multiple times increased significantly in netcoreapp3.0.

@adamralph adamralph added the bug Something isn't working label Jul 25, 2021
@adamralph adamralph added this to the 3.8.0 milestone Jul 25, 2021
@adamralph
Copy link
Owner Author

released in 3.8.0-alpha.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant