powershellpowershell-module

Import-module : The script failed due to call depth overflow


Working on a PowerShell module and noticed I'm having an issue when attempting to import it, no matter how

import-Module -Name ".\0.0.1\PSSymantecSEPM.psd1" -Force
An error occurred while running the REPL loop:
System.Management.Automation.ScriptCallDepthException: The script failed due to call depth overflow.
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeAndClear[TResult](PowerShell pwsh, PSInvocationSettings invocationSettings) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 76

After investigation, understood it's linked to recursion depth limit issue well described in a VSCode issue, but in my case it's not VSCode specific as I can reproduce without the integrated terminal

I assume now it's linked to my module directly, or manifest that somehow reaches this depth limit.

Anyone can point me to where/how I could fix this for this module ? Prepared some easy steps to reproduce via my personal project repo and specific branch I created to investigate this

# Closing repo
PS C:\Temp\test> git clone https://github.com/Douda/PSSymantecSEPM/
PS C:\Temp\test> cd .\PSSymantecSEPM\
# Switch to branch with issue
PS C:\Temp\test\PSSymantecSEPM> git checkout ScriptCallDepthException
Switched to a new branch 'ScriptCallDepthException'
branch 'ScriptCallDepthException' set up to track 'origin/ScriptCallDepthException'.
# attempting to import the module
PS C:\Temp\test\PSSymantecSEPM> Import-Module .\Output\PSSymantecSEPM\0.0.1\PSSymantecSEPM.psd1 -Force

The script failed due to call depth overflow.
    + CategoryInfo          : InvalidOperation: (0:Int32) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CallDepthOverflow

Tried with PS 5.1 & PS 7.4.0, same issue

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

I may also note, I am using ModuleBuilder to generate my module (and maintain versioning) but not sure it's linked

Thanks


Solution

  • As mentioned in the comments, this is because of a cyclic relationship between Initialize-SEPMConfiguration and Reset-SEPMConfiguration.

    Initialize-SEPMConfiguration is invoked on module execution/import, fails to find the expected configuration values in a (possibly non-existing) configuration file, and calls Reset-SEPMConfiguration.

    Reset-SEPMConfiguration appears to remove the config file (which possibly isn't even there), after which it calls Initialize-SEPMConfiguration, thus creating a call cycle that eventually exceeds your callstack's capacity.

    Remove the call back to Initialize-SEPMConfiguration from Reset-SEPMConfiguration and you'll be fine - my recommendation would be to implement some bounded retry-logic inside Initialize-SEPMConfiguration instead of recursing.