Search code examples
powershellrecursion

Configure depth overflow value - Start-Job


I have a recursive function that is executed around 750~ times - iterating over XML files and processing. The code is running using Start-Job

Example below:

$job = Start-Job -ScriptBlock {

    function Test-Function {

        Param 
        (
            $count
        )
        Write-Host "Count is: $count"

        $count++
        Test-Function -count $count
    }
    Test-Function -count 1

}

Output:

$job | Receive-Job
Count is: 224
Count is: 225
Count is: 226
Count is: 227
The script failed due to call depth overflow.

The depth overflow occurs at 227 consistently on my machine. If I remove Start-Job, I can reach 750~ (and further). I am using jobs for batch processing.

Is there a way to configure the depth overflow value when using Start-Job?

Is this a limitation of PowerShell Jobs?


Solution

  • I can't answer about the specifics of call depth overflow limitations in PS 5.1 / 7.2 but you could do your recursion based-off a Queue within the job.

    So instead of doing the recursion within the function, you do it from outside (still within the job though).

    Here's what this look like.

    $job = Start-Job -ScriptBlock {
    $Queue = [System.Collections.Queue]::new()
    
        function Test-Function {
    
            Param 
            (
                $count
            )
            Write-Host "Count is: $count"
    
            $count++
            # Next item to process.
            $Queue.Enqueue($Count)
        }
        
        # Call the function once
        Test-Function -count 1
        # Process the queue
        while ($Queue.Count -gt 0) {
            Test-Function -count $Queue.Dequeue()
        }
    }
    
    

    Reference:

    .net Queue class