0

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

7
  • You're asking to debug 6239 lines of code in your .psm1 to find which of the functions is using recursion and then understanding why said function is causing an infinite loop ? Commented Dec 4, 2023 at 17:12
  • Not at all, You just answered one of my questions that I actually didn't put above. A specific function part of the module can do that ? Commented Dec 4, 2023 at 17:16
  • 1
    If the config file isn't found Initialize-SEPMConfiguration will call Reset-SEPMConfiguration which then calls Initialize-SEPMConfiguration which then calls Reset-SEPMConfiguration... you get the point :) Remove that cyclic path and you should be good Commented Dec 4, 2023 at 17:16
  • the issue is Initialize-SepmConfiguration calling Reset-SEPMConfiguration which then is calling again Initialize-SepmConfiguration causing an infinite loop. Commented Dec 4, 2023 at 17:17
  • 1
    @Douda Parsed the file with [Parser]::ParseFile(), then listed all statements at the root of the script - there was only one command invocation, so that narrowed it down pretty quickly Commented Dec 4, 2023 at 17:25

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.