Skip to content

WIP: Add examples for Start-DebugAttachSession #5246

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jborean93
Copy link

PR Summary

Adds examples to demonstrate how the new Start-DebugAttachSession function added in PowerShell Editor Services can be used.

The new function is attached by the PR PowerShell/PowerShellEditorServices#2249.

PR Checklist

Note: Tick the boxes below that apply to this pull request by putting an x between the square brackets.
Please mark anything not applicable to this PR NA.

  • PR has a meaningful title
  • Summarized changes
  • [NA] PR has tests
  • This PR is ready to merge and is not work in progress
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready

Adds examples to demonstrate how the new `Start-DebugAttachSession`
function added in PowerShell Editor Services can be used.
@Copilot Copilot AI review requested due to automatic review settings July 25, 2025 05:21
@jborean93 jborean93 requested a review from a team as a code owner July 25, 2025 05:21
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds example scripts demonstrating the usage of the new Start-DebugAttachSession function from PowerShell Editor Services. The examples show how to attach a debugger to a child PowerShell process using custom named pipes.

  • Provides a target script that can be debugged when launched as a child process
  • Demonstrates the complete workflow for attaching to and debugging external PowerShell processes
  • Includes handling for both Windows PowerShell 5.1 and PowerShell Core compatibility

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
examples/ChildDebugSessionTarget.ps1 Target script that runs in a child process and waits for debugger attachment
examples/ChildDebugSessionAttach.ps1 Main example script showing how to launch and attach to a child PowerShell process

Comment on lines +11 to +12
$procParams = @{
FilePath = 'pwsh'
Copy link
Preview

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding 'pwsh' assumes PowerShell Core is available. Consider using $PSVersionTable.PSEdition to detect the current PowerShell edition and use 'powershell' for Windows PowerShell or 'pwsh' for PowerShell Core, or provide a parameter to specify the executable.

Suggested change
$procParams = @{
FilePath = 'pwsh'
$psExecutable = if ($PSVersionTable.PSEdition -eq 'Core') { 'pwsh' } else { 'powershell' }
$procParams = @{
FilePath = $psExecutable

Copilot uses AI. Check for mistakes.

Comment on lines +13 to +17
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
Copy link
Preview

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The polling loop calls Get-Runspace repeatedly without any delay optimization. Consider adding a small delay before each check or implementing exponential backoff to reduce unnecessary CPU usage during the wait period.

Suggested change
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
$currentDelay = 1 # Initial delay duration in seconds
$maxDelay = 16 # Maximum delay duration in seconds
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds $currentDelay
$currentDelay = [math]::Min($currentDelay * 2, $maxDelay) # Exponential backoff

Copilot uses AI. Check for mistakes.

Comment on lines +12 to +17
$runspaces = Get-Runspace
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
Copy link
Preview

Copilot AI Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The infinite loop lacks a timeout mechanism. Consider adding a maximum wait time to prevent the script from hanging indefinitely if the debugger never attaches.

Suggested change
$runspaces = Get-Runspace
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
$runspaces = Get-Runspace
$maxWaitTime = 60 # Maximum wait time in seconds
$elapsedTime = 0 # Initialize elapsed time
while ($true) {
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
break
}
Start-Sleep -Seconds 1
$elapsedTime += 1
if ($elapsedTime -ge $maxWaitTime) {
Write-Host "Timeout reached while waiting for debugger to attach."
break
}

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant