Skip to content

Fix flaky Debug-Runspace attach event test - #27885

Open
Travis Plunk (TravisEz13) wants to merge 3 commits into
masterfrom
fix-debug-runspace-flaky-attach-test
Open

Fix flaky Debug-Runspace attach event test#27885
Travis Plunk (TravisEz13) wants to merge 3 commits into
masterfrom
fix-debug-runspace-flaky-attach-test

Conversation

@TravisEz13

Copy link
Copy Markdown
Member

PR Summary

Make the Debug-Runspace attach-event test deterministic by opening the target runspace explicitly and waiting for the asynchronous Wait-Event pipeline to reach a running, busy state before invoking the debugger. Ensure the debugger, target pipeline, and runspace are cleaned up in all outcomes, while warning if best-effort stop cleanup fails.

PR Context

The test could start Debug-Runspace before the target runspace had opened and registered its event waiter, causing intermittent Windows CI failures when OnDebugAttach was not observed. This is a test-only synchronization fix; production code is unchanged. The original attach, attached-state, stop, and detach-state assertions remain intact.

PR Checklist

Test Results

  • Debug-Runspace.Tests.ps1: 5 passed, 0 failed
  • Ten consecutive standalone runs: 5 passed, 0 failed on every run
  • Related debugger tests via Start-PSPester: 25 passed, 0 failed

The test started a 'Wait-Event' pipeline with BeginInvoke on a PowerShell instance whose runspace was not yet opened, then immediately started Debug-Runspace against that runspace id. Nothing synchronized the two, so Debug-Runspace could run before the target runspace was open/executing, in which case the cmdlet errored out or skipped the null-conditional Events.GenerateEvent call and OnDebugAttach never fired.

Open the target runspace explicitly, gate on the Wait-Event pipeline actually running before attaching, wait deterministically for detach, and dispose all instances in a finally block. Assertions are unchanged and no production code was modified.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2a30274b-8dba-4f6b-92b3-e715b633f8b3
Keep the original five-second assertion timeout and avoid broad silent catches in cleanup while preserving deterministic readiness and teardown.
Preserve best-effort cleanup while surfacing stop failures instead of suppressing them silently.
Copilot AI lite review requested due to automatic review settings August 25, 2026 19:33
@TravisEz13
Travis Plunk (TravisEz13) requested a review from a team as a code owner August 25, 2026 19:33
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the Debug-Runspace Pester test to eliminate a race condition by explicitly opening the target runspace and synchronizing on the Wait-Event pipeline reaching a running/busy state before invoking Debug-Runspace. The change is test-only and aims to prevent intermittent CI failures where the OnDebugAttach event was not observed.

Changes:

  • Create and open a dedicated target runspace up front, and run Wait-Event inside it to ensure the event manager is initialized before attach.
  • Add deterministic synchronization via Wait-UntilTrue to confirm the Wait-Event pipeline is actually running (and the runspace is busy) before attaching the debugger.
  • Add robust cleanup via try/finally to stop and dispose the debugger, target pipeline, and runspace (with warnings only on best-effort cleanup failures).

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@TravisEz13 Travis Plunk (TravisEz13) added the CL-Test Indicates that a PR should be marked as a test change in the Change Log label Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Test Indicates that a PR should be marked as a test change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants