1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-06 00:51:47 +02:00

Merge pull request #12249 from nix-windows/stack-size

Remove broken stack size logic from Windows
This commit is contained in:
John Ericson 2025-01-31 22:57:01 -05:00 committed by GitHub
commit d949c8de7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 29 deletions

View file

@ -19,10 +19,6 @@
# include "namespaces.hh" # include "namespaces.hh"
#endif #endif
#ifndef _WIN32
# include <sys/resource.h>
#endif
namespace nix { namespace nix {
unsigned int getMaxCPU() unsigned int getMaxCPU()
@ -55,11 +51,11 @@ unsigned int getMaxCPU()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
#ifndef _WIN32
size_t savedStackSize = 0; size_t savedStackSize = 0;
void setStackSize(size_t stackSize) void setStackSize(size_t stackSize)
{ {
#ifndef _WIN32
struct rlimit limit; struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) { if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) {
savedStackSize = limit.rlim_cur; savedStackSize = limit.rlim_cur;
@ -77,31 +73,8 @@ void setStackSize(size_t stackSize)
); );
} }
} }
#else
ULONG_PTR stackLow, stackHigh;
GetCurrentThreadStackLimits(&stackLow, &stackHigh);
ULONG maxStackSize = stackHigh - stackLow;
ULONG currStackSize = 0;
// This retrieves the current promised stack size
SetThreadStackGuarantee(&currStackSize);
if (currStackSize < stackSize) {
savedStackSize = currStackSize;
ULONG newStackSize = std::min(static_cast<ULONG>(stackSize), maxStackSize);
if (SetThreadStackGuarantee(&newStackSize) == 0) {
logger->log(
lvlError,
HintFmt(
"Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): %4%",
savedStackSize,
stackSize,
maxStackSize,
std::to_string(GetLastError())
).str()
);
}
}
#endif
} }
#endif
void restoreProcessContext(bool restoreMounts) void restoreProcessContext(bool restoreMounts)
{ {

View file

@ -17,10 +17,13 @@ namespace nix {
*/ */
unsigned int getMaxCPU(); unsigned int getMaxCPU();
// It does not seem possible to dynamically change stack size on Windows.
#ifndef _WIN32
/** /**
* Change the stack size. * Change the stack size.
*/ */
void setStackSize(size_t stackSize); void setStackSize(size_t stackSize);
#endif
/** /**
* Restore the original inherited Unix process context (such as signal * Restore the original inherited Unix process context (such as signal

View file

@ -557,9 +557,11 @@ void mainWrapped(int argc, char * * argv)
int main(int argc, char * * argv) int main(int argc, char * * argv)
{ {
#ifndef _WIN32
// Increase the default stack size for the evaluator and for // Increase the default stack size for the evaluator and for
// libstdc++'s std::regex. // libstdc++'s std::regex.
nix::setStackSize(64 * 1024 * 1024); nix::setStackSize(64 * 1024 * 1024);
#endif
return nix::handleExceptions(argv[0], [&]() { return nix::handleExceptions(argv[0], [&]() {
nix::mainWrapped(argc, argv); nix::mainWrapped(argc, argv);