dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.25k stars 4.73k forks source link

[F.Rq.] Some way to configure the size of the stack #107183

Open YoshiRulz opened 2 months ago

YoshiRulz commented 2 months ago

Per discussions at #96347 (there is no official documentation†), the size of the default/global stack is fixed and differs between OSes. ASP.NET devs can use the stackSize setting (only with IIS?) to configure it. I would like to do the same with a CLI or desktop app. A new API or an environment variable would also be acceptable for me. I would use this to decrease the size in order to root out stealthy bugs, but I can also see it being used to increase the size for some non-ASP.NET server or other high-perf use case.

See also:

† There seemed to be no official documentation for the stack in modern .NET, and info for .NET Framework and Mono was all over the place, so I've collected everything I've found here.

janvorli commented 2 months ago

On Unix, you can set the default stack size this way:

On all OSes, but only for secondary threads, since the main thread stack size is set by the OS when launching the application and the runtime cannot influence it.

On Windows, you can also modify the stack size set in the host executable (if you build your app, you end up having YourApp.exe and YourApp.dll, the .exe is the host)

You can also specify the thread stack size when creating new threads in your code in the constructor of the thread (public Thread (System.Threading.ThreadStart start, int maxStackSize)). However, that doesn't work for threadpool threads.

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

janvorli commented 2 months ago

@YoshiRulz is this sufficient for you or would you like something additional?

YoshiRulz commented 1 month ago

My team develops on and builds for Windows and Linux, so I think we could use a combination of those 2 workarounds, but it still seems odd that there's no way to change the stack size for the main thread.

edit: I maintain that a compile-time EDITBIN invocation and a runtime ulimit invocation are both workarounds. For one, I imagine they can't be used on Android. BASH on macOS might have ulimit, not sure.

janvorli commented 1 month ago

it still seems odd that there's no way to change the stack size for the main thread.

@YoshiRulz there are ways to change it for the main thread both on Windows and Linux that I've mentioned in my previous comment:

janvorli commented 1 month ago

@YoshiRulz what's wrong with these ways? The default stack size for the main thread is something only the operating system can set, because it needs to be set before the process is launched and the application itself doesn't have any way to control it.

YoshiRulz commented 1 month ago

As per my edit above, they're not cross-platform, and one is compile-time and one is runtime.

If the runtime can't change its own stack size, perhaps it could spawn a new thread with the stack size specified in the assembly metadata (for example) and begin execution of the main method on that thread?