-
Notifications
You must be signed in to change notification settings - Fork 395
PSUseToExportFieldsInManifest throw System.InvalidOperationException: Collection was modified; enumeration operation may not execute. #902
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
Comments
Similar to your other issue, I cannot repro. Let's take this one aside and try to boil down the issue of the other one. |
I was able to reproduce, but the error does not always occur. |
@kalgiz Line of code executed immediately after the first (1..30 | ...) : |
I have tried again but it still does not happen on my machine (neither with the released version or the development branch). This sounds like a race condition where a list is modified in a non-thread safe manner. Some background: When PSSA analyses one file, it spawns a Thread for each rule. |
With Windows Seven sp1 Fr ( Microsoft Windows [version 6.1.7601] ) $Error[0]|select *
writeErrorStream : True
Exception : System.Management.Automation.CmdletInvocationException: La collection a été modifiée;
l'opération d'énumération peut ne pas s'exécuter. --->
System.InvalidOperationException: La collection a été modifiée; l'opération
d'énumération peut ne pas s'exécuter.
à System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
à System.Collections.Generic.List`1.Enumerator.MoveNextRare()
à
Microsoft.PowerShell.Commands.ModuleCmdletBase.UpdateCommandCollection(Collection`1
list, List`1 patterns)
à Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String
moduleManifestPath, ExternalScriptInfo scriptInfo, Hashtable data, Hashtable
localizedData, ManifestProcessingFlags manifestProcessingFlags, Version
minimumVersion, Version maximumVersion, Version requiredVersion, Nullable`1
requiredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)
à Microsoft.PowerShell.Commands.TestModuleManifestCommand.ProcessRecord()
à System.Management.Automation.CommandProcessor.ProcessRecord()
--- Fin de la trace de la pile d'exception interne ---
à System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
à
System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs,
Boolean performSyncInvoke)
à System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Run
space rsToUse, Boolean isSync)
à System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCol
lection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
à
System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1
input, PSDataCollection`1 output, PSInvocationSettings settings)
à System.Management.Automation.PowerShell.Invoke(IEnumerable input,
PSInvocationSettings settings)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetModuleManifest(String
filePath, IEnumerable`1& errorRecord)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseToExportFieldsInManife
st.<AnalyzeScript>d__3.MoveNext()
à System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
à System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass78_0.<
AnalyzeSyntaxTree>b__1()
TargetObject : C:\Program
Files\WindowsPowerShell\Modules\PSScriptAnalyzer\1.16.1\PSScriptAnalyzer.psd1
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCom
mand
InvocationInfo : System.Management.Automation.InvocationInfo
ErrorCategory_Category : 7
ErrorCategory_Activity : Invoke-ScriptAnalyzer
ErrorCategory_Reason : CmdletInvocationException
ErrorCategory_TargetName : C:\Program
Files\WindowsPowerShell\Modules\PSScriptAnalyzer\1.16.1\PSScriptAnalyzer.psd1
ErrorCategory_TargetType : String
ErrorCategory_Message : InvalidOperation: (C:\Program File...ptAnalyzer.psd1:String) [Invoke-ScriptAnalyzer],
CmdletInvocationException
SerializeExtendedInfo : False
ErrorDetails_ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
at <ScriptBlock>, <No file>: line 1
PSMessageDetails :
CategoryInfo : InvalidOperation: (C:\Program File...ptAnalyzer.psd1:String) [Invoke-ScriptAnalyzer],
CmdletInvocationException
ErrorDetails :
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}
Export-Clixml -InputObject $Error -Depth 5 -Path c:\temp\Issue902.txt |
By looking at the stack trace, it seems to come from here but the help on this method says that it is executed synchronously. Therefore this does not make sense to me assuming there is no bug in the |
May be a side effect around Dispose() ? With my tests, the problem is triggered more often with this form :
In use it is random. |
Update: I can finally reproduce now with a |
Yeah, my team has been experiencing this frequently but sporadically on our (Windows 10) laptops -- but never on our beefier (Server 2016) build boxes. Is this purely a matter of filing a bug against PowerShell's Invoke or can we work around it by using a concurrent collection, or an optional switch to do analysis in a slower, non-parallel way? |
@Jaykul There are various multi-threading bugs that I have observed/analyzed in the last months and my current understanding is that there are 2 root causes:
I observed that some of those bugs are less likely to occur on machines with more than 2 cores and if they are fast. |
I've been seeing this for awhile on a variety of psd1's. Happens transiently. Only happens when running psscriptanalyzer against psd1's. I've seen it on Win10 laptops and Server 2012 R2 servers. I haven't noticed a difference in frequency between the laptops or servers, but I will pay more attention to it after reading the comment above. All machines involved have >=4 logical cores. 'Frequently' in my case means probably between 1/10th and 1/20th of the time, roughly estimated. |
The bug here involves ALIASES as far as I can tell. At this point, I can reproduce this easily on my laptop with an empty psm1 and a three line .psd1: @{
ModuleVersion = "1.0.0"
RootModule = "broken.psm1"
AliasesToExport = @("anything")
} NOTE:
You can actually set This reproduces too: New-Item Aliaser -Type Directory
New-ModuleManifest Aliaser\Aliaser.psd1 -AliasesToExport anything -RootModule aliaser.psm1 -FunctionsToExport @() -CmdletsToExport @() -VariablesToExport @()
Set-Content Aliaser\Aliaser.psm1 "" -Encoding UTF8
1..100 | %{ Invoke-ScriptAnalyzer .\Aliaser\Aliaser.psd1 } It's clearly a problem specific to aliases and this rule. |
I have seen this on a number of my modules as well. Most recently with psake. @Jaykul It doesn't seem to always involve aliases. I rarely define those in the manifest and even setting it to In the psake repo, I can consistently hit this issue <90% of the time with this: $sut = (Get-Item -Path .\src).FullName
(1..10) | % {$_; Invoke-ScriptAnalyzer -Path $sut} |
I have this issue 100% of the time running locally but not in CI.
|
I had a look at this again, thanks for your examples, especially the one by @Jaykul proved to be simple and error out every 5th time. Looking at the error stack traces, it seems this is a concurrency bug of |
This also happens in PowerShell 7 btw and unfortunately once I the attach the debugger to PowerShell itself the bug does not happen any (because execution must be slowed down), therefore this is definitely a race condition. |
I opened 2 PRs: |
Since this got fixed in PSSA 1.18.1, I assume all your cases have been fixed but was wondering if any of you have seen the error ever again? |
I no longer encounter this error. |
Steps to reproduce
Expected behavior
No error.
Actual behavior
Environment data
See also the issue #900
The text was updated successfully, but these errors were encountered: