Skip to content

Commit dfd63aa

Browse files
authored
Increase stack size for PowerShell 5 (#1797)
Sets the `Thread` max stack size to use the same value that is normally used in console host. This gives PowerShell code more room for execution, so recursive calls, or calls that are wrapped in Pester (which itself is stack-hungry), won't fail with: ``` System.Management.Automation.ScriptCallDepthException: The script failed due to call depth overflow. ``` Fixes: PowerShell/vscode-powershell#3962
1 parent 93f22b5 commit dfd63aa

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,16 @@ public PsesInternalHost(
112112
_runspaceStack = new Stack<RunspaceFrame>();
113113
_cancellationContext = new CancellationContext();
114114

115-
_pipelineThread = new Thread(Run)
115+
// Default stack size on .NET Framework is 524288 (512KB) (as reported by GetProcessDefaultStackSize)
116+
// this leaves very little room in the stack. Windows PowerShell internally sets the value based on
117+
// PipelineMaxStackSizeMB as seen here: https://github.com/PowerShell/PowerShell/issues/1187,
118+
// which has default of 10 and multiplies that by 1_000_000, so the default stack size is
119+
// 10_000_000 (~10MB) when starting in normal console host.
120+
//
121+
// For PS7 the value is ignored by .NET because settings the stack size is not supported, but we can
122+
// still provide 0, which means fallback to the default in both .NET and .NET Framework.
123+
int maxStackSize = VersionUtils.IsPS5 ? 10_000_000 : 0;
124+
_pipelineThread = new Thread(Run, maxStackSize)
116125
{
117126
Name = "PSES Pipeline Execution Thread",
118127
};

0 commit comments

Comments
 (0)