KirillOsenkov / MSBuildStructuredLog

A logger for MSBuild that records a structured representation of executed targets, tasks, property and item values.
MIT License
1.41k stars 188 forks source link

Race condition when loading binlogs #724

Closed rolfbjarne closed 7 months ago

rolfbjarne commented 7 months ago

I do something like this:

var build = BinLog.ReadBuild ("msbuild.binlog");
var props = build.FindChildrenRecursive<Property> (v => true);
Console.WriteLine ($"Count A: {props.Count ()}");
props = build.FindChildrenRecursive<Property> (v => true);
Console.WriteLine ($"Count B: {props.Count ()}");

And sometimes I get different answers between A and B.

It took me a while to figure out what's happening, but the problem is that some items are loaded in the background:

https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/3071c2b297a3e6e38d0112f57d84725b47009a67/src/StructuredLogger/Construction/Construction.cs#L571

https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/3071c2b297a3e6e38d0112f57d84725b47009a67/src/StructuredLogger/Construction/Construction.cs#L993

which means that doing the same thing multiple times might yield different results.

Maybe there should be a setting to say to not load stuff in the background when reading binlogs programmatically?

KirillOsenkov commented 7 months ago

Sorry about that, I thought the viewer doesn't care because these finish quickly enough, but I forgot about the API case.

I just made it that we block on all these tasks before returning the build.

KirillOsenkov commented 7 months ago

Published https://www.nuget.org/packages/MSBuild.StructuredLogger/2.2.100, hopefully it will work properly for you.