Skip to content

Commit 87a69cb

Browse files
committed
Deal with debug abort in WinPS
1 parent 4303ce9 commit 87a69cb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationT
133133
}
134134
catch (Exception e) when (cancellationToken.IsCancellationRequested || e is PipelineStoppedException || e is PSRemotingDataStructureException)
135135
{
136+
StopDebuggerIfRemoteDebugSessionFailed();
136137
throw new OperationCanceledException();
137138
}
138139
catch (RuntimeException e)
@@ -193,6 +194,30 @@ private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationT
193194
return results;
194195
}
195196

197+
private void StopDebuggerIfRemoteDebugSessionFailed()
198+
{
199+
// When remoting to Windows PowerShell,
200+
// command cancellation may cancel the remote debug session in a way that the local debug session doesn't detect.
201+
// Instead we have to query the remote directly
202+
if (_pwsh.Runspace.RunspaceIsRemote)
203+
{
204+
var assessDebuggerCommand = new PSCommand().AddScript("$Host.Runspace.Debugger.InBreakpoint");
205+
206+
var outputCollection = new PSDataCollection<PSObject>();
207+
_pwsh.Runspace.Debugger.ProcessCommand(assessDebuggerCommand, outputCollection);
208+
209+
foreach (PSObject output in outputCollection)
210+
{
211+
if (object.Equals(output?.BaseObject, false))
212+
{
213+
_psRunspaceContext.ProcessDebuggerResult(new DebuggerCommandResults(DebuggerResumeAction.Stop, evaluatedByDebugger: true));
214+
_logger.LogWarning("Cancelling debug session due to remote command cancellation causing the end of remote debugging session");
215+
_psHost.UI.WriteWarningLine("Debug session aborted by command cancellation. This is a known issue in the Windows PowerShell 5.1 remoting system.");
216+
}
217+
}
218+
}
219+
}
220+
196221
private void CancelNormalExecution()
197222
{
198223
_pwsh.Stop();

0 commit comments

Comments
 (0)