← All Scenarios
· Scenario 06
Concurrent Stop and Invoke Race
Goal
Race Stop() against Invoke() from different threads simultaneously. Neither should deadlock or crash. Stop(TimeSpan) must also be thread-safe.
Trying to break
Concurrent Stop + Invoke is the classic deadlock scenario. After the fix, Stop(TimeSpan) should resolve the race cleanly with no ObjectDisposedException escaping.
2 test stepsPress Start to walk through the scenario step by step.
View full script source (concurrent-stop-and-invoke.ps1)
# Uses Start-ThreadJob for thread context
$ps = [PowerShell]::Create()
$ps.AddScript('Start-Sleep -Seconds 30') > $null
$invokeJob = Start-ThreadJob { param($ps)
try { $ps.Invoke() > $null }
catch [PipelineStoppedException] { } }
-ArgumentList $ps
$stopJob = Start-ThreadJob { param($ps)
Start-Sleep -Milliseconds 500
$ps.Stop([TimeSpan]::FromSeconds(10)) }
-ArgumentList $ps
$invokeJob | Wait-Job -Timeout 15
$stopJob | Wait-Job -Timeout 15