Skip to content

Commit a194462

Browse files
authored
Merge pull request #21641 from heejaechang/threadSafe
make SupportedDiagnosticId for build error thread safe
2 parents 1f4221b + c21f729 commit a194462

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/VisualStudio/Core/Def/Implementation/TaskList/ExternalErrorDiagnosticUpdateSource.cs

+18-15
Original file line numberDiff line numberDiff line change
@@ -420,26 +420,29 @@ public void Done()
420420

421421
public bool SupportedDiagnosticId(ProjectId projectId, string id)
422422
{
423-
if (_diagnosticIdMap.TryGetValue(projectId, out var ids))
423+
lock (_diagnosticIdMap)
424424
{
425-
return ids.Contains(id);
426-
}
425+
if (_diagnosticIdMap.TryGetValue(projectId, out var ids))
426+
{
427+
return ids.Contains(id);
428+
}
427429

428-
// set ids set
429-
var map = new HashSet<string>();
430-
_diagnosticIdMap.Add(projectId, map);
430+
// set ids set
431+
var map = new HashSet<string>();
432+
_diagnosticIdMap.Add(projectId, map);
431433

432-
var project = _solution.GetProject(projectId);
433-
if (project == null)
434-
{
435-
// projectId no longer exist, return false;
436-
return false;
437-
}
434+
var project = _solution.GetProject(projectId);
435+
if (project == null)
436+
{
437+
// projectId no longer exist, return false;
438+
return false;
439+
}
438440

439-
var descriptorMap = _owner._diagnosticService.GetDiagnosticDescriptors(project);
440-
map.UnionWith(descriptorMap.Values.SelectMany(v => v.Select(d => d.Id)));
441+
var descriptorMap = _owner._diagnosticService.GetDiagnosticDescriptors(project);
442+
map.UnionWith(descriptorMap.Values.SelectMany(v => v.Select(d => d.Id)));
441443

442-
return map.Contains(id);
444+
return map.Contains(id);
445+
}
443446
}
444447

445448
public ImmutableArray<DiagnosticData> GetBuildDiagnostics()

src/VisualStudio/Core/Test/Diagnostics/ExternalDiagnosticUpdateSourceTests.vb

+14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Diagnostics
6868
End Using
6969
End Sub
7070

71+
<Fact>
72+
Public Sub TestExternalDiagnostics_SupportedDiagnosticId_Concurrent()
73+
Using workspace = TestWorkspace.CreateCSharp(String.Empty)
74+
Dim waiter = New Waiter()
75+
Dim service = New TestDiagnosticAnalyzerService()
76+
Dim source = New ExternalErrorDiagnosticUpdateSource(workspace, service, New MockDiagnosticUpdateSourceRegistrationService(), waiter)
77+
78+
Dim project = workspace.CurrentSolution.Projects.First()
79+
source.OnSolutionBuild(Me, Shell.UIContextChangedEventArgs.From(True))
80+
81+
Parallel.For(0, 100, Sub(i As Integer) source.SupportedDiagnosticId(project.Id, "CS1002"))
82+
End Using
83+
End Sub
84+
7185
<Fact>
7286
Public Async Function TestExternalDiagnostics_DuplicatedError() As Task
7387
Using workspace = TestWorkspace.CreateCSharp(String.Empty)

0 commit comments

Comments
 (0)