Title: PowerShell hangs when a WinCompat module import fails due to Core engine modules not being found in $PSHOME\Modules · Issue #13157 · PowerShell/PowerShell · GitHub
Open Graph Title: PowerShell hangs when a WinCompat module import fails due to Core engine modules not being found in $PSHOME\Modules · Issue #13157 · PowerShell/PowerShell
X Title: PowerShell hangs when a WinCompat module import fails due to Core engine modules not being found in $PSHOME\Modules · Issue #13157 · PowerShell/PowerShell
Description: I've encountered an issue using PowerShell v7.0.1 or higher in a .NET Windows application. This bug causes a hang when PS fails to import a module through WinCompat in these two scenarios: the application depends only on System.Managemen...
Open Graph Description: I've encountered an issue using PowerShell v7.0.1 or higher in a .NET Windows application. This bug causes a hang when PS fails to import a module through WinCompat in these two scenarios: the appl...
X Description: I've encountered an issue using PowerShell v7.0.1 or higher in a .NET Windows application. This bug causes a hang when PS fails to import a module through WinCompat in these two scenarios: the ...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/13157
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PowerShell hangs when a WinCompat module import fails due to Core engine modules not being found in $PSHOME\\Modules","articleBody":"I've encountered an issue using PowerShell **v7.0.1 or higher** in a .NET Windows application.\r\nThis bug causes a hang when PS fails to import a module through WinCompat in these two scenarios:\r\n1. the application depends only on System.Management.Automation and Microsoft.PowerShell.Security (the latter is needed because of #14095) but **not on the entire PowerShell SDK**\r\n2. the application depends on the entire PowerShell SDK, but only in self-contained published builds\r\n\r\nPS: I know that applications using PowerShell should depend on the entire SDK and not just on System.Management.Automation, but in my case I have an application that only needs to run some commands on the machine and then operate on their output. The full SDK contains 20MB+ of dependencies that my application never uses and significantly bloat the distributable EXE.\r\n\r\n## Steps to reproduce\r\n\r\nThe simplest code that triggers the bug is the following:\r\n```csharp\r\nInitialSessionState sessionState = InitialSessionState.CreateDefault2();\r\nsessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted; // needed to make import work\r\nusing PowerShell pwsh = PowerShell.Create(sessionState);\r\npwsh.AddScript(\"Import-Module Dism -UseWindowsPowerShell\");\r\npwsh.Invoke();\r\nConsole.WriteLine(\"Script executed\");\r\n```\r\nI have a created a VS project (which can be [downloaded here](https://drive.google.com/file/d/118wAPM5LGOyI3RAdUAxrBN9E_KLjXIpL/view?usp=sharing)) that contains this code and a PublishProfile that can be used to reproduce the bug in the second scenario described above.\r\n\r\n**Important:** when removing the project dependency on Microsoft.PowerShell.SDK remember to remove the `bin` directory inside the project folder! Otherwise you'll get inconsistent results.\r\n\r\n## Expected behavior\r\n\r\n```\r\nScript executed\r\n```\r\nis printed to the standard output and then the application exits gracefully. (in scenario 1 `Import-Module` command will fail anyway, since we don't have `Microsoft.PowerShell.Utility` module that is required for WinCompat)\r\n\r\n## Actual behavior\r\n\r\nThe application hangs indefinitely before printing the `Script executed` message when:\r\n- it depends only on System.Management.Automation and Microsoft.PowerShell.Security v7.0.1+ regardless of the build type\r\n- it depends on Microsoft.PowerShell.SDK v7.0.1+, but **only in Published builds**\r\n\r\n### Possible cause\r\nLooking at the [stack trace of the pipeline execution thread](https://pastebin.com/uvQkdxZ0) while the application hangs in scenario 1, the thread appears to be stuck in an infinite loop when trying to import the `Microsoft.PowerShell.Utility` module.\r\nThis bug was introduced by #12269. (see my analysis below: https://github.com/PowerShell/PowerShell/issues/13157#issuecomment-741975648)","author":{"url":"https://github.com/Fs00","@type":"Person","name":"Fs00"},"datePublished":"2020-07-11T14:09:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/13157/PowerShell/issues/13157"}
| 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:78788953-7aa3-6964-5475-8931691d694f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A240:21C9D6:23C33E4:2FDBEEB:6A547024 |
| html-safe-nonce | e9fb77825b30263d4b0afebce2485cd55198ff75281fe69d5811b0ce17b59af9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMjQwOjIxQzlENjoyM0MzM0U0OjJGREJFRUI6NkE1NDcwMjQiLCJ2aXNpdG9yX2lkIjoiNzA4NzY1NzA3Nzc3MTEwNDI5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a4dd0a9f3e2c54ae59210eca8ea5c38c0961b0e30186ea0c77d1042f32cd00ad |
| hovercard-subject-tag | issue:655215458 |
| 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/13157/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f1401654b9573294a78a286088ef56533e92753c133e25791ac8ace0688ea279/PowerShell/PowerShell/issues/13157 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f1401654b9573294a78a286088ef56533e92753c133e25791ac8ace0688ea279/PowerShell/PowerShell/issues/13157 |
| og:image:alt | I've encountered an issue using PowerShell v7.0.1 or higher in a .NET Windows application. This bug causes a hang when PS fails to import a module through WinCompat in these two scenarios: the appl... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Fs00 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| 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 | 6d28624802c2f7d9500239dd6870ed7031b7353b |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width