@@ -601,12 +601,14 @@ private void DoOneRepl(CancellationToken cancellationToken)
601601 return ;
602602 }
603603
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 )
607610 {
608- DebugContext . OwnsDebugServerState = false ;
609- _languageServer ? . SendNotification ( "powerShell/stopDebugger" ) ;
611+ StopDebugContext ( ) ;
610612 }
611613
612614 // 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)
633635 // However, we must distinguish the last two scenarios, since PSRL will not print a new line in those cases.
634636 if ( string . IsNullOrEmpty ( userInput ) )
635637 {
636- if ( cancellationToken . IsCancellationRequested
637- || LastKeyWasCtrlC ( ) )
638+ if ( cancellationToken . IsCancellationRequested || LastKeyWasCtrlC ( ) )
638639 {
639640 UI . WriteLine ( ) ;
640641 }
@@ -834,7 +835,12 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
834835
835836 private void OnCancelKeyPress ( object sender , ConsoleCancelEventArgs args )
836837 {
838+ // We need to cancel the current task.
837839 _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 ( ) ;
838844 }
839845
840846 private ConsoleKeyInfo ReadKey ( bool intercept )
@@ -854,13 +860,26 @@ private bool LastKeyWasCtrlC()
854860 && _lastKey . Value . IsCtrlC ( ) ;
855861 }
856862
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+
857875 private void OnDebuggerStopped ( object sender , DebuggerStopEventArgs debuggerStopEventArgs )
858876 {
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.
859881 if ( ! DebugContext . IsDebugServerActive )
860882 {
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 ;
864883 _languageServer ? . SendNotification ( "powerShell/startDebugger" ) ;
865884 }
866885
0 commit comments