Serialize CommandInfo lookups onto a single dedicated runspace - #2
Merged
jessehouwing merged 3 commits intoAug 19, 2026
Merged
Conversation
Co-authored-by: jessehouwing <4173387+jessehouwing@users.noreply.github.com>
Co-authored-by: jessehouwing <4173387+jessehouwing@users.noreply.github.com>
Co-authored-by: jessehouwing <4173387+jessehouwing@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
jessehouwing
August 19, 2026 16:02
View session
jessehouwing
marked this pull request as ready for review
August 19, 2026 16:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to PowerShell#2206 / issue PowerShell#2205.
CommandInfoCacheresolved commands through aRunspacePool(1, 10), allowing up to tenGet-Commandinvocations to run concurrently across separate runspaces. The PowerShell engine's command discovery state is not thread safe (PowerShell#4003), which surfaced as intermittentCommandNotFoundExceptionforGet-Commanditself and non-deterministic diagnostic counts.A runspace pool doesn't help here: it already guarantees a given runspace is used by only one
PowerShellinstance at a time, and that per-runspace serialization is precisely the configuration that fails. The unsafe state is engine-global, so the gate has to be global too — at which point additional pooled runspaces are never concurrently active.Changes
Engine/CommandInfoCache.csRunspace.SemaphoreSlimso a future re-entrant lookup can't self-deadlock.Disposeacquires the same lock; a lookup that acquires it after disposal returnsnullrather than touching a disposed runspace.CommandNotFoundExceptionfallback from Fix intermittent "The term 'Get-Command' is not recognized" failures during recursive analysis PowerShell/PSScriptAnalyzer#2206 as a safety net for hosts that drive the engine from other threads.Tests/Engine/CommandInfoCacheConcurrency.tests.ps1(new)Helper.Instancebefore the cmdlet does installs a helper with no command invocation context, which breaks every later analysis in the process. Pre-existing engine fragility, called out here so the test doesn't trip it.Performance
Invoke-ScriptAnalyzer -Path Tests -Recurse, warm, alternating runs:Steady-state cost is within noise. The pre-change build also produced large outliers and varying diagnostic counts, so serializing appears to trade little or no throughput for determinism.