Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
YoshiRulz opened this issue Aug 30, 2024 · 7 comments
Open

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

YoshiRulz opened this issue Aug 30, 2024 · 7 comments

Comments

@YoshiRulz
Copy link

YoshiRulz commented Aug 30, 2024

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:

  • Previous requests #11634 and #12042, which were closed as resolved, but OP in #96347 says the new flag was never exposed for public use.
  • #30034 was requesting a new API to get the currently applicable stack size at runtime. Closed as stale.
  • #8861 and #33622 inquiring as to why the stack is a different size on different OSes.

† 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.

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 30, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Aug 30, 2024
@janvorli
Copy link
Member

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

  • Using the ulimit -s shell command. That sets it for the main thread and on glibc based distros for secondary threads too. On MUSL based distros like Alpine, the DOTNET_DefaultStackSize needs to be used for the secondary threads.

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.

  • Using the DOTNET_DefaultStackSize env variable
  • Using DEFAULT_STACK_SIZE property passed to coreclr_initialize in case you have your own custom .NET host

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)

  • To set the stack size to e.g. 4MB: editbin.exe YourApp.exe /STACK:0x400000

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.

Copy link
Contributor

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

@janvorli janvorli removed the untriaged New issue has not been triaged by the area owner label Sep 3, 2024
@janvorli
Copy link
Member

janvorli commented Sep 3, 2024

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

@janvorli janvorli added this to the 10.0.0 milestone Sep 3, 2024
@YoshiRulz
Copy link
Author

YoshiRulz commented Sep 4, 2024

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
Copy link
Member

janvorli commented Sep 4, 2024

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:

  • Using the ulimit -s shell command on Linux
  • Modify the stack size set in the host executable on Windows using the editbin.exe.

@janvorli
Copy link
Member

janvorli commented Sep 4, 2024

@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.

@vcsjones vcsjones removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 4, 2024
@YoshiRulz
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants