Repository navigation
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.Management.Automation and Microsoft.PowerShell.Security (the latter is needed because of Circular dependency at runtime between System.Management.Automation and Microsoft.PowerShell.Security #14095) but not on the entire PowerShell SDK
- the application depends on the entire PowerShell SDK, but only in self-contained published builds
PS: 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.
Steps to reproduce
The simplest code that triggers the bug is the following:
InitialSessionState sessionState = InitialSessionState.CreateDefault2();
sessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted; // needed to make import work
using PowerShell pwsh = PowerShell.Create(sessionState);
pwsh.AddScript("Import-Module Dism -UseWindowsPowerShell");
pwsh.Invoke();
Console.WriteLine("Script executed");I have a created a VS project (which can be downloaded here) that contains this code and a PublishProfile that can be used to reproduce the bug in the second scenario described above.
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.
Expected behavior
Script executed
is 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)
Actual behavior
The application hangs indefinitely before printing the Script executed message when:
- it depends only on System.Management.Automation and Microsoft.PowerShell.Security v7.0.1+ regardless of the build type
- it depends on Microsoft.PowerShell.SDK v7.0.1+, but only in Published builds
Possible cause
Looking at the stack trace of the pipeline execution thread 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.
This bug was introduced by #12269. (see my analysis below: #13157 (comment))
Activity
vexx32 commentedon Jul 11, 2020
/cc Steve Lee (@SteveL-MSFT) Aditya Patwardhan (@adityapatwardhan)
iSazonov commentedon Jul 13, 2020
I guess the issue could be already fixed in latest builds but you need to replace Appx module in your repro steps with another module because Appx module is not compatible with latest PowerShell 7.1 Preview builds.
daxian-dbw commentedon Jul 13, 2020
Francesco Saltori (@Fs00) The repro project runs correctly for me on Windows 10 1909, where Appx is made compatible with PS 7.0+ (
CompatiblePSEditions = @('Desktop','Core')inAppx.psd1).What version of Windows are you using? I presume it's not the latest Windows 10 but a downlevel OS, right?
If you are using a downlevel machine, then
Appxmodule there may not be compatible with PS 7, and thus the loading of it would go through the Windows module compatibility feature, and it's possible that caused a hang.Loop in Andrew (@anmenaga) to take a look.
Fs00 commentedon Jul 13, 2020
Dongbo Wang (@daxian-dbw) I'm using Windows 10 1909 (not the latest cumulative update though, I'm not at home so I cannot check my
Appx.psd1atm). Did you try it in both the scenarios I listed above?Anyway, I'm almost sure that on my end my application used WinCompat feature because I happened to see the spawned PowerShell child process in the Task Manager (and now I know why it was there).
daxian-dbw commentedon Jul 13, 2020
Yes, I was able to run both scenarios successfully.
Good to know, so the Appx module might not have the
CompatiblePSEditions = @('Desktop','Core')in it. Andrew (@anmenaga) is our SME for the WinCompat module feature, and he will be able to help.iSazonov commentedon Jul 14, 2020
Appx does not work with latest PowerShell 7.1 Preview. See #13138.
Fs00 commentedon Jul 14, 2020
Ilya (@iSazonov) Thanks, that's good to know.
[-][REGRESSION] Since 7.0.1 PowerShell.Create(InitialSessionState) hangs in some circumstances while importing modules[/-][+][REGRESSION] Since 7.0.1 PowerShell hangs while importing WinCompat modules from C# code[/+]Fs00 commentedon Nov 16, 2020
Dongbo Wang (@daxian-dbw) Andrew (@anmenaga) I've done some more testing and discovered that the bug happens only when importing WinCompat modules.
I have updated the repro project and the issue description to be more accurate. The project now targets PowerShell 7.1 and .NET 5.
I'm confident that you'll now be able to reproduce the issue and have enough information to investigate it. Let me know if there are any problems.
anmenaga commentedon Nov 25, 2020
The
Release.pubxmlhas 32-bit<RuntimeIdentifier>win-x86</RuntimeIdentifier>. Is this intentional?Fs00 commentedon Nov 25, 2020
That's intentional, although I don't know if that has an impact on the bug (I haven't tested with x64).
Fs00 commentedon Dec 6, 2020
Any news on this? Did the latest repro project work?
anmenaga commentedon Dec 8, 2020
The latest repro project did work (when using attached publish profile (
Release.pubxml), without it it runs fine).I've looked through WinCompat code and it seems to be behaving as expected, but problems start when it calls PS Remoting code. I assume this has something to do with app running as WoW64 (I vaguely recall remoting bugs with WoW64 from long time ago). This needs more investigation time.
4 remaining items
Fs00 commentedon Feb 24, 2021
Hello Andrew (@anmenaga), is my analysis enough for you to be able to tackle this issue and solve it once and for all?
Thanks in advance!
[-][REGRESSION] Since 7.0.1 PowerShell hangs while importing WinCompat modules when Core engine modules are not found in $PSHOME\Modules[/-][+]Since 7.0.1 PowerShell hangs when a WinCompat module import fails and Core engine modules are not present in $PSHOME\Modules[/+][-]Since 7.0.1 PowerShell hangs when a WinCompat module import fails and Core engine modules are not present in $PSHOME\Modules[/-][+]PowerShell hangs when a WinCompat module import fails and Core engine modules are not present in $PSHOME\Modules[/+][-]PowerShell hangs when a WinCompat module import fails and Core engine modules are not present in $PSHOME\Modules[/-][+]PowerShell hangs when a WinCompat module import fails due to Core engine modules not being found in $PSHOME\Modules[/+]