Title: Test-ModuleManifest is not thread safe (exceptions thrown from deep inside PowerShell engine) · Issue #9889 · PowerShell/PowerShell · GitHub
Open Graph Title: Test-ModuleManifest is not thread safe (exceptions thrown from deep inside PowerShell engine) · Issue #9889 · PowerShell/PowerShell
X Title: Test-ModuleManifest is not thread safe (exceptions thrown from deep inside PowerShell engine) · Issue #9889 · PowerShell/PowerShell
Description: PSScriptAnalyzer version of 1.18.0 or older sporadically encounter an exception when calling Test-ModuleManifest concurrently, it seems to be an issue of the PowerShell engine code itself, not the cmdlet The issue applies to any PowerShe...
Open Graph Description: PSScriptAnalyzer version of 1.18.0 or older sporadically encounter an exception when calling Test-ModuleManifest concurrently, it seems to be an issue of the PowerShell engine code itself, not the ...
X Description: PSScriptAnalyzer version of 1.18.0 or older sporadically encounter an exception when calling Test-ModuleManifest concurrently, it seems to be an issue of the PowerShell engine code itself, not the ...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/9889
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Test-ModuleManifest is not thread safe (exceptions thrown from deep inside PowerShell engine)","articleBody":"PSScriptAnalyzer version of 1.18.0 or older sporadically encounter an exception when calling `Test-ModuleManifest` concurrently, it seems to be an issue of the PowerShell engine code itself, not the cmdlet\r\nThe issue applies to any PowerShell version (5, 6.2, 7.0-preview1)\r\n\r\nFor context, here is how PSSA calls `Test-ModuleManifest` in the `Singleton` `Helper` class by creating an instance of `PowerShell` each time (that gets disposed afterwards) but with a re-used runspace pool (previous versions did not re-use the runspace pool and suffered from the same issue as well though).\r\nhttps://github.com/PowerShell/PSScriptAnalyzer/blob/0bef1c8b8ddf5b570dd0b6c90f0d0257b94eb374/Engine/Helper.cs#L306-L315\r\n```csharp\r\nusing (var ps = System.Management.Automation.PowerShell.Create())\r\n{\r\n ps.RunspacePool = _runSpacePool;\r\n ps.AddCommand(\"Test-ModuleManifest\")\r\n .AddParameter(\"Path\", filePath)\r\n .AddParameter(\"WarningAction\", ActionPreference.SilentlyContinue);\r\n try\r\n {\r\n psObj = ps.Invoke();\r\n }\r\n { ... }\r\n}\r\n```\r\nThe above code gets called from from different threads (each rule has its own thread) that are started as follows here:\r\nhttps://github.com/PowerShell/PSScriptAnalyzer/blob/a68b13956ab1136af91066d1ce61113574447945/Engine/ScriptAnalyzer.cs#L2075\r\n```csharp\r\nallowedRules.Select(scriptRule => Task.Factory.StartNew(() => .... );\r\n```\r\n\r\n# Steps to reproduce\r\n\r\nExtract the files in the below zip to a folder (yes, they are a bit artificial but are a good and simple way of making the race condition more reliably on most machines (my machine is a 4 core i7).\r\n[repro.zip](https://github.com/PowerShell/PowerShell/files/3287403/repro.zip)\r\n\r\n```powershell\r\nInstall-module PSScriptAnalyzer -RequiredVersion 1.18.0\r\n1..100 | Invoke-ScriptAnalyzer -Path $PathToReproPsd1\r\n```\r\n\r\n# Expected behavior\r\n\r\n```none\r\nNo leaked errors\r\n```\r\n\r\n# Actual behavior\r\n\r\nPowerShell leaks errors that indicate a thread safety problem.\r\nPR #9860 offered a fix but the review concluded it was not the right fix/place.\r\n\r\nThe stack trace is below and I uploaded a minidump with heap [here](https://www.dropbox.com/s/pgblge4chymc8wc/pwsh.dmp?dl=0) from the latest `master` commit `bdc8548700118cea6c31420c2f7478785e6d84ac` of `PowerShell` for one of the most common exceptions (`CmdletInvocationException` with inner exception from `System.Collections.ListDictionaryInternal`: `'Collection was modified; enumeration operation may not execute.'`)\r\n```\r\n at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input) in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\pipelinebase.cs:line 416\r\n at System.Management.Automation.Runspaces.Pipeline.Invoke() in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\Pipeline.cs:line 531\r\n at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke) in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\PowerShell.cs:line 5619\r\n at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync) in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\PowerShell.cs:line 5466\r\n at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings) in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\PowerShell.cs:line 4588\r\n at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings) in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\PowerShell.cs:line 4780\r\n at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings) in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\PowerShell.cs:line 4530\r\n at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings) in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\PowerShell.cs:line 2425\r\n at System.Management.Automation.PowerShell.Invoke() in C:\\Users\\chris\\PowerShell\\src\\System.Management.Automation\\engine\\hostifaces\\PowerShell.cs:line 2296\r\n at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidUsingDeprecatedManifestFields.AnalyzeScript(Ast ast, String fileName)+MoveNext()\r\n```\r\nI wonder if the call to `return Output.NonBlockingRead(Int32.MaxValue);` in [pipelinebase.cs (line 416) ](https://github.com/PowerShell/PowerShell/blob/d80154430d600a52a1d1e3a9f3ecf032b36185d4/src/System.Management.Automation/engine/hostifaces/pipelinebase.cs#L416) would need to be blocking?\r\n\r\n\r\n# Environment data\r\n\r\nWindows (any version, e.g. 1903), Any version of PowerShell (5.1, 6.2, 7.0-prevew1, latest commit from master)","author":{"url":"https://github.com/bergmeister","@type":"Person","name":"bergmeister"},"datePublished":"2019-06-13T17:48:37Z","url":"https://github.com/PowerShell/PowerShell/issues/9889"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a514fe8a-f1a1-56e8-d261-6dd7a64a1e66 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB7A:1D838C:28CE1:37585:6AFCE8B4 |
| html-safe-nonce | 73a613571b5be12eaf4943b7726a4e9fb5952032bab3ab37f42db8ec7de1e902 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjdBOjFEODM4QzoyOENFMTozNzU4NTo2QUZDRThCNCIsInZpc2l0b3JfaWQiOiI1Njc2MzY3ODU1NTI0NTcxMzE3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1a84d2fdc0ee06110d387ee008341e64460258f37061ddf2e86991364c65f5c6 |
| hovercard-subject-tag | issue:455872725 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/9889/issue_layout |
| twitter:image | https://opengraph.githubassets.com/571155c07b73226aa99f8e91b6356c0d2b547ac4b08034276a6ccfc986e261ae/PowerShell/PowerShell/issues/9889 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/571155c07b73226aa99f8e91b6356c0d2b547ac4b08034276a6ccfc986e261ae/PowerShell/PowerShell/issues/9889 |
| og:image:alt | PSScriptAnalyzer version of 1.18.0 or older sporadically encounter an exception when calling Test-ModuleManifest concurrently, it seems to be an issue of the PowerShell engine code itself, not the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bergmeister |
| hostname | github.com |
| expected-hostname | github.com |
| None | 99aede4c5f55ec5cf06666a8c9e03ebf9f85f7231223e7f1c38c16e5504835b3 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ecfd63ca125ec74b42f74eeb158b3fa081c8c497 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width