Skip to content

Commit

Permalink
Protect concurrent list access
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Dec 10, 2024
1 parent 3474213 commit 2790bb5
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ private sealed class DesignTimeTelemetryLogger(ITelemetryService telemetryServic
/// The names of any targets that reported errors. Names may be hashed.
/// May be empty even when errors exist, as not all errors come from a target.
/// </summary>
private List<string>? _errorTargets;
/// <remarks>
/// Targets can run concurrently, so use of this collection must be protected by a lock.
/// </remarks>
private readonly List<string> _errorTargets = [];

/// <summary>
/// Whether the build succeeded.
Expand Down Expand Up @@ -116,8 +119,10 @@ void OnErrorRaised(object sender, BuildErrorEventArgs e)

if (TryGetTargetRecord(e.BuildEventContext, out TargetRecord? record))
{
_errorTargets ??= [];
_errorTargets.Add(GetHashedTargetName(record));
lock (_errorTargets)
{
_errorTargets.Add(GetHashedTargetName(record));
}
}
}

Expand Down Expand Up @@ -150,6 +155,7 @@ void ILogger.Shutdown()
void SendTelemetry()
{
object[][] targetDurations;
string[] errorTargets;

lock (_targetRecordById)
{
Expand All @@ -161,6 +167,8 @@ void SendTelemetry()
.Take(10)
.Select(record => new object[] { GetHashedTargetName(record), record.Elapsed.Milliseconds })
.ToArray();

errorTargets = _errorTargets.ToArray();
}

telemetryService.PostProperties(
Expand All @@ -169,7 +177,7 @@ void SendTelemetry()
(TelemetryPropertyName.DesignTimeBuildComplete.Succeeded, _succeeded),
(TelemetryPropertyName.DesignTimeBuildComplete.Targets, new ComplexPropertyValue(targetDurations)),
(TelemetryPropertyName.DesignTimeBuildComplete.ErrorCount, _errorCount),
(TelemetryPropertyName.DesignTimeBuildComplete.ErrorTargets, _errorTargets),
(TelemetryPropertyName.DesignTimeBuildComplete.ErrorTargets, errorTargets),
]);
}

Expand Down

0 comments on commit 2790bb5

Please sign in to comment.