diff --git a/src/PowerShellEditorServices/Session/PowerShell5Operations.cs b/src/PowerShellEditorServices/Session/PowerShell5Operations.cs index e91c0edc8..20954ac21 100644 --- a/src/PowerShellEditorServices/Session/PowerShell5Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell5Operations.cs @@ -85,7 +85,11 @@ public IEnumerable ExecuteCommandInDebugger( public void StopCommandInDebugger(PowerShellContext powerShellContext) { - powerShellContext.CurrentRunspace.Runspace.Debugger.StopProcessCommand(); + // If the RunspaceAvailability is None, the runspace is dead and we should not try to run anything in it. + if (powerShellContext.CurrentRunspace.Runspace.RunspaceAvailability != RunspaceAvailability.None) + { + powerShellContext.CurrentRunspace.Runspace.Debugger.StopProcessCommand(); + } } public void ExitNestedPrompt(PSHost host) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index f5eb09494..2284a99ff 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -765,10 +765,21 @@ public async Task> ExecuteCommandAsync( executionOptions, true); - throw e; + throw; } finally { + // If the RunspaceAvailability is None, it means that the runspace we're in is dead. + // If this is the case, we should abort the execution which will clean up the runspace + // (and clean up the debugger) and then pop it off the stack. + // An example of when this happens is when the "attach" debug config is used and the + // process you're attached to dies randomly. + if (this.CurrentRunspace.Runspace.RunspaceAvailability == RunspaceAvailability.None) + { + this.AbortExecution(shouldAbortDebugSession: true); + this.PopRunspace(); + } + // Get the new prompt before releasing the runspace handle if (executionOptions.WriteOutputToHost) {