Skip to content

Commit 2faa66a

Browse files
Clean up and pop dead runspace when using 'attach' for V2 (#897)
* Clean up and pop dead runspace when using 'attach' * named param * big comment
1 parent 38cb599 commit 2faa66a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/PowerShellEditorServices/Session/PowerShell5Operations.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
8585

8686
public void StopCommandInDebugger(PowerShellContext powerShellContext)
8787
{
88-
powerShellContext.CurrentRunspace.Runspace.Debugger.StopProcessCommand();
88+
// If the RunspaceAvailability is None, the runspace is dead and we should not try to run anything in it.
89+
if (powerShellContext.CurrentRunspace.Runspace.RunspaceAvailability != RunspaceAvailability.None)
90+
{
91+
powerShellContext.CurrentRunspace.Runspace.Debugger.StopProcessCommand();
92+
}
8993
}
9094

9195
public void ExitNestedPrompt(PSHost host)

src/PowerShellEditorServices/Session/PowerShellContext.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,21 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
765765
executionOptions,
766766
true);
767767

768-
throw e;
768+
throw;
769769
}
770770
finally
771771
{
772+
// If the RunspaceAvailability is None, it means that the runspace we're in is dead.
773+
// If this is the case, we should abort the execution which will clean up the runspace
774+
// (and clean up the debugger) and then pop it off the stack.
775+
// An example of when this happens is when the "attach" debug config is used and the
776+
// process you're attached to dies randomly.
777+
if (this.CurrentRunspace.Runspace.RunspaceAvailability == RunspaceAvailability.None)
778+
{
779+
this.AbortExecution(shouldAbortDebugSession: true);
780+
this.PopRunspace();
781+
}
782+
772783
// Get the new prompt before releasing the runspace handle
773784
if (executionOptions.WriteOutputToHost)
774785
{

0 commit comments

Comments
 (0)