Skip to content

Fix race condition in Add-Type - #19471

Merged
Dongbo Wang (daxian-dbw) merged 2 commits into
PowerShell:masterfrom
daxian-dbw:addtype
Apr 13, 2023
Merged

Dongbo Wang (daxian-dbw) merged 2 commits into
PowerShell:masterfrom
daxian-dbw:addtype

Conversation

@daxian-dbw

Copy link
Copy Markdown
Member

PR Summary

Fix #19470

Add-Type uses static HashSet and Dictionary for caching type names and assemblies, which are not thread-safe. When running Add-Type with the same C# code from multiple Runspaces simultaneously, these caches may be accessed at the same time, and that will cause a race condition.

The fix is to use ConcurrentDictionary for both caches.

PR Checklist

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 8ed8b52 into PowerShell:master Apr 13, 2023
@daxian-dbw
Dongbo Wang (daxian-dbw) deleted the addtype branch April 13, 2023 04:41
@ghost

Copy link
Copy Markdown

🎉v7.4.0-preview.3 has been released which incorporates this pull request.:tada:

Handy links:

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

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add-Type fails sporadically with "An item with the same key has already been added"

3 participants