From 18dccd2a7e79c13b2393e1e3895f44d0db28b4bd Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 28 Mar 2019 13:43:26 -0700 Subject: [PATCH] Clean up and pop dead runspace when using 'attach' --- .../Session/PowerShellContext.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index cf1206b0f..25ba1d673 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -522,7 +522,7 @@ await Task.Factory.StartNew>( if (!e.SerializedRemoteException.TypeNames[0].EndsWith("PipelineStoppedException")) { // Rethrow anything that isn't a PipelineStoppedException - throw e; + throw; } } @@ -608,7 +608,18 @@ await Task.Factory.StartNew>( } finally { - // Get the new prompt before releasing the runspace handle + // 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(); + this.PopRunspace(); + } + + // Get the new prompt before releasing the runspace:?> handle if (executionOptions.WriteOutputToHost) { SessionDetails sessionDetails = null; @@ -792,10 +803,10 @@ public async Task ExecuteScriptWithArgs(string script, string arguments = null, var strBld = new StringBuilder(); - // The script parameter can refer to either a "script path" or a "command name". If it is a + // The script parameter can refer to either a "script path" or a "command name". If it is a // script path, we can determine that by seeing if the path exists. If so, we always single // quote that path in case it includes special PowerShell characters like ', &, (, ), [, ] and - // . Any embedded single quotes are escaped. + // . Any embedded single quotes are escaped. // If the provided path is already quoted, then File.Exists will not find it. // This keeps us from quoting an already quoted path. // Related to issue #123.