-
Notifications
You must be signed in to change notification settings - Fork 12
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
Race condition exception when accessing $Host.UI.psobject.Properties inside a runspace #31
Comments
I cannot repro this in 5.1 or 7.3.6 when running in or out of WT, either when using Microsoft.PowerShell.ThreadJob v2.1.0 or ThreadJob 2.0.3 on |
Where can I get the 2.1.0 version, so I could test it on Windows 10 and PowerShell 5.1 and 7.3.6? On PowerShell Gallery there is only 2.0.3. The same on GitHub repo. |
|
Thanks, sorry, I've missed the info about the new module name. Will test it tomorrow. I've just finished a 9 hour car drive for the holidays so I wasn't thinking clearly. |
@kilasuit Did test it with To be sure, I have tested it on 3 different computers:
Also always try it on the new PowerShell session, do not try it in the current if there was no repro. If still no success try this example: $cpuCount = [Environment]::ProcessorCount
$splatStartThreadJob = @{
ThrottleLimit = $cpuCount
StreamingHost = $Host
ErrorAction = 'Stop'
InitializationScript = {Import-Module Pester -Force -ErrorAction Stop}
}
$threadJobs = 1..($cpuCount * 2) | ForEach-Object {
Microsoft.PowerShell.ThreadJob\Start-ThreadJob -Name $_ -ScriptBlock {
$pc = New-PesterConfiguration
$pc.Run.Container = New-PesterContainer -ScriptBlock {Describe 'd' { It 'i' { 1 | Should -Be 1 }}}
$pc.Output.Verbosity = 'None'
$pc.Run.PassThru = $true
$pc.Run.SkipRemainingOnFailure ='Block'
[PSCustomObject]@{
Test = $Using:_
Result = Invoke-Pester -Configuration $pc
}
} @splatStartThreadJob
}
Wait-Job -Job $threadJobs | Receive-Job |
Adding this snippet from the Discord discussion yesterday from @SeeminglyScience as another way to repro this $event = [System.Threading.ManualResetEvent]::new($false)
$jobs = 1..500 | % {
Start-ThreadJob {
$event = $using:event
$null = $event.WaitOne()
$supportsVT = $Host.UI.psobject.Properties['SupportsVirtualTerminal'].Value
} -StreamingHost $Host
}
Start-Sleep -Milliseconds 500
$null = $event.Set()
$jobs | Receive-Job -AutoRemoveJob -Wait |
Based on the callstack, it appears that ListDictionary is being used somewhere which is not threadsafe. In this case, this doesn't appear to be an issue with ThreadJob itself, but something the thread is accessing is not threadsafe and results in the race condition. If the actual use case is to see within a thread if |
The issue was detected first when the Pester was executed with ThreadJob in order to speed up infrastructure tests. Pester is accessing the I got lots of help from @SeeminglyScience, when debugging this issue. Maybe you could contact him for some more in-depth analysis. |
I would agree that I don't think it's a ThreadJob problem. My guess is that it's a race condition in how PowerShell is creating the internal At a glance it looks like there's a lock around the creation of the underlying collection, but the collection itself is populated lazily with no lock. I'd also second the suggestion of working around it by accessing the object before hand in the primary thread if possible. I'm hesitant to suggest more locks there as it may have a noticeable impact on performance for an issue that doesn't get hit often. |
Prerequisites
Steps to reproduce (thanks to @SeeminglyScience)
Environment:
Sporadic race condition exception when accessing
$Host.UI.psobject.Properties
inside a runspace started withStart-ThreadJob
:Start new session:
pwsh -nop
Paste following:
Steps to reproduce with Pester 5.5.0:
Environment:
Sporadic race condition exception when accessing
$Host.UI.psobject.Properties
inside a runspace started withStart-ThreadJob
:Start new session:
pwsh -nop
Paste following:
More details here.
Expected behavior
There should be no exception.
Actual behavior
Error details
Environment data
Version
ThreadJob 2.0.3 and Microsoft.PowerShell.ThreadJob 2.1.0
Visuals
The text was updated successfully, but these errors were encountered: