Open kborowinski opened 1 year ago
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 Microsoft Windows 10.0.25905 - aka Win 11 23H2 via the Insiders Dev ring
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.
Install-Module Microsoft.PowerShell.ThreadJob
installs the renamed and updated ThreadJob module & ThreadJob module is technically obsolete going forward as when the PowerShell team took ownership of the module it was renamed, although I think this may not have been well communicated at the time
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 Microsoft.PowerShell.ThreadJob
module v2.1.0, still the same issue:
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 SupportsVirtualTerminal
is true/false, then it would be best to capture that outside of the thread job and pass it in.
The issue was detected first when the Pester was executed with ThreadJob in order to speed up infrastructure tests. Pester is accessing the SupportsVirtualTerminal
inside the Pester.psm1. I guess that Pester authors weren't aware that this is not thread safe and it would require change in Pester code.
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 PSMemberInfoCollection<>
and caching it in the member resurrection table.
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:
Start-Sleep -Milliseconds 500 $null = $event.Set()
$jobs | Receive-Job -AutoRemoveJob -Wait
More details here.
Expected behavior
Actual behavior
Error details
Environment data
Version
ThreadJob 2.0.3 and Microsoft.PowerShell.ThreadJob 2.1.0
Visuals