@@ -133,6 +133,7 @@ private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationT
133
133
}
134
134
catch ( Exception e ) when ( cancellationToken . IsCancellationRequested || e is PipelineStoppedException || e is PSRemotingDataStructureException )
135
135
{
136
+ StopDebuggerIfRemoteDebugSessionFailed ( ) ;
136
137
throw new OperationCanceledException ( ) ;
137
138
}
138
139
catch ( RuntimeException e )
@@ -193,6 +194,30 @@ private IReadOnlyList<TResult> ExecuteInDebugger(CancellationToken cancellationT
193
194
return results ;
194
195
}
195
196
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
+
196
221
private void CancelNormalExecution ( )
197
222
{
198
223
_pwsh . Stop ( ) ;
0 commit comments