Skip to content

Commit b09eb33

Browse files
authored
Lock calls around Test-ModuleManifest, which is not thread safe (#1258)
1 parent f99e1f4 commit b09eb33

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

Engine/Helper.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Helper
3131

3232
private readonly Lazy<CommandInfoCache> _commandInfoCacheLazy;
3333
private readonly RunspacePool _runSpacePool;
34+
private readonly object _testModuleManifestLock = new object();
3435

3536
#endregion
3637

@@ -303,35 +304,39 @@ public PSModuleInfo GetModuleManifest(string filePath, out IEnumerable<ErrorReco
303304
errorRecord = null;
304305
PSModuleInfo psModuleInfo = null;
305306
Collection<PSObject> psObj = null;
306-
using (var ps = System.Management.Automation.PowerShell.Create())
307+
// Test-ModuleManifest is not thread safe
308+
lock (_testModuleManifestLock)
307309
{
308-
ps.RunspacePool = _runSpacePool;
309-
ps.AddCommand("Test-ModuleManifest")
310-
.AddParameter("Path", filePath)
311-
.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
312-
try
310+
using (var ps = System.Management.Automation.PowerShell.Create())
313311
{
314-
psObj = ps.Invoke();
315-
}
316-
catch (CmdletInvocationException e)
317-
{
318-
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
319-
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
320-
// we consume it to allow execution of the of this method.
321-
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
312+
ps.RunspacePool = _runSpacePool;
313+
ps.AddCommand("Test-ModuleManifest")
314+
.AddParameter("Path", filePath)
315+
.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
316+
try
322317
{
323-
throw;
318+
psObj = ps.Invoke();
319+
}
320+
catch (CmdletInvocationException e)
321+
{
322+
// Invoking Test-ModuleManifest on a module manifest that doesn't have all the valid keys
323+
// throws a NullReferenceException. This is probably a bug in Test-ModuleManifest and hence
324+
// we consume it to allow execution of the of this method.
325+
if (e.InnerException == null || e.InnerException.GetType() != typeof(System.NullReferenceException))
326+
{
327+
throw;
328+
}
329+
}
330+
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
331+
{
332+
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
333+
ps.Streams.Error.CopyTo(errorRecordArr, 0);
334+
errorRecord = errorRecordArr;
335+
}
336+
if (psObj != null && psObj.Any() && psObj[0] != null)
337+
{
338+
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
324339
}
325-
}
326-
if (ps.HadErrors && ps.Streams != null && ps.Streams.Error != null)
327-
{
328-
var errorRecordArr = new ErrorRecord[ps.Streams.Error.Count];
329-
ps.Streams.Error.CopyTo(errorRecordArr, 0);
330-
errorRecord = errorRecordArr;
331-
}
332-
if (psObj != null && psObj.Any() && psObj[0] != null)
333-
{
334-
psModuleInfo = psObj[0].ImmediateBaseObject as PSModuleInfo;
335340
}
336341
}
337342
return psModuleInfo;

0 commit comments

Comments
 (0)