Skip to content

Race condition exception when accessing $Host.UI.psobject.Properties inside a runspace #31

Description

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest version
  • Search the existing issues.

Steps to reproduce (thanks to Patrick Meinecke (@SeeminglyScience))

Environment:

  1. PowerShell 7.3.6 or higher on up-to-date Windows 10 22H2 x64
  2. ThreadJob 2.0.3 and Microsoft.PowerShell.ThreadJob 2.1.0

Sporadic race condition exception when accessing $Host.UI.psobject.Properties inside a runspace started with Start-ThreadJob:

  1. Start new session:
    pwsh -nop

  2. Paste following:

$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
  1. If you couldn't repro first time, start new PowerShell session, do not try in the same session.

Steps to reproduce with Pester 5.5.0:

Environment:

  1. PowerShell 7.3.6 or higher on up-to-date Windows 10 22H2 x64
  2. Pester 5.5.0
  3. ThreadJob 2.0.3 and Microsoft.PowerShell.ThreadJob 2.1.0

Sporadic race condition exception when accessing $Host.UI.psobject.Properties inside a runspace started with Start-ThreadJob:

  1. Start new session:
    pwsh -nop

  2. Paste following:

1..500 | ForEach-Object {
    Start-ThreadJob {
        1 | Should -BeExactly 1
        $supportsVT = $Host.UI.psobject.Properties['SupportsVirtualTerminal'].Value
    } -StreamingHost $Host
} | Wait-Job | Receive-Job | Remove-Job -Force
  1. If you couldn't repro first time, start new PowerShell session, do not try in the same session.

More details here.

Expected behavior

There should be no exception.

Actual behavior

InvalidOperation: Cannot index into a null array.
InvalidOperation: Cannot index into a null array.
InvalidOperation: Cannot index into a null array.
InvalidOperation: Cannot index into a null array.

Error details

PS C:\> Get-Error

Exception             :
    Type        : System.Management.Automation.RuntimeException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot index into a null array.
            HResult : -2146233087
        CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : NullArray
        InvocationInfo        :
            ScriptLineNumber : 1
            OffsetInLine     : 26
            HistoryId        : -1
            Line             : 1 | Should -BeExactly 1; $supportsVT = $Host.UI.psobject.Properties['SupportsVirtualTerminal'].Value
            PositionMessage  : At line:1 char:26
                               + … eExactly 1; $supportsVT = $Host.UI.psobject.Properties['SupportsVirtu …
                               +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    TargetSite  : System.Object CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Object, System.String)
    Message     : Cannot index into a null array.
    Data        : System.Collections.ListDictionaryInternal
    Source      : Anonymously Hosted DynamicMethods Assembly
    HResult     : -2146233087
    StackTrace  :
   at CallSite.Target(Closure, CallSite, Object, String)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : NullArray
InvocationInfo        :
    ScriptLineNumber : 1
    OffsetInLine     : 26
    HistoryId        : -1
    Line             : 1 | Should -BeExactly 1; $supportsVT = $Host.UI.psobject.Properties['SupportsVirtualTerminal'].Value
    PositionMessage  : At line:1 char:26
                       + … eExactly 1; $supportsVT = $Host.UI.psobject.Properties['SupportsVirtu …
                       +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version

ThreadJob 2.0.3 and Microsoft.PowerShell.ThreadJob 2.1.0

Visuals

race

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions