Skip to content

Debugging with active breakpoint crashes Powershell session #3667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
fflaten opened this issue Nov 4, 2021 · 20 comments · Fixed by PowerShell/PowerShellEditorServices#1630
Closed
5 tasks done
Assignees
Labels
Area-Debugging Bug: Pre-release Bugs reproducing only in the pre-release extension. Issue-Bug A bug to squash.

Comments

@fflaten
Copy link
Contributor

fflaten commented Nov 4, 2021

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.

Summary

Powershell session crashes in preview extension when attempting to debug a script with active breakpoint in unsaved file. Debugging the same code in a saved file works.

Also occurs when debugging Pester tests, but was able to reproduce the same exception in this simpler demo so I believe it might be the same root cause.

PowerShell Version

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

1.62.0-insider
b3318bc0524af3d74034b8bb8a64df0ccf35549a
x64

Extension Version

Steps to Reproduce

  1. Open a new unsaved file with content $a = 1
  2. Place a breakpoint on the line and launch/debug file as normal

Visuals

Repro
pses3-debug-crash

Exception
115

Logs

No response

@fflaten fflaten added the Issue-Bug A bug to squash. label Nov 4, 2021
@ghost ghost added the Needs: Triage Maintainer attention needed! label Nov 4, 2021
@fflaten
Copy link
Contributor Author

fflaten commented Nov 4, 2021

@JustinGrote Can you repro this? Would've expected you to hit this bug while troubleshooting #3655, unless it's a local problem.

@andyleejordan andyleejordan added the Bug: Pre-release Bugs reproducing only in the pre-release extension. label Nov 4, 2021
@JustinGrote
Copy link
Collaborator

JustinGrote commented Nov 4, 2021

I can consistently reproduce if you:

  1. Start a debug session (I use start interactive session)
  2. Run a pester test

I get the same scopes issue. Doesn't happen if you don't have a debug session started.

@JustinGrote
Copy link
Collaborator

JustinGrote commented Nov 4, 2021

This particular issue (not the pester reproduction) also just happens with an unsaved file. If you save the file it works fine.

image

EDIT: Couldn't repro with an unsaved file either, tho the run syntax is a little strange and probably needs tweaking.
image

@andyleejordan
Copy link
Member

Definitely some debugging issues to work through...

@andyleejordan andyleejordan added Area-Debugging and removed Needs: Triage Maintainer attention needed! labels Nov 4, 2021
@andyleejordan andyleejordan self-assigned this Nov 4, 2021
@andyleejordan andyleejordan added this to the Committed-vNext milestone Nov 4, 2021
@JustinGrote
Copy link
Collaborator

Full stacktrace of being able to reproduce it, I'm pretty sure whatever the problem is happens before this point. I'll try to get a debug session going.

An error has occurred that was not properly handled. Additional information is shown below. The PowerShell process will exit.
Unhandled exception. System.Management.Automation.CmdletInvocationException: The scope number '3' exceeds the number of active scopes. (Parameter 'Scope')       
Actual value was 3.
 ---> System.Management.Automation.PSArgumentOutOfRangeException: The scope number '3' exceeds the number of active scopes. (Parameter 'Scope')
Actual value was 3.
   at System.Management.Automation.SessionStateInternal.GetScopeByID(Int32 scopeID)
   at System.Management.Automation.SessionStateInternal.GetScopeByID(String scopeID)
   at System.Management.Automation.SessionStateInternal.GetVariableTableAtScope(String scopeID)
   at Microsoft.PowerShell.Commands.VariableCommandBase.GetMatchingVariables(String name, String lookupScope, Boolean& wasFiltered, Boolean quiet)
   at Microsoft.PowerShell.Commands.GetVariableCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)     
   at System.Management.Automation.PowerShell.InvokeWithDebugger(IEnumerable`1 input, IList`1 output, PSInvocationSettings settings, Boolean invokeMustRun)      
   at System.Management.Automation.ScriptDebugger.ProcessCommand(PSCommand command, PSDataCollection`1 output)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteInDebugger(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 153
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 55
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 92
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.DebugService.FetchVariableContainerAsync(String scope, VariableContainerDetails autoVariables) in D:\a\1\s\src\PowerShellEditorServices\Services\DebugAdapter\DebugService.cs:line 690
   at Microsoft.PowerShell.EditorServices.Services.DebugService.FetchStackFramesAsync(String scriptNameOverride) in D:\a\1\s\src\PowerShellEditorServices\Services\DebugAdapter\DebugService.cs:line 818
   at Microsoft.PowerShell.EditorServices.Services.DebugService.FetchStackFramesAndVariablesAsync(String scriptNameOverride) in D:\a\1\s\src\PowerShellEditorServices\Services\DebugAdapter\DebugService.cs:line 661
   at Microsoft.PowerShell.EditorServices.Services.DebugService.OnDebuggerStopAsync(Object sender, DebuggerStopEventArgs e) in D:\a\1\s\src\PowerShellEditorServices\Services\DebugAdapter\DebugService.cs:line 920
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_1(Object state)
   at System.Threading.QueueUserWorkItemCallback.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Nov 4, 2021
@JustinGrote
Copy link
Collaborator

Looks normal up to this point, something in the _pwsh prcess setup?
image

@JustinGrote
Copy link
Collaborator

@JustinGrote
Copy link
Collaborator

Fails here, tries to do a Get-Variable -Scope 3
https://github.com/PowerShell/PowerShellEditorServices/blob/f825d89c5ef13423f78c5c3b68809a3daca9609f/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs#L687-L692

image

@JustinGrote
Copy link
Collaborator

I think I found the problem. This seems to assume the stacktrace length is the same as the number of variable scopes, which is going to not be true if any assemblies are involved I think.
image

@JustinGrote
Copy link
Collaborator

JustinGrote commented Nov 4, 2021

This doesn't account for a scope exception. Adding a try catch to swallow that specific exception here should have the expected result, at least unless we can find a more proactive way to determine if variables exist without having to run a command. Any advice @rjmholt @SeeminglyScience for how we can know ahead of time if a stackframe has local variables?

https://github.com/PowerShell/PowerShellEditorServices/blob/f825d89c5ef13423f78c5c3b68809a3daca9609f/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs#L690-L691

@SeeminglyScience
Copy link
Collaborator

This is a pretty old issue (#1856) though in the past it just polluted $Error so not much of an issue.

In the PR for the rewrite I made a comment about us setting ErrorActionPreference to Stop somewhere. @andschwa do you know if that got resolved? Non-terminating errors like parameter binding definitely should not be crashing or even hitting a catch.

@SeeminglyScience
Copy link
Collaborator

Some details about why this error occurs in general in this comment.

@JustinGrote
Copy link
Collaborator

This does look exactly what it is #1856 (comment)

@SeeminglyScience
Copy link
Collaborator

Yeah it comes up from time to time. I think the real problem is that a benign error is causing a crash. Ideally it would be a silent non-terminating error, but at the very least there's a catch somewhere that's too specific.

@JustinGrote
Copy link
Collaborator

Agreed, @andschwa there should be a broad catch for all otherwise-uncaught pwsh invocation exceptions that logs the exception occurred but doesn't kill PSES (or at the very worst auto-restarts it)

@SeeminglyScience
Copy link
Collaborator

Ah right parameter binding exceptions are terminating so not ErrorActionPreference related. Just a catch somewhere that's too narrow probably

@JustinGrote
Copy link
Collaborator

Draft Fix: PowerShell/PowerShellEditorServices#1620

@SeeminglyScience
Copy link
Collaborator

I think it might need to just be here. Maybe change the RuntimeException clause to Exception since really anything can be thrown there.

@JustinGrote
Copy link
Collaborator

I think it might need to just be here. Maybe change the RuntimeException clause to Exception since really anything can be thrown there.

Makes sense, @andschwa this should be a separate PR and it has wide reaching consequences so I'd ask you try this part :)

@JustinGrote JustinGrote self-assigned this Nov 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Debugging Bug: Pre-release Bugs reproducing only in the pre-release extension. Issue-Bug A bug to squash.
Projects
No open projects
Status: Done
5 participants