Skip to content

Retry Test-Module manifest calls to avoid sporadic failures due to concurrency/thread safety issue in PowerShell itself #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,30 @@ public bool IsDscResourceModule(string filePath)
return false;
}


/// <summary>
/// Gets the module manifest with retries to compensate for sporadic faiures to thread safety bugs in PowerShell.
/// </summary>
/// <param name="filePath"></param>
/// <param name="errorRecord"></param>
/// <returns>Returns a object of type PSModuleInfo</returns>
public PSModuleInfo GetModuleManifestWithRetry(string filePath, out IEnumerable<ErrorRecord> errorRecord)
{
CmdletInvocationException cmdletInvocationException = null;
for (int attempt = 0; attempt < 3; attempt++)
{
try
{
return GetModuleManifest(filePath, out errorRecord);
}
catch (CmdletInvocationException exception)
{
cmdletInvocationException = exception;
}
}
throw cmdletInvocationException;
}

/// <summary>
/// Gets the module manifest
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Rules/MissingModuleManifestField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
if (Helper.IsModuleManifest(fileName))
{
IEnumerable<ErrorRecord> errorRecords;
var psModuleInfo = Helper.Instance.GetModuleManifest(fileName, out errorRecords);
var psModuleInfo = Helper.Instance.GetModuleManifestWithRetry(fileName, out errorRecords);
if (errorRecords != null)
{
foreach (var errorRecord in errorRecords)
Expand Down
2 changes: 1 addition & 1 deletion Rules/UseToExportFieldsInManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)

// check if valid module manifest
IEnumerable<ErrorRecord> errorRecord = null;
PSModuleInfo psModuleInfo = Helper.Instance.GetModuleManifest(fileName, out errorRecord);
PSModuleInfo psModuleInfo = Helper.Instance.GetModuleManifestWithRetry(fileName, out errorRecord);
if ((errorRecord != null && errorRecord.Count() > 0) || psModuleInfo == null)
{
yield break;
Expand Down