Fix flaky Debug-Runspace attach event test - #27883
Open
Travis Plunk (TravisEz13) wants to merge 1 commit into
Open
Fix flaky Debug-Runspace attach event test#27883Travis Plunk (TravisEz13) wants to merge 1 commit into
Travis Plunk (TravisEz13) wants to merge 1 commit into
Conversation
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
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the Debug-Runspace Pester test that intermittently timed out waiting for the OnDebugAttach engine event by eliminating a runspace startup race and adding deterministic readiness/cleanup logic.
Changes:
- Creates and opens the target runspace explicitly before starting
Wait-Event, ensuring the runspace isOpenedand has an initialized event manager. - Gates the debugger attach on actual pipeline/runspace readiness using
Wait-UntilTrue(pipeline stateRunning+ runspace availabilityBusy) rather than relying on timing. - Adds deterministic detach waiting and
try/finallycleanup to stop/dispose PowerShell instances and the created runspace even on assertion failures.
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Fixes the flaky
Debug-RunspacetestShould write attach event and mark runspace as having a remote debugger attached, which intermittently fails in CI at:Root cause (test-side race, not a product bug)
The test did:
BeginInvokeonly queues work. Nothing guaranteed the target runspace had finished opening beforeDebug-Runspaceexecuted. WhenDebug-Runspacewon the race, either:DebugRunspaceCommand.EndProcessingoperated on a runspace that was notOpened, producing a terminatingInvalidOperationerror before ever reaching the event generation, or_runspace.Events?.GenerateEvent(...)(DebugRunspaceCommand.cs:269) was skipped by the null-conditional because the runspace's execution context / event manager was not yet available.Either way
OnDebugAttachwas never delivered and the 5-second wait timed out.Note the fix must gate on runspace readiness, not merely on subscriber registration: an event generated before
Wait-EventreachesProcessRecordis not lost, becauseWaitEventCommand.ScanEventQueuedrainsEvents.ReceivedEventson entry.Fix
Test-only changes — no production code modified:
Open()the target runspace and assign it to thePowerShellinstance, eliminating the "runspace not yet open" window and guaranteeing a live event manager.Debug-Runspace, gate on real readiness with the existingWait-UntilTruehelper: the pipeline'sInvocationStateInfo.StateisRunningand the runspace'sRunspaceAvailabilityisBusy. This is a bounded readiness gate, not a fixed sleep or an inflated timeout.IsRemoteDebuggerAttachedto return to$false, since the cmdlet resets it while unwinding asynchronously with respect toStop().try/finallyso the debuggerPowerShell, targetPowerShell, and the runspace are stopped and disposed even when an assertion fails.All four original assertions are preserved:
OnDebugAttachreceived,IsRemoteDebuggerAttachedbecomes$true, stopping throwsPipelineStoppedException, andIsRemoteDebuggerAttachedreturns to$false.PR Context
Removes a source of intermittent Windows CI failures.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerTest Results
Built locally (
Start-PSBuild, Windows, Debug) and run against the freshpwsh:Repeated 10 consecutive standalone runs:
P=5 F=0every time.Via the repo harness, including the closely related debugger suite: