@@ -601,12 +601,14 @@ private void DoOneRepl(CancellationToken cancellationToken)
601
601
return ;
602
602
}
603
603
604
- // If we started the debug server, then on each REPL we need to check if we're still
605
- // actively debugging, and if not, stop the server.
606
- if ( DebugContext . OwnsDebugServerState && ! CurrentRunspace . Runspace . Debugger . InBreakpoint )
604
+ // We use the REPL as a poll to check if the debug context is active but PowerShell
605
+ // indicates we're no longer debugging. This happens when PowerShell was used to start
606
+ // the debugger (instead of using a Code launch configuration) via Wait-Debugger or
607
+ // simply hitting a PSBreakpoint. We need to synchronize the state and stop the debug
608
+ // context (and likely the debug server).
609
+ if ( DebugContext . IsActive && ! CurrentRunspace . Runspace . Debugger . InBreakpoint )
607
610
{
608
- DebugContext . OwnsDebugServerState = false ;
609
- _languageServer ? . SendNotification ( "powerShell/stopDebugger" ) ;
611
+ StopDebugContext ( ) ;
610
612
}
611
613
612
614
// When a task must run in the foreground, we cancel out of the idle loop and return to the top level.
@@ -633,8 +635,7 @@ private void DoOneRepl(CancellationToken cancellationToken)
633
635
// However, we must distinguish the last two scenarios, since PSRL will not print a new line in those cases.
634
636
if ( string . IsNullOrEmpty ( userInput ) )
635
637
{
636
- if ( cancellationToken . IsCancellationRequested
637
- || LastKeyWasCtrlC ( ) )
638
+ if ( cancellationToken . IsCancellationRequested || LastKeyWasCtrlC ( ) )
638
639
{
639
640
UI . WriteLine ( ) ;
640
641
}
@@ -834,7 +835,12 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
834
835
835
836
private void OnCancelKeyPress ( object sender , ConsoleCancelEventArgs args )
836
837
{
838
+ // We need to cancel the current task.
837
839
_cancellationContext . CancelCurrentTask ( ) ;
840
+
841
+ // If the current task was running under the debugger, we need to synchronize the
842
+ // cancelation with our debug context (and likely the debug server).
843
+ StopDebugContext ( ) ;
838
844
}
839
845
840
846
private ConsoleKeyInfo ReadKey ( bool intercept )
@@ -854,13 +860,26 @@ private bool LastKeyWasCtrlC()
854
860
&& _lastKey . Value . IsCtrlC ( ) ;
855
861
}
856
862
863
+ private void StopDebugContext ( )
864
+ {
865
+ // We are officially stopping the debugger.
866
+ DebugContext . IsActive = false ;
867
+
868
+ // If the debug server is active, we need to synchronize state and stop it.
869
+ if ( DebugContext . IsDebugServerActive )
870
+ {
871
+ _languageServer ? . SendNotification ( "powerShell/stopDebugger" ) ;
872
+ }
873
+ }
874
+
857
875
private void OnDebuggerStopped ( object sender , DebuggerStopEventArgs debuggerStopEventArgs )
858
876
{
877
+ // The debugger has officially started. We use this to later check if we should stop it.
878
+ DebugContext . IsActive = true ;
879
+
880
+ // If the debug server is NOT active, we need to synchronize state and start it.
859
881
if ( ! DebugContext . IsDebugServerActive )
860
882
{
861
- // If the we've hit a breakpoint and the debug server is not active, then we need to
862
- // start it (and own stopping it later).
863
- DebugContext . OwnsDebugServerState = true ;
864
883
_languageServer ? . SendNotification ( "powerShell/startDebugger" ) ;
865
884
}
866
885
0 commit comments