From 01334e2b83b062e9d088e5c46f7b3dd5dcc5c1ab Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 1 Jul 2022 14:16:45 -0700 Subject: [PATCH] Catch `OperationCanceledException` in both command loops Our flaky extension command test seems to be flaky because sometimes another task gets queued, and since it runs in the foreground it cancels that task. Interactively, this happens in the first loop (with `DoOneRepl`) which catches the cancellation exception; but when under tests that is a no-op, so it happens in the second loop. Hence we should duplicate the same logic and so catch that exception. --- .../Commands/StartEditorServicesCommand.cs | 4 +++- .../Services/PowerShell/Host/PsesInternalHost.cs | 9 ++++++++- .../Extensions/ExtensionCommandTests.cs | 5 ++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs b/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs index 3d31ede06..325e7220f 100644 --- a/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs +++ b/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs @@ -228,8 +228,10 @@ protected override void EndProcessing() using EditorServicesLoader psesLoader = EditorServicesLoader.Create(_logger, editorServicesConfig, sessionFileWriter, _loggerUnsubscribers); _logger.Log(PsesLogLevel.Verbose, "Loading EditorServices"); - // Start editor services and wait here until it shuts down + // Synchronously start editor services and wait here until it shuts down. +#pragma warning disable VSTHRD002 psesLoader.LoadAndRunEditorServicesAsync().GetAwaiter().GetResult(); +#pragma warning restore VSTHRD002 } catch (Exception e) { diff --git a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs index 97ee2edd7..cca3ad156 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs @@ -769,7 +769,14 @@ private void RunExecutionLoop(bool isForDebug = false) && !cancellationScope.CancellationToken.IsCancellationRequested && _taskQueue.TryTake(out ISynchronousTask task)) { - task.ExecuteSynchronously(cancellationScope.CancellationToken); + try + { + task.ExecuteSynchronously(cancellationScope.CancellationToken); + } + catch (OperationCanceledException e) + { + _logger.LogDebug(e, "Task {Task} was canceled!", task); + } } if (_shouldExit diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs index 2ae739c26..b2e5e6b46 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs @@ -33,7 +33,9 @@ public ExtensionCommandTests() serviceProvider: null, editorOperations: null, executionService: psesHost); +#pragma warning disable VSTHRD002 extensionService.InitializeAsync().Wait(); +#pragma warning restore VSTHRD002 extensionCommandService = new(extensionService); } @@ -110,7 +112,8 @@ await psesHost.ExecutePSCommandAsync( Assert.Equal(commandName, commandAdded.Name); Assert.Equal(commandDisplayName, commandAdded.DisplayName); - // Invoke the command + // Invoke the command. + // TODO: What task was this cancelling? await extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext).ConfigureAwait(true); // Assert the expected value