Skip to content

Commit a383c77

Browse files
Catch OperationCanceledException in both command loops (#1850)
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.
1 parent b30934e commit a383c77

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,10 @@ protected override void EndProcessing()
228228

229229
using EditorServicesLoader psesLoader = EditorServicesLoader.Create(_logger, editorServicesConfig, sessionFileWriter, _loggerUnsubscribers);
230230
_logger.Log(PsesLogLevel.Verbose, "Loading EditorServices");
231-
// Start editor services and wait here until it shuts down
231+
// Synchronously start editor services and wait here until it shuts down.
232+
#pragma warning disable VSTHRD002
232233
psesLoader.LoadAndRunEditorServicesAsync().GetAwaiter().GetResult();
234+
#pragma warning restore VSTHRD002
233235
}
234236
catch (Exception e)
235237
{

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,14 @@ private void RunExecutionLoop(bool isForDebug = false)
771771
&& !cancellationScope.CancellationToken.IsCancellationRequested
772772
&& _taskQueue.TryTake(out ISynchronousTask task))
773773
{
774-
task.ExecuteSynchronously(cancellationScope.CancellationToken);
774+
try
775+
{
776+
task.ExecuteSynchronously(cancellationScope.CancellationToken);
777+
}
778+
catch (OperationCanceledException e)
779+
{
780+
_logger.LogDebug(e, "Task {Task} was canceled!", task);
781+
}
775782
}
776783

777784
if (_shouldExit

test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public ExtensionCommandTests()
3333
serviceProvider: null,
3434
editorOperations: null,
3535
executionService: psesHost);
36+
#pragma warning disable VSTHRD002
3637
extensionService.InitializeAsync().Wait();
38+
#pragma warning restore VSTHRD002
3739
extensionCommandService = new(extensionService);
3840
}
3941

@@ -110,7 +112,8 @@ await psesHost.ExecutePSCommandAsync(
110112
Assert.Equal(commandName, commandAdded.Name);
111113
Assert.Equal(commandDisplayName, commandAdded.DisplayName);
112114

113-
// Invoke the command
115+
// Invoke the command.
116+
// TODO: What task was this cancelling?
114117
await extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext).ConfigureAwait(true);
115118

116119
// Assert the expected value

0 commit comments

Comments
 (0)