From c88f7c26a459a02c017a1b66136992cfd487c869 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Tue, 19 Apr 2022 14:25:32 -0400 Subject: [PATCH 1/2] Fix logic error with IsUsable check We definitely do not want to ignore cancel requests when the runspace **is** usable. Oops --- .../Services/PowerShell/Execution/SynchronousPowerShellTask.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs index 636f18ce6..71accf212 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs @@ -333,7 +333,7 @@ private void StopDebuggerIfRemoteDebugSessionFailed() private void CancelNormalExecution() { - if (_pwsh.Runspace.RunspaceStateInfo.IsUsable()) + if (!_pwsh.Runspace.RunspaceStateInfo.IsUsable()) { return; } From 137662182b5b99af4abbdc2ced8dca3ec27d5cf0 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Tue, 19 Apr 2022 14:29:21 -0400 Subject: [PATCH 2/2] Move context frame pop logic around This should make default prompt spam significantly less likely or impossible. Also fixes a scenario where if the REPL loop is evaluating and in an attached process, closing the attachee would dead lock. --- .../PowerShell/Host/PsesInternalHost.cs | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs index 1917e3432..e0d2d15d3 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs @@ -680,18 +680,30 @@ private void RunExecutionLoop(bool isForDebug = false) } using CancellationScope cancellationScope = _cancellationContext.EnterScope(false); - DoOneRepl(cancellationScope.CancellationToken); + + try + { + DoOneRepl(cancellationScope.CancellationToken); + } + catch (OperationCanceledException) + { + if (isForDebug) + { + while (Runspace is { RunspaceIsRemote: true } remoteRunspace + && !remoteRunspace.RunspaceStateInfo.IsUsable()) + { + PopPowerShell(RunspaceChangeAction.Exit); + } + + return; + } + } while (!ShouldExitExecutionLoop && !cancellationScope.CancellationToken.IsCancellationRequested && _taskQueue.TryTake(out ISynchronousTask task)) { task.ExecuteSynchronously(cancellationScope.CancellationToken); - while (Runspace is { RunspaceIsRemote: true } remoteRunspace - && !remoteRunspace.RunspaceStateInfo.IsUsable()) - { - PopPowerShell(RunspaceChangeAction.Exit); - } } if (_shouldExit @@ -759,7 +771,7 @@ private void DoOneRepl(CancellationToken cancellationToken) } catch (OperationCanceledException) { - // Do nothing, since we were just cancelled + throw; } // Propagate exceptions thrown from the debugger when quitting. catch (TerminateException) @@ -828,6 +840,7 @@ public void WriteWithPrompt(PSCommand command, CancellationToken cancellationTok private string InvokeReadLine(CancellationToken cancellationToken) { + cancellationToken.ThrowIfCancellationRequested(); try { _readKeyCancellationToken = cancellationToken;