Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Thread safe class method defined in Powershell hangs inside a Lazy inside a ConcurrentDictionary #18248

Copy link
Copy link
@User1785604260

Description

@User1785604260
Issue body actions

Prerequisites

Steps to reproduce

I have a thread safe class method (it's static and empty) that is hanging when called inside of a System.Lazy initializer inside of a System.Collections.Concurrent.ConcurrentDictionary when used with ForEach-Object -Parallel. Calling a normal dotnet method works fine (e.g. [System.Convert]::ToString(132456, 2)), but my [Dead]::Lock() method call hangs.

This is as far as I was able to strip my repro code down while still causing the hang:

#Requires -Version 7

Write-Host "Starting job`n"

$dict = [System.Collections.Concurrent.ConcurrentDictionary[string, System.Lazy[string]]]::new()

$job = 1..16 | ForEach-Object -AsJob -ThrottleLimit 16 -Parallel {
    $threadNum = $_
    $dict = $using:dict

    class Dead {
        # This method should be thread safe(?)
        static [void] Lock() {
        }
    }

    [System.Console]::WriteLine("Starting thread #$threadNum")

    $lazy = $dict.GetOrAdd("same key", [System.Lazy[string]]::new(
        {
            # Calling this method causes the script to hang, even though it should(?) be thread safe
            [Dead]::Lock()

            $okToCall = [System.Convert]::ToString(132456, 2)
            return ""
        },
        [System.Threading.LazyThreadSafetyMode]::ExecutionAndPublication))

    [void]$lazy.Value

    [System.Console]::WriteLine("Completed thread #$threadNum")
}
try {
    while (!$job.Finished.WaitOne(1000)) {
        Write-Host "$(Get-Date): Waiting"
    }
    $job | Receive-Job

    Write-Host "`nFinished job`n"
} finally {
    $job | Stop-Job
}

Note: you may need to run this a bunch of times to actually see the hang. I'm also running this without my profile to ensure that's not interfering: pwsh.exe -NoProfile -File $Home\OneDrive\Scripts\bug.ps1

I've had independent verification of the hang from at least one other user.

Expected behavior

PS C:\> pwsh.exe -NoProfile -File $Home\OneDrive\Scripts\bug.ps1
Starting job

Starting thread #1
Starting thread #2
Starting thread #3
Starting thread #4
Starting thread #5
Starting thread #6
Starting thread #7
Starting thread #8
Starting thread #9
Starting thread #10
Starting thread #11
Starting thread #12
Starting thread #13
Starting thread #14
Starting thread #15
Starting thread #16
Completed thread #2
Completed thread #6
Completed thread #9
Completed thread #13
Completed thread #3
Completed thread #1
Completed thread #4
Completed thread #5
Completed thread #7
Completed thread #8
Completed thread #10
Completed thread #11
Completed thread #12
Completed thread #14
Completed thread #15
Completed thread #16

Finished job

PS C:\>

Actual behavior

PS C:\> pwsh.exe -NoProfile -File $Home\OneDrive\Scripts\bug.ps1
Starting job

Starting thread #2
Starting thread #1
Starting thread #3
Starting thread #4
Starting thread #5
Starting thread #6
Starting thread #7
Starting thread #8
Starting thread #9
Starting thread #10
Starting thread #11
Starting thread #12
Starting thread #13
Starting thread #14
Starting thread #15
Starting thread #16
10/10/2022 11:52:52: Waiting
10/10/2022 11:52:53: Waiting
10/10/2022 11:52:54: Waiting
10/10/2022 11:52:55: Waiting
10/10/2022 11:52:56: Waiting
10/10/2022 11:52:57: Waiting
10/10/2022 11:52:58: Waiting
10/10/2022 11:52:59: Waiting
10/10/2022 11:53:00: Waiting
10/10/2022 11:53:01: Waiting
10/10/2022 11:53:02: Waiting

Error details

No response

Environment data

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

Visuals

No response

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-DuplicateThe issue is a duplicate.The issue is a duplicate.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.