From 52f08b44b9d55beb16653ea7d7298eede91eb2da Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Fri, 7 Jun 2019 22:55:57 +0100 Subject: [PATCH 1/2] Retry Test-Module manifest calls to avoid sporadic failures to concurrency/thread safety issue in PowerShell itself --- Engine/Helper.cs | 25 +++++++++++++++++++++++++ Rules/MissingModuleManifestField.cs | 2 +- Rules/UseToExportFieldsInManifest.cs | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Engine/Helper.cs b/Engine/Helper.cs index 5af3a9fcf..bc7ab2156 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -292,6 +292,31 @@ public bool IsDscResourceModule(string filePath) return false; } + + /// + /// Gets the module manifest with retries to compensate for sporadic faiures to thread safety bugs in PowerShell. + /// + /// + /// + /// Returns a object of type PSModuleInfo + public PSModuleInfo GetModuleManifestWithRetry(string filePath, out IEnumerable errorRecord) + { + CmdletInvocationException cmdletInvocationException = null; + for (int attempt = 0; attempt < 3; attempt++) + { + try + { + return GetModuleManifest(filePath, out errorRecord); + } + catch (CmdletInvocationException exception) + { + cmdletInvocationException = exception; + Console.WriteLine($"Catch: {attempt}"); + } + } + throw cmdletInvocationException; + } + /// /// Gets the module manifest /// diff --git a/Rules/MissingModuleManifestField.cs b/Rules/MissingModuleManifestField.cs index 78deb38ab..737ef9033 100644 --- a/Rules/MissingModuleManifestField.cs +++ b/Rules/MissingModuleManifestField.cs @@ -40,7 +40,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) if (Helper.IsModuleManifest(fileName)) { IEnumerable 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) diff --git a/Rules/UseToExportFieldsInManifest.cs b/Rules/UseToExportFieldsInManifest.cs index bfd99db9e..19e4fee32 100644 --- a/Rules/UseToExportFieldsInManifest.cs +++ b/Rules/UseToExportFieldsInManifest.cs @@ -51,7 +51,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) // check if valid module manifest IEnumerable 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; From d73eea218fb8409b865d42bd8a88bdf3bf0d58fa Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Fri, 7 Jun 2019 23:05:33 +0100 Subject: [PATCH 2/2] cleanup --- Engine/Helper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Engine/Helper.cs b/Engine/Helper.cs index bc7ab2156..d0144814b 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -311,7 +311,6 @@ public PSModuleInfo GetModuleManifestWithRetry(string filePath, out IEnumerable< catch (CmdletInvocationException exception) { cmdletInvocationException = exception; - Console.WriteLine($"Catch: {attempt}"); } } throw cmdletInvocationException;