From 037c2e0f16625a61e937809b6220242896f247ae Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Thu, 26 May 2022 14:37:30 -0400 Subject: [PATCH] Set `IsDebuggingRemoteRunspace` sooner for attach Since we're not setting `DebugContext.IsActive` sooner, we also need to set `IsDebuggingRemoteRunspace` early as well. Otherwise `DoOneRepl` sees `IsActive`, but not in a breakpoint, and stops debugging. --- .../Handlers/LaunchAndAttachHandler.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs b/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs index 8e5e758f2..4b7cd9acc 100644 --- a/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs +++ b/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs @@ -216,6 +216,26 @@ public async Task Handle(PsesLaunchRequestArguments request, Can } public async Task Handle(PsesAttachRequestArguments request, CancellationToken cancellationToken) + { + // We want to set this as early as possible to avoid an early `StopDebugging` call in + // DoOneRepl. There's too many places to reset this if it fails so we're wrapping the + // entire method in a try here to reset it if failed. + // + // TODO: Ideally DoOneRepl would be paused until the attach is fully initialized, though + // the current architecture makes that challenging. + _debugService.IsDebuggingRemoteRunspace = true; + try + { + return await HandleImpl(request, cancellationToken).ConfigureAwait(false); + } + catch + { + _debugService.IsDebuggingRemoteRunspace = false; + throw; + } + } + + private async Task HandleImpl(PsesAttachRequestArguments request, CancellationToken cancellationToken) { // The debugger has officially started. We use this to later check if we should stop it. ((PsesInternalHost)_executionService).DebugContext.IsActive = true; @@ -413,7 +433,6 @@ await _executionService.ExecutePSCommandAsync( // Clear any existing breakpoints before proceeding await _breakpointService.RemoveAllBreakpointsAsync().ConfigureAwait(continueOnCapturedContext: false); - _debugService.IsDebuggingRemoteRunspace = true; _debugStateService.WaitingForAttach = true; Task nonAwaitedTask = _executionService .ExecutePSCommandAsync(debugRunspaceCmd, CancellationToken.None, PowerShellExecutionOptions.ImmediateInteractive)