Skip to content

Updating breakpoints in a nested debugger or in a job debugger results in breakpoints with the same id in the parent being overwritten #10167

Closed

Description

@KirkMunro

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 $job

At 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
d

Now you're back in your main runspace, outside of the job. Have a look at your breakpoints by invoking the following command:

Get-PSBreakpoint

This 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

Activity

  1. added
    Issue-Question
    on Jul 16, 2019
  2. KirkMunro commented on Aug 21, 2019

    @KirkMunro
    ContributorAuthor

    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, and NestedRunspaceDebugger. 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 debugger HandleBreakpointUpdated event 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.

  3. PaulHigin commented on Aug 23, 2019

    @PaulHigin
    Contributor

    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.

  4. KirkMunro commented on Sep 6, 2019

    @KirkMunro
    ContributorAuthor

    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 -Runspace parameter 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.

  5. PaulHigin commented on Sep 9, 2019

    @PaulHigin
    Contributor

    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.

    private void HandleBreakpointUpdated(object sender, BreakpointUpdatedEventArgs e)

    I am fine doing this in one of your debugging PRs. Thanks!

  6. KirkMunro commented on Sep 9, 2019

    @KirkMunro
    ContributorAuthor

    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.

  7. added a commit that references this issue on Sep 9, 2019
    1929862
  8. ghost closed this as completedin #10503on Sep 10, 2019
  9. ghost added a commit that references this issue on Sep 10, 2019
    096a78f
  10. added a commit that references this issue on Sep 14, 2019
    f3a44f7
  11. added a commit that references this issue on Aug 6, 2026
    6c69c82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-QuestionWG-Interactive-Debugging

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions