Skip to content

Commit 8797b4d

Browse files
committed
add lock around MSBuild registration
1 parent 135fdd2 commit 8797b4d

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Microsoft.ComponentDetection.Detectors/nuget/NuGetMSBuildBinaryLogComponentDetector.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class NuGetMSBuildBinaryLogComponentDetector : FileComponentDetector
3131
};
3232

3333
private static bool isMSBuildRegistered;
34+
private static object msBuildRegistrationGate = new();
3435

3536
public NuGetMSBuildBinaryLogComponentDetector(
3637
IObservableDirectoryWalkerFactory walkerFactory,
@@ -131,12 +132,15 @@ protected override Task OnFileFoundAsync(ProcessRequest processRequest, IDiction
131132
{
132133
try
133134
{
134-
if (!isMSBuildRegistered)
135+
lock (msBuildRegistrationGate)
135136
{
136-
// this must happen once per process, and never again
137-
var defaultInstance = MSBuildLocator.QueryVisualStudioInstances().First();
138-
MSBuildLocator.RegisterInstance(defaultInstance);
139-
isMSBuildRegistered = true;
137+
if (!isMSBuildRegistered)
138+
{
139+
// this must happen once per process, and never again
140+
var defaultInstance = MSBuildLocator.QueryVisualStudioInstances().First();
141+
MSBuildLocator.RegisterInstance(defaultInstance);
142+
isMSBuildRegistered = true;
143+
}
140144
}
141145

142146
var singleFileComponentRecorder = this.ComponentRecorder.CreateSingleFileComponentRecorder(processRequest.ComponentStream.Location);

0 commit comments

Comments
 (0)