From db8bdbf24344451d380690352e2df5b81cc3ac71 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Fri, 31 Mar 2023 10:55:54 -0700 Subject: [PATCH 1/2] Fix for livelock in DiagnosticComputer --- .../DiagnosticAnalyzer/DiagnosticComputer.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs b/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs index 87af77bad43e4..1878e3a734da4 100644 --- a/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs +++ b/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs @@ -14,11 +14,12 @@ using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.PooledObjects; using Microsoft.CodeAnalysis.Shared.Extensions; +using Microsoft.CodeAnalysis.Shared.TestHooks; using Microsoft.CodeAnalysis.Telemetry; using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Workspaces.Diagnostics; -using Microsoft.VisualStudio.Threading; using Roslyn.Utilities; +using static Microsoft.VisualStudio.Threading.ThreadingTools; namespace Microsoft.CodeAnalysis.Remote.Diagnostics { @@ -303,8 +304,21 @@ static async Task WaitForHighPriorityTasksAsync(CancellationToken cancellationTo foreach (var task in highPriorityTasksToAwait) { cancellationToken.ThrowIfCancellationRequested(); - await task.WithCancellation(cancellationToken).NoThrowAwaitable(false); + + await WaitForHighPriorityTaskAsync(task, cancellationToken).ConfigureAwait(false); + } + } + + static async Task WaitForHighPriorityTaskAsync(Task task, CancellationToken cancellationToken) + { + if (task.IsCompleted) + { + // Make sure to yield so continuations of 'task' can make progress. + await Task.Yield().ConfigureAwait(false); + return; } + + await task.WithCancellation(cancellationToken).NoThrowAwaitableInternal(false); } } } From a7035c5816462a34e5aca5c01a91faf7ce42daa6 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Fri, 31 Mar 2023 11:25:11 -0700 Subject: [PATCH 2/2] Address feedback --- .../DiagnosticAnalyzer/DiagnosticComputer.cs | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs b/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs index 1878e3a734da4..2355bc5317845 100644 --- a/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs +++ b/src/Workspaces/Remote/ServiceHub/Services/DiagnosticAnalyzer/DiagnosticComputer.cs @@ -304,21 +304,16 @@ static async Task WaitForHighPriorityTasksAsync(CancellationToken cancellationTo foreach (var task in highPriorityTasksToAwait) { cancellationToken.ThrowIfCancellationRequested(); - - await WaitForHighPriorityTaskAsync(task, cancellationToken).ConfigureAwait(false); - } - } - - static async Task WaitForHighPriorityTaskAsync(Task task, CancellationToken cancellationToken) - { - if (task.IsCompleted) - { - // Make sure to yield so continuations of 'task' can make progress. - await Task.Yield().ConfigureAwait(false); - return; + if (task.IsCompleted) + { + // Make sure to yield so continuations of 'task' can make progress. + await Task.Yield().ConfigureAwait(false); + } + else + { + await task.WithCancellation(cancellationToken).NoThrowAwaitableInternal(false); + } } - - await task.WithCancellation(cancellationToken).NoThrowAwaitableInternal(false); } } }