Repository navigation
Closed
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Interactive-Debuggingdebugging PowerShell script
Description
This issue is reproducible in PS 5.1 and 6.2.1 (and presumably 7 preview since the buggy code is still there, although I didn't test that version specifically yet).
If you debug a job, you enter a nested debugging session. From within that session, you can set, enable, disable, or remove breakpoints in the job debugger. Setting or removing breakpoints will result in the same changes made to any breakpoints with the corresponding IDs that were set in the parent debugger.
Steps to reproduce
# Create a job that does some stuff
$job = Start-Job -ScriptBlock {
1..1000 | ForEach-Object {
Start-Sleep -Seconds 1
$_
Write-Error 'boo'
Write-Verbose 'Verbose' -Verbose
$DebugPreference = 'Continue'
Write-Debug 'Debug'
Write-Warning 'Warning'
}
}
# Set some local breakpoints, and remove one
$bp1 = Set-PSBreakpoint -Command Write-Verbose
$bp2 = Set-PSBreakpoint -Command Get-Date
$bp3 = Set-PSBreakpoint -Variable Home -Mode Read -Action {Write-Host 'Read var'}
Remove-PSBreakpoint -Breakpoint $bp2
# Have a look at your local breakpoints (these do not impact the running job).
Get-PSBreakpoint
# Wait for the job and its child job to enter a running state
while ($job.State -ne 'Running' -or $job.ChildJobs[0].State -ne 'Running') {
Start-Sleep -Milliseconds 250
}
# Debug the job
Debug-Job -Job $jobAt this point you will be in the job debugger. From here, run the following commands:
# Create a bunch of breakpoints
$jbp1 = Set-PSBreakpoint -Variable DebugPreference
$jbp2 = Set-PSBreakpoint -Command Write-Debug
$jbp3 = Set-PSBreakpoint -Command Start-Sleep
# Have a look at your job breakpoints
Get-PSBreakpoint
# Disable the second breakpoint
Disable-PSBreakpoint -Breakpoint $jbp2
# Remove the last breakpoint
Remove-PSBreakpoint -Breakpoint $jbp3
# Detach your debugger
dNow you're back in your main runspace, outside of the job. Have a look at your breakpoints by invoking the following command:
Get-PSBreakpointThis output shows that:
- breakpoint 0 was overwritten with a completely different breakpoint (the first one that was set in the job)
- breakpoint 1 was re-created (remember that it was removed before debugging the job), and it's not the same as it was before (note that it is not disabled in the parent even though we disabled it in the job debugger because the code behind that simply disables the breakpoint object again, which is already disabled).
- breakpoint 2 was deleted (remember that it was removed from within the job debugging session)
Expected behavior
ID Script Line Command Variable Action
-- ------ ---- ------- -------- ------
0 Write-Verbose
2 Home Write-Host 'Read var'
Actual behavior
ID Script Line Command Variable Action
-- ------ ---- ------- -------- ------
0 DebugPreference
1 Write-Debug
Environment data
Name Value
---- -----
PSVersion 6.2.1
PSEdition Core
GitCommitId 6.2.1
OS Microsoft Windows 10.0.17763
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Metadata
Metadata
Assignees
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Interactive-Debuggingdebugging PowerShell script
Activity
KirkMunro commentedon Aug 21, 2019
I could use some additional eyes on the root cause behind this issue, because what is done in PowerShell right now is off, and I can fix it, but I don't have the history behind it.
The code in question is in debugger.cs, on lines 3227 to 3247.
That event handler is invoked in response to a breakpoint being updated in the local debugger or in any nested debugger. This includes the
PSTaskChildDebugger,RemotingJobDebugger,RemoteDebugger,ServerRemoteDebugger, andNestedRunspaceDebugger. That's why modifications to breakpoints in a remote runspace (in the example above, in a job) result in breakpoints changing in the local debugger. The local debuggerHandleBreakpointUpdatedevent receives details about the breakpoint that was changed in another runspace, and then tries to make the same changes in the local debugger.This behavior seems clearly wrong to me, and I'd like to fix it. My initial thought is: that event handler doesn't appear to be needed at all, so remove it. If I create, enable, disable, or remove a breakpoint in a local debugger, the breakpoint is created, enabled, disabled or removed via direct API invocation. If I do the same in a remote debugger, the same happens. Why then do we have an event handler that is notified when a breakpoint is updated in a remote session in PowerShell itself? The only logical reason I could come up with was to synchronize breakpoints across all runspaces, but we don't do that -- we manage breakpoints separately in each runspace.
At any rate, Paul Higinbotham (@PaulHigin), if you could have a look at this and share your thoughts, it would be appreciated.
PaulHigin commentedon Aug 23, 2019
This definitely doesn't sound right. I haven't looked at this in a long while, but much of it was added to support workflow script debugging, and I am not surprised it could use some re-factoring. I'll try to look at this early next week and give you my thoughts.
KirkMunro commentedon Sep 6, 2019
Hey Paul Higinbotham (@PaulHigin), just curious, did you find time to have a look at this yet?
I'm currently working on a PR that adds a
-Runspaceparameter to the PSBreakpoint cmdlets. It may be appropriate to fix this issue as part of that PR, which is why I ask.It's not a hard requirement though, since this issue can be addressed separately as well, so no real rush.
PaulHigin commentedon Sep 9, 2019
Sorry for the late reply. It looks like this is just a coding mistake. The parent debugger is handling 'BreakpointUpdated' events from a nested debugger and updating its breakpoints. I can't think why this would ever be needed or correct, and my guess is that it was added for 'completeness'.
I think this breakpoint update handler can be completely removed.
PowerShell/src/System.Management.Automation/engine/debugger/debugger.cs
Line 3311 in 139cd94
I am fine doing this in one of your debugging PRs. Thanks!
KirkMunro commentedon Sep 9, 2019
That's the exact same outcome that I came up with as well. Thank you Paul Higinbotham (@PaulHigin) for confirming you feel the same. I'll submit a PR for that shortly to get this corrected.
fix PowerShell#10167
Remove the event handler that was causing breakpoint changes to be er…
Remove the event handler that was causing breakpoint changes to be er…
Remove the event handler that was causing breakpoint changes to be er…