Skip to content

PowerShell hangs when a WinCompat module import fails due to Core engine modules not being found in $PSHOME\Modules #13157

Closed

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:

  1. 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
  2. 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

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Jul 11, 2020
  2. iSazonov commented on Jul 13, 2020

    @iSazonov
    Collaborator

    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.

  3. added
    WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of modules
    on Jul 13, 2020
  4. daxian-dbw commented on Jul 13, 2020

    @daxian-dbw
    Member

    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') in Appx.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 Appx module 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.

  5. Fs00 commented on Jul 13, 2020

    @Fs00
    ContributorAuthor

    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.psd1 atm). 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).

  6. daxian-dbw commented on Jul 13, 2020

    @daxian-dbw
    Member

    Did you try it in both the scenarios I listed above?

    Yes, I was able to run both scenarios successfully.

    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).

    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.

  7. iSazonov commented on Jul 14, 2020

    @iSazonov
    Collaborator

    Appx does not work with latest PowerShell 7.1 Preview. See #13138.

  8. Fs00 commented on Jul 14, 2020

    @Fs00
    ContributorAuthor

    Ilya (@iSazonov) Thanks, that's good to know.

  9. changed the title [-][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[/+] on Nov 16, 2020
  10. Fs00 commented on Nov 16, 2020

    @Fs00
    ContributorAuthor

    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.

  11. anmenaga commented on Nov 25, 2020

    @anmenaga

    The Release.pubxml has 32-bit <RuntimeIdentifier>win-x86</RuntimeIdentifier>. Is this intentional?

  12. Fs00 commented on Nov 25, 2020

    @Fs00
    ContributorAuthor

    That's intentional, although I don't know if that has an impact on the bug (I haven't tested with x64).

  13. Fs00 commented on Dec 6, 2020

    @Fs00
    ContributorAuthor

    Any news on this? Did the latest repro project work?

  14. anmenaga commented on Dec 8, 2020

    @anmenaga

    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.

  15. 4 remaining items

  16. Fs00 commented on Feb 24, 2021

    @Fs00
    ContributorAuthor

    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!

  17. added
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of modules
    on Mar 1, 2021
  18. changed the title [-][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[/+] on Jul 3, 2021
  19. changed the title [-]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[/+] on Jul 3, 2021
  20. changed the title [-]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[/+] on Jul 3, 2021
  21. ghost added
    In-PRIndicates that a PR is out for the issue
    on Jan 10, 2022
  22. ghost removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Jan 12, 2022
  23. ghost removed
    In-PRIndicates that a PR is out for the issue
    on Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Engine-Module

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions