From 99230ec2c970229ac60dda4e2ebfe8175fb57166 Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Fri, 24 May 2019 13:12:38 -0400 Subject: [PATCH] Properly thread through cancellation through a bunch of our RPC services (#34065) * Property thread through cancellation through a bunch of our RPC services. * Tokens * Fix * Revert --- .../SymbolSearchUpdateEngine.Update.cs | 16 +++++------ .../SymbolSearchUpdateEngineFactory.cs | 24 ++++++++--------- ...udioSymbolSearchService.ProgressService.cs | 13 ++++----- ...ualStudioSymbolSearchService.LogService.cs | 5 ++-- .../RemoteHostClientServiceFactoryTests.cs | 12 ++++----- .../SymbolSearchUpdateEngineTests.vb | 12 ++++----- .../SymbolSearch/ISymbolSearchLogService.cs | 5 ++-- .../ISymbolSearchProgressService.cs | 17 ++++++------ .../CodeAnalysisService_SymbolFinder.cs | 7 +++-- .../RemoteSymbolSearchUpdateEngine.cs | 27 ++++++++++--------- 10 files changed, 73 insertions(+), 65 deletions(-) diff --git a/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngine.Update.cs b/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngine.Update.cs index 185d6fe2433..afa41714cbd 100644 --- a/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngine.Update.cs +++ b/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngine.Update.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Concurrent; @@ -59,9 +59,9 @@ internal partial class SymbolSearchUpdateEngine private readonly IDatabaseFactoryService _databaseFactoryService; private readonly Func _reportAndSwallowException; - private Task LogInfoAsync(string text) => _logService.LogInfoAsync(text); + private Task LogInfoAsync(string text) => _logService.LogInfoAsync(text, _updateCancellationToken); - private Task LogExceptionAsync(Exception e, string text) => _logService.LogExceptionAsync(e.ToString(), text); + private Task LogExceptionAsync(Exception e, string text) => _logService.LogExceptionAsync(e.ToString(), text, _updateCancellationToken); public Task UpdateContinuouslyAsync(string source, string localSettingsDirectory) { @@ -227,24 +227,24 @@ private async Task DownloadFullDatabaseAsync() try { var title = string.Format(EditorFeaturesWpfResources.Downloading_IntelliSense_index_for_0, _source); - await _service._progressService.OnDownloadFullDatabaseStartedAsync(title).ConfigureAwait(false); + await _service._progressService.OnDownloadFullDatabaseStartedAsync(title, _service._updateCancellationToken).ConfigureAwait(false); var (succeeded, delay) = await DownloadFullDatabaseWorkerAsync().ConfigureAwait(false); if (succeeded) { - await _service._progressService.OnDownloadFullDatabaseSucceededAsync().ConfigureAwait(false); + await _service._progressService.OnDownloadFullDatabaseSucceededAsync(_service._updateCancellationToken).ConfigureAwait(false); } else { await _service._progressService.OnDownloadFullDatabaseFailedAsync( - EditorFeaturesWpfResources.Downloading_index_failed).ConfigureAwait(false); + EditorFeaturesWpfResources.Downloading_index_failed, _service._updateCancellationToken).ConfigureAwait(false); } return delay; } catch (OperationCanceledException) { - await _service._progressService.OnDownloadFullDatabaseCanceledAsync().ConfigureAwait(false); + await _service._progressService.OnDownloadFullDatabaseCanceledAsync(_service._updateCancellationToken).ConfigureAwait(false); throw; } catch (Exception e) @@ -252,7 +252,7 @@ private async Task DownloadFullDatabaseAsync() var message = string.Format( EditorFeaturesWpfResources.Downloading_index_failed_0, "\r\n" + e.ToString()); - await _service._progressService.OnDownloadFullDatabaseFailedAsync(message).ConfigureAwait(false); + await _service._progressService.OnDownloadFullDatabaseFailedAsync(message, _service._updateCancellationToken).ConfigureAwait(false); throw; } } diff --git a/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngineFactory.cs b/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngineFactory.cs index b6d81f69dd7..6892872a514 100644 --- a/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngineFactory.cs +++ b/src/EditorFeatures/Core.Wpf/SymbolSearch/SymbolSearchUpdateEngineFactory.cs @@ -102,23 +102,23 @@ public CallbackObject(ISymbolSearchLogService logService, ISymbolSearchProgressS _progressService = progressService; } - public Task LogExceptionAsync(string exception, string text) - => _logService.LogExceptionAsync(exception, text); + public Task LogExceptionAsync(string exception, string text, CancellationToken cancellationToken) + => _logService.LogExceptionAsync(exception, text, cancellationToken); - public Task LogInfoAsync(string text) - => _logService.LogInfoAsync(text); + public Task LogInfoAsync(string text, CancellationToken cancellationToken) + => _logService.LogInfoAsync(text, cancellationToken); - public Task OnDownloadFullDatabaseStartedAsync(string title) - => _progressService.OnDownloadFullDatabaseStartedAsync(title); + public Task OnDownloadFullDatabaseStartedAsync(string title, CancellationToken cancellationToken) + => _progressService.OnDownloadFullDatabaseStartedAsync(title, cancellationToken); - public Task OnDownloadFullDatabaseSucceededAsync() - => _progressService.OnDownloadFullDatabaseSucceededAsync(); + public Task OnDownloadFullDatabaseSucceededAsync(CancellationToken cancellation) + => _progressService.OnDownloadFullDatabaseSucceededAsync(cancellation); - public Task OnDownloadFullDatabaseCanceledAsync() - => _progressService.OnDownloadFullDatabaseCanceledAsync(); + public Task OnDownloadFullDatabaseCanceledAsync(CancellationToken cancellationToken) + => _progressService.OnDownloadFullDatabaseCanceledAsync(cancellationToken); - public Task OnDownloadFullDatabaseFailedAsync(string message) - => _progressService.OnDownloadFullDatabaseFailedAsync(message); + public Task OnDownloadFullDatabaseFailedAsync(string message, CancellationToken cancellationToken) + => _progressService.OnDownloadFullDatabaseFailedAsync(message, cancellationToken); } } } diff --git a/src/VisualStudio/Core/Def/Implementation/SymbolSearch/VisualStudioSymbolSearchService.ProgressService.cs b/src/VisualStudio/Core/Def/Implementation/SymbolSearch/VisualStudioSymbolSearchService.ProgressService.cs index ad63dfa2a51..9c2cad2d9c9 100644 --- a/src/VisualStudio/Core/Def/Implementation/SymbolSearch/VisualStudioSymbolSearchService.ProgressService.cs +++ b/src/VisualStudio/Core/Def/Implementation/SymbolSearch/VisualStudioSymbolSearchService.ProgressService.cs @@ -3,6 +3,7 @@ using System; using System.Composition; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.ErrorReporting; using Microsoft.CodeAnalysis.Host.Mef; @@ -28,18 +29,18 @@ public VisualStudioSymbolSearchProgressService(VSShell.SVsServiceProvider servic (IVsTaskStatusCenterService)serviceProvider.GetService(typeof(SVsTaskStatusCenterService))); } - public async Task OnDownloadFullDatabaseStartedAsync(string title) + public async Task OnDownloadFullDatabaseStartedAsync(string title, CancellationToken cancellationToken) { try { - await OnDownloadFullDatabaseStartedWorkerAsync(title).ConfigureAwait(false); + await OnDownloadFullDatabaseStartedWorkerAsync(title, cancellationToken).ConfigureAwait(false); } catch (Exception e) when (FatalError.ReportWithoutCrashUnlessCanceled(e)) { } } - private Task OnDownloadFullDatabaseStartedWorkerAsync(string title) + private Task OnDownloadFullDatabaseStartedWorkerAsync(string title, CancellationToken cancellationToken) { var options = GetOptions(title); var data = new TaskProgressData @@ -78,7 +79,7 @@ private static TaskHandlerOptions GetOptions(string title) return options; } - public Task OnDownloadFullDatabaseSucceededAsync() + public Task OnDownloadFullDatabaseSucceededAsync(CancellationToken cancellation) { lock (_gate) { @@ -87,7 +88,7 @@ public Task OnDownloadFullDatabaseSucceededAsync() } } - public Task OnDownloadFullDatabaseCanceledAsync() + public Task OnDownloadFullDatabaseCanceledAsync(CancellationToken cancellationToken) { lock (_gate) { @@ -96,7 +97,7 @@ public Task OnDownloadFullDatabaseCanceledAsync() } } - public Task OnDownloadFullDatabaseFailedAsync(string message) + public Task OnDownloadFullDatabaseFailedAsync(string message, CancellationToken cancellationToken) { lock (_gate) { diff --git a/src/VisualStudio/Core/Def/SymbolSearch/VisualStudioSymbolSearchService.LogService.cs b/src/VisualStudio/Core/Def/SymbolSearch/VisualStudioSymbolSearchService.LogService.cs index 2f809ce1014..6a42ffd0ac1 100644 --- a/src/VisualStudio/Core/Def/SymbolSearch/VisualStudioSymbolSearchService.LogService.cs +++ b/src/VisualStudio/Core/Def/SymbolSearch/VisualStudioSymbolSearchService.LogService.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Editor.Shared.Utilities; using Microsoft.CodeAnalysis.SymbolSearch; @@ -23,12 +24,12 @@ public LogService(IThreadingContext threadingContext, IVsActivityLog activityLog _activityLog = activityLog; } - public Task LogInfoAsync(string text) + public Task LogInfoAsync(string text, CancellationToken cancellationToken) { return LogAsync(text, __ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION); } - public Task LogExceptionAsync(string exception, string text) + public Task LogExceptionAsync(string exception, string text, CancellationToken cancellationToken) { return LogAsync(text + ". " + exception, __ACTIVITYLOG_ENTRYTYPE.ALE_ERROR); } diff --git a/src/VisualStudio/Core/Test.Next/Remote/RemoteHostClientServiceFactoryTests.cs b/src/VisualStudio/Core/Test.Next/Remote/RemoteHostClientServiceFactoryTests.cs index 27fc5a8bef8..00b2b82f902 100644 --- a/src/VisualStudio/Core/Test.Next/Remote/RemoteHostClientServiceFactoryTests.cs +++ b/src/VisualStudio/Core/Test.Next/Remote/RemoteHostClientServiceFactoryTests.cs @@ -287,13 +287,13 @@ public Assembly LoadFromPath(string fullPath) private class MockLogAndProgressService : ISymbolSearchLogService, ISymbolSearchProgressService { - public Task LogExceptionAsync(string exception, string text) => Task.CompletedTask; - public Task LogInfoAsync(string text) => Task.CompletedTask; + public Task LogExceptionAsync(string exception, string text, CancellationToken cancellationToken) => Task.CompletedTask; + public Task LogInfoAsync(string text, CancellationToken cancellationToken) => Task.CompletedTask; - public Task OnDownloadFullDatabaseStartedAsync(string title) => Task.CompletedTask; - public Task OnDownloadFullDatabaseSucceededAsync() => Task.CompletedTask; - public Task OnDownloadFullDatabaseCanceledAsync() => Task.CompletedTask; - public Task OnDownloadFullDatabaseFailedAsync(string message) => Task.CompletedTask; + public Task OnDownloadFullDatabaseStartedAsync(string title, CancellationToken cancellationToken) => Task.CompletedTask; + public Task OnDownloadFullDatabaseSucceededAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task OnDownloadFullDatabaseCanceledAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task OnDownloadFullDatabaseFailedAsync(string message, CancellationToken cancellationToken) => Task.CompletedTask; } } } diff --git a/src/VisualStudio/Core/Test/SymbolSearch/SymbolSearchUpdateEngineTests.vb b/src/VisualStudio/Core/Test/SymbolSearch/SymbolSearchUpdateEngineTests.vb index dee8ab4dfd4..8360f58ed0b 100644 --- a/src/VisualStudio/Core/Test/SymbolSearch/SymbolSearchUpdateEngineTests.vb +++ b/src/VisualStudio/Core/Test/SymbolSearch/SymbolSearchUpdateEngineTests.vb @@ -751,11 +751,11 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.SymbolSearch Private Sub New() End Sub - Public Function LogExceptionAsync(exception As String, text As String) As Task Implements ISymbolSearchLogService.LogExceptionAsync + Public Function LogExceptionAsync(exception As String, text As String, cancellationToken As CancellationToken) As Task Implements ISymbolSearchLogService.LogExceptionAsync Return Task.CompletedTask End Function - Public Function LogInfoAsync(text As String) As Task Implements ISymbolSearchLogService.LogInfoAsync + Public Function LogInfoAsync(text As String, cancellationToken As CancellationToken) As Task Implements ISymbolSearchLogService.LogInfoAsync Return Task.CompletedTask End Function End Class @@ -768,19 +768,19 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.SymbolSearch Private Sub New() End Sub - Public Function OnDownloadFullDatabaseStartedAsync(title As String) As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseStartedAsync + Public Function OnDownloadFullDatabaseStartedAsync(title As String, cancellationToken As CancellationToken) As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseStartedAsync Return Task.CompletedTask End Function - Public Function OnDownloadFullDatabaseSucceededAsync() As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseSucceededAsync + Public Function OnDownloadFullDatabaseSucceededAsync(cancellationToken As CancellationToken) As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseSucceededAsync Return Task.CompletedTask End Function - Public Function OnDownloadFullDatabaseCanceledAsync() As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseCanceledAsync + Public Function OnDownloadFullDatabaseCanceledAsync(cancellationToken As CancellationToken) As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseCanceledAsync Return Task.CompletedTask End Function - Public Function OnDownloadFullDatabaseFailedAsync(message As String) As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseFailedAsync + Public Function OnDownloadFullDatabaseFailedAsync(message As String, cancellationToken As CancellationToken) As Task Implements ISymbolSearchProgressService.OnDownloadFullDatabaseFailedAsync Return Task.CompletedTask End Function End Class diff --git a/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchLogService.cs b/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchLogService.cs index f97844a1712..5af1be1aaf9 100644 --- a/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchLogService.cs +++ b/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchLogService.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Threading; using System.Threading.Tasks; namespace Microsoft.CodeAnalysis.SymbolSearch @@ -9,7 +10,7 @@ namespace Microsoft.CodeAnalysis.SymbolSearch /// internal interface ISymbolSearchLogService { - Task LogExceptionAsync(string exception, string text); - Task LogInfoAsync(string text); + Task LogExceptionAsync(string exception, string text, CancellationToken cancellationToken); + Task LogInfoAsync(string text, CancellationToken cancellationToken); } } diff --git a/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchProgressService.cs b/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchProgressService.cs index e0398777d0d..a37d9c3de05 100644 --- a/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchProgressService.cs +++ b/src/Workspaces/Core/Portable/SymbolSearch/ISymbolSearchProgressService.cs @@ -2,6 +2,7 @@ using System; using System.Composition; +using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Host; using Microsoft.CodeAnalysis.Host.Mef; @@ -11,11 +12,11 @@ namespace Microsoft.CodeAnalysis.SymbolSearch { internal interface ISymbolSearchProgressService : IWorkspaceService { - Task OnDownloadFullDatabaseStartedAsync(string title); + Task OnDownloadFullDatabaseStartedAsync(string title, CancellationToken cancellationToken); - Task OnDownloadFullDatabaseSucceededAsync(); - Task OnDownloadFullDatabaseCanceledAsync(); - Task OnDownloadFullDatabaseFailedAsync(string message); + Task OnDownloadFullDatabaseSucceededAsync(CancellationToken cancellationToken); + Task OnDownloadFullDatabaseCanceledAsync(CancellationToken cancellationToken); + Task OnDownloadFullDatabaseFailedAsync(string message, CancellationToken cancellationToken); } [ExportWorkspaceService(typeof(ISymbolSearchProgressService)), Shared] @@ -26,9 +27,9 @@ public DefaultSymbolSearchProgressService() { } - public Task OnDownloadFullDatabaseStartedAsync(string title) => Task.CompletedTask; - public Task OnDownloadFullDatabaseSucceededAsync() => Task.CompletedTask; - public Task OnDownloadFullDatabaseCanceledAsync() => Task.CompletedTask; - public Task OnDownloadFullDatabaseFailedAsync(string message) => Task.CompletedTask; + public Task OnDownloadFullDatabaseStartedAsync(string title, CancellationToken cancellationToken) => Task.CompletedTask; + public Task OnDownloadFullDatabaseSucceededAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task OnDownloadFullDatabaseCanceledAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task OnDownloadFullDatabaseFailedAsync(string message, CancellationToken cancellationToken) => Task.CompletedTask; } } diff --git a/src/Workspaces/Remote/ServiceHub/Services/CodeAnalysisService_SymbolFinder.cs b/src/Workspaces/Remote/ServiceHub/Services/CodeAnalysisService_SymbolFinder.cs index 14243ccfed3..dc7e4eb518c 100644 --- a/src/Workspaces/Remote/ServiceHub/Services/CodeAnalysisService_SymbolFinder.cs +++ b/src/Workspaces/Remote/ServiceHub/Services/CodeAnalysisService_SymbolFinder.cs @@ -163,7 +163,9 @@ private class FindLiteralReferencesProgressCallback : IStreamingFindLiteralRefer private readonly CodeAnalysisService _service; private readonly CancellationToken _cancellationToken; - public FindLiteralReferencesProgressCallback(CodeAnalysisService service, CancellationToken cancellationToken) + public FindLiteralReferencesProgressCallback( + CodeAnalysisService service, + CancellationToken cancellationToken) { _service = service; _cancellationToken = cancellationToken; @@ -181,7 +183,8 @@ private class FindReferencesProgressCallback : IStreamingFindReferencesProgress private readonly CodeAnalysisService _service; private readonly CancellationToken _cancellationToken; - public FindReferencesProgressCallback(CodeAnalysisService service, CancellationToken cancellationToken) + public FindReferencesProgressCallback( + CodeAnalysisService service, CancellationToken cancellationToken) { _service = service; _cancellationToken = cancellationToken; diff --git a/src/Workspaces/Remote/ServiceHub/Services/RemoteSymbolSearchUpdateEngine.cs b/src/Workspaces/Remote/ServiceHub/Services/RemoteSymbolSearchUpdateEngine.cs index 477aef2cbe0..d13cfe4a553 100644 --- a/src/Workspaces/Remote/ServiceHub/Services/RemoteSymbolSearchUpdateEngine.cs +++ b/src/Workspaces/Remote/ServiceHub/Services/RemoteSymbolSearchUpdateEngine.cs @@ -15,7 +15,8 @@ internal partial class RemoteSymbolSearchUpdateEngine : { private readonly SymbolSearchUpdateEngine _updateEngine; - public RemoteSymbolSearchUpdateEngine(Stream stream, IServiceProvider serviceProvider) + public RemoteSymbolSearchUpdateEngine( + Stream stream, IServiceProvider serviceProvider) : base(serviceProvider, stream) { _updateEngine = new SymbolSearchUpdateEngine( @@ -67,23 +68,23 @@ public Task> FindReferenceAssembliesWithT #region Messages to forward from here to VS - public Task LogExceptionAsync(string exception, string text) - => this.InvokeAsync(nameof(LogExceptionAsync), new object[] { exception, text }, CancellationToken.None); + public Task LogExceptionAsync(string exception, string text, CancellationToken cancellationToken) + => this.InvokeAsync(nameof(LogExceptionAsync), new object[] { exception, text }, cancellationToken); - public Task LogInfoAsync(string text) - => this.InvokeAsync(nameof(LogInfoAsync), new object[] { text }, CancellationToken.None); + public Task LogInfoAsync(string text, CancellationToken cancellationToken) + => this.InvokeAsync(nameof(LogInfoAsync), new object[] { text }, cancellationToken); - public Task OnDownloadFullDatabaseStartedAsync(string title) - => this.InvokeAsync(nameof(OnDownloadFullDatabaseStartedAsync), new object[] { title }, CancellationToken.None); + public Task OnDownloadFullDatabaseStartedAsync(string title, CancellationToken cancellationToken) + => this.InvokeAsync(nameof(OnDownloadFullDatabaseStartedAsync), new object[] { title }, cancellationToken); - public Task OnDownloadFullDatabaseSucceededAsync() - => this.InvokeAsync(nameof(OnDownloadFullDatabaseSucceededAsync), CancellationToken.None); + public Task OnDownloadFullDatabaseSucceededAsync(CancellationToken cancellationToken) + => this.InvokeAsync(nameof(OnDownloadFullDatabaseSucceededAsync), cancellationToken); - public Task OnDownloadFullDatabaseCanceledAsync() - => this.InvokeAsync(nameof(OnDownloadFullDatabaseCanceledAsync), CancellationToken.None); + public Task OnDownloadFullDatabaseCanceledAsync(CancellationToken cancellationToken) + => this.InvokeAsync(nameof(OnDownloadFullDatabaseCanceledAsync), cancellationToken); - public Task OnDownloadFullDatabaseFailedAsync(string message) - => this.InvokeAsync(nameof(OnDownloadFullDatabaseFailedAsync), new object[] { message }, CancellationToken.None); + public Task OnDownloadFullDatabaseFailedAsync(string message, CancellationToken cancellationToken) + => this.InvokeAsync(nameof(OnDownloadFullDatabaseFailedAsync), new object[] { message }, cancellationToken); #endregion } -- GitLab