PowerShellEditorServices icon indicating copy to clipboard operation
PowerShellEditorServices copied to clipboard

Write event on configurationDone for attach requests

Open jborean93 opened this issue 6 months ago • 3 comments

Prerequisites

  • [x] I have written a descriptive issue title.
  • [x] I have searched all issues to ensure it has not already been requested.

Summary

On attach scenarios, it is not possible to wait for all the breakpoints set by the client to be set in the remote runspace. A client could have something like

$originalRunspaces = Get-Runspace

Read-Host -Prompt "Please attach to process $pid with runspace $([Runspce]::DefaultRunspace.Id)"

# Waits for VSCode to attach to runspace, an extra runspace will be created by
# Enter-PSHostProcess
while ($true) {
    $newRunspace = Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id } | Select-Object -First 1
    if ($newRunspace) {
        break
    }
    Start-Sleep -Milliseconds 300
}

& .\script.ps1

But this only checks that PSES has attached to the named pipe. It won't wait for PSES to have called Debug-Runspace on the target runspace and more importantly any breakpoints inside script.ps1 to be set. It's also error prone if you cannot control when the attach actually happens as the additional runspace check could have occurred before the first Get-Runspace call.

What would be ideal is for a well known event to be created by PSES when configurationDone was received so that the client only had to do

# Attach to this runspace somehow

$e = Wait-Event -SourceIdentifier PSES.ConfigurationDone -Timeout 30
if ($e) {
    $e | Remove-Event
}
else {
    throw "Timed out waiting for attach event"
}

Proposed Design

My proposal requires some changes to PowerShell itself but I wanted to see if the general idea was acceptable before trying to propose the change there. The benefit of using an event is that no custom functions needed to be loaded, and subsequently checked by the code, and Wait-Event has a simple timeout operation.

Ultimately what needs to happen is for PowerShell to expose a __New-Event command, similar to the new __Set-PSBreakpoint commands, that is treated specially by the PSRemoting server. This would be able to do the following but without having to spin up a new pipeline.

$runspace = Get-Runspace -Id $runspaceId
$runspace.Events.GenerateEvent($SourceId, $Sender, $EventArgs, $EventData)

We cannot just have PSES run this script or call New-Event directly as the runspace pipeline is busy running Debug-Runspace. The only time it can run commands is if the caller ran Wait-Debugger which is not ideal as it will pause the execution and require the client to continue on manually.

An alternative that I haven't tried out yet is to delay Debug-Runspace until after configurationDone is sent. This would allow both the breakpoints and event to be manually emited as commands on the runspace specified without having to block the pipeline with Debug-Runspace. I am not sure if that will be possible though but it could be an option that doesn't require any changes to PowerShell itself.

jborean93 avatar Jul 23 '25 04:07 jborean93

The issue https://github.com/PowerShell/PowerShellEditorServices/issues/2191 would need to be fixed to try and set breakpoints and event information before Debug-Runspace is run. That'll solve the use cases quite nicely for both 5.1 and 7 and wouldn't require any changes to PowerShell itself.

jborean93 avatar Jul 24 '25 01:07 jborean93

We had a huge discussion on this as I recall and the DAP protocol currently doesn't have a way to "sync" breakpoints as per lots of discussion in https://github.com/PowerShell/PowerShellEditorServices/pull/1853. Two options are using the config provider as a proxy for the breakpoint state, or implementing custom messaging. I'm leaning towards the config provider.

Agreed that there's going to be some really tricky blocking and whatnot to handle to deal with all potential attach/launch local and remote use cases across both 5.1 and 7. If necessary we can implement the 7 logic and throw exception on 5.1 mentioning 5.1 is best effort and to move to 7 for that support.

JustinGrote avatar Jul 24 '25 03:07 JustinGrote

If #2191 is fixed then theoretically the breakpoint setting can be done in the runspace entered by Enter-PSHostProcess before it calls Debug-Runspace. It's the latter that causes the blockage right now and breakpoints only get set without a Wait-Debugger on 7+ because it uses a newer API that essentially avoids having to start a pipeline and thus the engine can set it manually.

If #2191 isn't fixed, or we like the idea of only ever emitting the event after the attach happens then this will need a change in PowerShell to support this scenario.

jborean93 avatar Jul 24 '25 04:07 jborean93