diff --git a/Engine/CommandInfoCache.cs b/Engine/CommandInfoCache.cs index 67e35c211..61e78d86b 100644 --- a/Engine/CommandInfoCache.cs +++ b/Engine/CommandInfoCache.cs @@ -12,20 +12,44 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer /// /// Provides threadsafe caching around CommandInfo lookups with `Get-Command -Name ...`. /// - internal class CommandInfoCache + internal class CommandInfoCache : IDisposable { private readonly ConcurrentDictionary> _commandInfoCache; private readonly Helper _helperInstance; private readonly RunspacePool _runspacePool; + private bool disposed = false; /// /// Create a fresh command info cache instance. /// - public CommandInfoCache(Helper pssaHelperInstance, RunspacePool runspacePool) + public CommandInfoCache(Helper pssaHelperInstance) { _commandInfoCache = new ConcurrentDictionary>(); _helperInstance = pssaHelperInstance; - _runspacePool = runspacePool; + _runspacePool = RunspaceFactory.CreateRunspacePool(1, 10); + _runspacePool.Open(); + } + + /// Dispose the runspace pool + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if ( disposed ) + { + return; + } + + if ( disposing ) + { + _runspacePool.Dispose(); + } + + disposed = true; } /// diff --git a/Engine/Helper.cs b/Engine/Helper.cs index 330547d02..71be95b4b 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -30,7 +30,6 @@ public class Helper private PSVersionTable psVersionTable; private readonly Lazy _commandInfoCacheLazy; - private readonly RunspacePool _runSpacePool; private readonly object _testModuleManifestLock = new object(); #endregion @@ -116,11 +115,7 @@ internal set /// private Helper() { - // There are 5 rules that use the CommandInfo cache but one rule (AvoidAlias) makes parallel queries. - // Therefore 10 runspaces was a heuristic measure where no more speed improvement was seen. - _runSpacePool = RunspaceFactory.CreateRunspacePool(1, 10); - _runSpacePool.Open(); - _commandInfoCacheLazy = new Lazy(() => new CommandInfoCache(pssaHelperInstance: this, runspacePool: _runSpacePool)); + _commandInfoCacheLazy = new Lazy(() => new CommandInfoCache(pssaHelperInstance: this)); } /// @@ -309,7 +304,6 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable