From 803a0aa49725950543ddd6efa209803aa3e62613 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 28 Mar 2019 17:46:42 -0700 Subject: [PATCH 1/3] Clean up and pop dead runspace when using 'attach' --- .../Session/PowerShell5Operations.cs | 6 +++++- src/PowerShellEditorServices/Session/PowerShellContext.cs | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) 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..6fd66b1bd 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -765,10 +765,16 @@ public async Task> ExecuteCommandAsync( executionOptions, true); - throw e; + throw; } finally { + if (this.CurrentRunspace.Runspace.RunspaceAvailability == RunspaceAvailability.None) + { + this.AbortExecution(true); + this.PopRunspace(); + } + // Get the new prompt before releasing the runspace handle if (executionOptions.WriteOutputToHost) { From e3c2f9933457f027b094e5eda881c44e18aa5757 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 28 Mar 2019 17:53:51 -0700 Subject: [PATCH 2/3] named param --- src/PowerShellEditorServices/Session/PowerShellContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 6fd66b1bd..f28da8ac8 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -771,7 +771,7 @@ public async Task> ExecuteCommandAsync( { if (this.CurrentRunspace.Runspace.RunspaceAvailability == RunspaceAvailability.None) { - this.AbortExecution(true); + this.AbortExecution(shouldAbortDebugSession: true); this.PopRunspace(); } From 28f833831e9cb6b7a8d627660f525ab6884da1ef Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 28 Mar 2019 17:55:24 -0700 Subject: [PATCH 3/3] big comment --- src/PowerShellEditorServices/Session/PowerShellContext.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index f28da8ac8..2284a99ff 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -769,6 +769,11 @@ public async Task> ExecuteCommandAsync( } 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);