Skip to content

Process is terminating due to StackOverflowException with custom provider on PSModulePath #5565

Description

Steps to reproduce

$path = '~/code'
pushd $path
Install-module SHiPS 
git clone https://github.com/DarqueWarrior/vsteam.git
$env:PSModulePath += ";$((resolve-path $path).path)"
Import-module vsteam -verbose
Add-TeamAccount -Account <YourVSTSAccountNameOnly> -Drive <DesiredDrive> -Verbose
# Enter your Personal Access Token to VSTS Account

Expected behavior

# Drive will be added
Set-Location <DesiredDrive>:
ls

Actual behavior

Process is terminating due to StackOverflowException.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-rc
PSEdition                      Core
GitCommitId                    v6.0.0-rc
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Workaround

Do not add module path to PSModulePath and the steps below will work.

$path = '~/code'
pushd $path
Install-module SHiPS 
git clone https://github.com/DarqueWarrior/vsteam.git
Import-module ./vsteam -verbose
Add-TeamAccount -Account <YourVSTSAccountNameOnly> -Drive <DesiredDrive> -Verbose
# Enter your Personal Access Token to VSTS Account
# Drive will be 
Set-Location <DesiredDrive>:
ls

Activity

  1. added
    WG-Enginecore PowerShell engine, interpreter, and runtime
    Issue-BugIssue has been identified as a bug in the product
    on Nov 28, 2017
  2. SteveL-MSFT commented on Nov 28, 2017

    @SteveL-MSFT
    Member

    This repros in Windows PowerShell 5.1 so it's not due to any changes we've made in PSCore6

  3. SteveL-MSFT commented on Nov 28, 2017

    @SteveL-MSFT
    Member

    My hypothesis is that the VSTeam module calls into SHiPs module which in turn calls VSTeam module, etc... until stack runs out. cc Jianyun (@jianyunt)

  4. usepowershell commented on Nov 28, 2017

    @usepowershell

    I was able to replicate the error in 5.1 as well. Donovan Brown (@DarqueWarrior) we never tested with the module on psmodulepath with Windows 5.1 - only loading from off psmodulepath.

  5. jianyunt commented on Nov 29, 2017

    @jianyunt
    Contributor

    Something is "wrong" in the steps below so I cannot repro it?

    cd E:\azure
    git clone https://github.com/DarqueWarrior/vsteam.git
    PS E:\azure> install-module ships
    PS E:\azure> cd E:\azure\vsteam
    PS E:\azure\vsteam> ipmo ships
    PS E:\azure\vsteam> $path="e:\azure"
    PS E:\azure\vsteam> $((resolve-path $path).path)
    E:\azure
    PS E:\azure\vsteam> $env:PSModulePath += ";$((resolve-path $path).path)"
    PS E:\azure\vsteam> $env:PSModulePath
    C:\Users\jianyunt\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Progr
    m Files (x86)\Microsoft Azure Information Protection\Powershell;E:\azure
    PS E:\azure\vsteam> Import-module vsteam
    PS E:\azure\vsteam> gmo vsteam | fl
    
    
    Name              : vsteam
    Path              : E:\azure\vsteam\vsteam.psd1
    Description       : Adds functionality for working with Visual Studio Team Services and Team Foundation Server.
    ModuleType        : Manifest
    Version           : 2.0.0
    NestedModules     : {team, git, Approvals, buildDefinitions...}
    ExportedFunctions : {Add-VSTeam, Add-VSTeamAccount, Add-VSTeamAzureRMServiceEndpoint, Add-VSTeamBuild...}
    ExportedCmdlets   :
    ExportedVariables : VSTeamVersionTable
    ExportedAliases   : {Add-AzureRMServiceEndpoint, Add-Build, Add-BuildDefinition, Add-BuildTag...}
    
    
    
    PS E:\azure\vsteam> New-PSDrive -Name vs -PSProvider SHiPS -Root 'VSTeam#VSAccount' -Scope Global -verbose
    VERBOSE: Performing the operation "New drive" on target "Name: vs Provider: Microsoft.PowerShell.SHiPS\SHiPS Root: VSTeam#VSAccount".
    VERBOSE: import-module VSTeam; get-module VSTeam -verbose
    VERBOSE: E:\azure\vsteam\vsteam.psd1
    VERBOSE: using module 'E:\azure\vsteam\vsteam.psd1'; $mod=get-module VSTeam; &($mod){[VSAccount]::new('VSAccount')}
    
    Name           Used (GB)     Free (GB) Provider      Root                                                                                    CurrentLocation
    ----           ---------     --------- --------      ----                                                                                    ---------------
    vs                                     SHiPS         VSTeam#VSAccount
  6. iSazonov commented on Nov 29, 2017

    @iSazonov
    Collaborator

    We have related Issue with discussions about reloading nested modules. #2505

  7. SteveL-MSFT commented on Nov 29, 2017

    @SteveL-MSFT
    Member

    Jianyun (@jianyunt) it doesn't repro if you call new-psdrive outside of the module. it only overflows if new-psdrive is called within the module:

    add-teamaccount -account foo -personal bar -drive vs
  8. DarqueWarrior commented on Nov 29, 2017

    @DarqueWarrior
    ContributorAuthor

    The goal was to offer a much easier way to add a drive using the -Drive parameter. As Steve Lee (@SteveL-MSFT) explained if called separately it works fine. I never remember how to call New-PSDrive and have to look it up so I wanted to save the users of VSTeam from the same issue by simply doing it for them with -Drive.
    The part that confuses me is the fact that if the module is not on PSModulePath everything works but if it is it breaks.

  9. SteveL-MSFT commented on Nov 29, 2017

    @SteveL-MSFT
    Member
  10. jianyunt commented on Nov 30, 2017

    @jianyunt
    Contributor

    It looks like there is a circular dependency in handling nestedmodudule (VSTeam=>Team=>VSTeam) if the module path is in the psmodulepath. The problem has been there in Windows PowerShell too.

    Details:

    The infinite loop is in the GetExportedTypeDefinitions. While new-psdrive, the SHiPS calls “import-module VSTeam; get-module VSTeam” via the PowerShellprovider.SessionState.InvokeCommand.InvokeScript().
    Here is my findings about why loading vsteam module from local path works but it does not if under psmodulepath.

    1. Import-module vsteam from a local path. This case works fine. From my watch window, you can see VSTeam has a nestedmodule, Team but it has no nestedmodules.
      image

    2. While VSTeam module is in the psmodulepath, from the debugger you can see VSteam has NestedModule, Team, which a nestedmodule, VSTeam, which is causing module loading infinitely.
      image

     Stack
    System.Management.Automation.dll!System.Management.Automation.PSModuleInfo.GetExportedTypeDefinitions() Line 580 (e:\powershell\src\System.Management.Automation\engine\Modules\PSModuleInfo.cs:580)
    System.Management.Automation.dll!System.Management.Automation.AnalysisCache.CacheModuleExports(System.Management.Automation.PSModuleInfo module, System.Management.Automation.ExecutionContext context) Line 465 (e:\powershell\src\System.Management.Automation\engine\Modules\AnalysisCache.cs:465)
    System.Management.Automation.dll!Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingModulePath(System.Management.Automation.PSModuleInfo parentModule, bool found, System.Collections.Generic.IEnumerable<string> modulePath, string name, System.Management.Automation.SessionState ss, Microsoft.PowerShell.Commands.ModuleCmdletBase.ImportModuleOptions options, Microsoft.PowerShell.Commands.ModuleCmdletBase.ManifestProcessingFlags manifestProcessingFlags, out System.Management.Automation.PSModuleInfo module) Line 345 (e:\powershell\src\System.Management.Automation\engine\Modules\ModuleCmdletBase.cs:345)
    System.Management.Automation.dll!Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingModulePath(bool found, System.Collections.Generic.IEnumerable<string> modulePath, string name, System.Management.Automation.SessionState ss, Microsoft.PowerShell.Commands.ModuleCmdletBase.ImportModuleOptions options, Microsoft.PowerShell.Commands.ModuleCmdletBase.ManifestProcessingFlags manifestProcessingFlags, out System.Management.Automation.PSModuleInfo module) Line 276 (e:\powershell\src\System.Management.Automation\engine\Modules\ModuleCmdletBase.cs:276)
    System.Management.Automation.dll!Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(Microsoft.PowerShell.Commands.ModuleCmdletBase.ImportModuleOptions importModuleOptions, string name) Line 708 (e:\powershell\src\System.Management.Automation\engine\Modules\ImportModuleCommand.cs:708)
    System.Management.Automation.dll!Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord() Line 1731 (e:\powershell\src\System.Management.Automation\engine\Modules\ImportModuleCommand.cs:1731)
    System.Management.Automation.dll!System.Management.Automation.Cmdlet.DoProcessRecord() Line 177 (e:\powershell\src\System.Management.Automation\engine\cmdlet.cs:177)
    System.Management.Automation.dll!System.Management.Automation.CommandProcessor.ProcessRecord() Line 360 
    

    Repro steps

    cd E:\azure\vsteam
    Import-module SHipS
    $path="e:\azure"
    $((resolve-path $path).path)
    $env:PSModulePath += ";$((resolve-path $path).path)"
    Import-module VSTeam -verbose
    $secure=convertTo-SecureString "htbaixci3me7dag364fgp2hccdgzh6zznrvhvfgjzkxrrtmwpa" -AsPlainText -Force
    Add-TeamAccount -Account http://10.0.0.5:8080/tfs/DefaultCollection -Drive vss -verbose -SecurePersonalAccessToken $secure
    

    Recommended workaround:

    I think by separating teamspsdrive from vsteam module will likely workaround the problem.

  11. SteveL-MSFT commented on Nov 30, 2017

    @SteveL-MSFT
    Member

    Looking at the code in GetExportedTypesDefinitions, it appears to be a TODO to handle circular dependencies and caching nested module types. We should fix this in 6.1, but existing usage will need to use the workaround particularly if targeting Windows PowerShell 5.1 as well.

  12. DarqueWarrior commented on Dec 6, 2017

    @DarqueWarrior
    ContributorAuthor

    I just want to confirm that the circular dependencies are implicit and not explicit correct? I don't see where I am referencing the two modules against each other explicitly.

  13. SteveL-MSFT commented on Dec 6, 2017

    @SteveL-MSFT
    Member

    Donovan Brown (@DarqueWarrior) yes, it is implicit due to how SHiPS calls back into the module to access the class since we don't supported exposing classes via ExportedTypes yet.

  14. rjmholt commented on Feb 23, 2021

    @rjmholt
    Collaborator

    Was this issue fixed in #6843?

  15. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  16. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  17. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  18. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions