Skip to content

Commit 721745f

Browse files
Clean up and pop dead runspace when using 'attach' (#896)
1 parent f7cac50 commit 721745f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/PowerShellEditorServices/Session/PowerShellContext.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
522522
if (!e.SerializedRemoteException.TypeNames[0].EndsWith("PipelineStoppedException"))
523523
{
524524
// Rethrow anything that isn't a PipelineStoppedException
525-
throw e;
525+
throw;
526526
}
527527
}
528528

@@ -608,7 +608,18 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
608608
}
609609
finally
610610
{
611-
// Get the new prompt before releasing the runspace handle
611+
// If the RunspaceAvailability is None, it means that the runspace we're in is dead.
612+
// If this is the case, we should abort the execution which will clean up the runspace
613+
// (and clean up the debugger) and then pop it off the stack.
614+
// An example of when this happens is when the "attach" debug config is used and the
615+
// process you're attached to dies randomly.
616+
if (this.CurrentRunspace.Runspace.RunspaceAvailability == RunspaceAvailability.None)
617+
{
618+
this.AbortExecution();
619+
this.PopRunspace();
620+
}
621+
622+
// Get the new prompt before releasing the runspace:?> handle
612623
if (executionOptions.WriteOutputToHost)
613624
{
614625
SessionDetails sessionDetails = null;
@@ -792,10 +803,10 @@ public async Task ExecuteScriptWithArgs(string script, string arguments = null,
792803

793804
var strBld = new StringBuilder();
794805

795-
// The script parameter can refer to either a "script path" or a "command name". If it is a
806+
// The script parameter can refer to either a "script path" or a "command name". If it is a
796807
// script path, we can determine that by seeing if the path exists. If so, we always single
797808
// quote that path in case it includes special PowerShell characters like ', &, (, ), [, ] and
798-
// <space>. Any embedded single quotes are escaped.
809+
// <space>. Any embedded single quotes are escaped.
799810
// If the provided path is already quoted, then File.Exists will not find it.
800811
// This keeps us from quoting an already quoted path.
801812
// Related to issue #123.

0 commit comments

Comments
 (0)