Skip to content

Commit

Permalink
Lock calls around Test-ModuleManifest, which is not thread safe (Powe…
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmeister committed Jun 12, 2019
1 parent f99e1f4 commit b09eb33
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Helper

private readonly Lazy<CommandInfoCache> _commandInfoCacheLazy;
private readonly RunspacePool _runSpacePool;
private readonly object _testModuleManifestLock = new object();

#endregion

Expand Down Expand Up @@ -303,35 +304,39 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
errorRecord = null;
PSModuleInfo psModuleInfo = null;
Collection<PSObject> psObj = null;
using (var ps = System.Management.Automation.PowerShell.Create())
// Test-ModuleManifest is not thread safe
lock (_testModuleManifestLock)
{
ps.RunspacePool = _runSpacePool;
ps.AddCommand("Test-ModuleManifest")
.AddParameter("Path", filePath)
.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
try
using (var ps = System.Management.Automation.PowerShell.Create())
{
psObj = ps.Invoke();
}
catch (CmdletInvocationException e)
{
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
// we consume it to allow execution of the of this method.
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
ps.RunspacePool = _runSpacePool;
ps.AddCommand("Test-ModuleManifest")
.AddParameter("Path", filePath)
.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
try
{
throw;
psObj = ps.Invoke();
}
catch (CmdletInvocationException e)
{
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
// we consume it to allow execution of the of this method.
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
{
throw;
}
}
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
{
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
ps.Streams.Error.CopyTo(errorRecordArr, 0);
errorRecord = errorRecordArr;
}
if (psObj != null && psObj.Any() && psObj[0] != null)
{
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
}
}
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
{
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
ps.Streams.Error.CopyTo(errorRecordArr, 0);
errorRecord = errorRecordArr;
}
if (psObj != null && psObj.Any() && psObj[0] != null)
{
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
}
}
return psModuleInfo;
Expand Down

0 comments on commit b09eb33

Please sign in to comment.