From b644e5750e9a590e7ea64cc0c4dbda9afaf5643f Mon Sep 17 00:00:00 2001 From: Brian McKenna Date: Fri, 15 Nov 2024 10:45:29 +1100 Subject: [PATCH 1/2] Remove broken stack size logic from Windows The API only changes the stack size once there's already a stack overflow exception. Pretty useless. --- src/libutil/current-process.cc | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index ac01f441e..46e72b63a 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -19,10 +19,6 @@ # include "namespaces.hh" #endif -#ifndef _WIN32 -# include -#endif - namespace nix { unsigned int getMaxCPU() @@ -77,29 +73,6 @@ 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(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 } From 7c8c71f8e9319b17e85c0f510bfd0d0558361ac2 Mon Sep 17 00:00:00 2001 From: Brian McKenna Date: Fri, 31 Jan 2025 21:11:45 +1100 Subject: [PATCH 2/2] Totally exclude nix::setStackSize on Windows --- src/libutil/current-process.cc | 4 ++-- src/libutil/current-process.hh | 3 +++ src/nix/main.cc | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index 46e72b63a..255ae2cf5 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -51,11 +51,11 @@ unsigned int getMaxCPU() ////////////////////////////////////////////////////////////////////// +#ifndef _WIN32 size_t savedStackSize = 0; void setStackSize(size_t stackSize) { - #ifndef _WIN32 struct rlimit limit; if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) { savedStackSize = limit.rlim_cur; @@ -73,8 +73,8 @@ void setStackSize(size_t stackSize) ); } } - #endif } +#endif void restoreProcessContext(bool restoreMounts) { diff --git a/src/libutil/current-process.hh b/src/libutil/current-process.hh index 8286bf89d..660dcfe0b 100644 --- a/src/libutil/current-process.hh +++ b/src/libutil/current-process.hh @@ -17,10 +17,13 @@ namespace nix { */ unsigned int getMaxCPU(); +// It does not seem possible to dynamically change stack size on Windows. +#ifndef _WIN32 /** * Change the stack size. */ void setStackSize(size_t stackSize); +#endif /** * Restore the original inherited Unix process context (such as signal diff --git a/src/nix/main.cc b/src/nix/main.cc index b0e26e093..80ef53084 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -557,9 +557,11 @@ void mainWrapped(int argc, char * * argv) int main(int argc, char * * argv) { +#ifndef _WIN32 // Increase the default stack size for the evaluator and for // libstdc++'s std::regex. nix::setStackSize(64 * 1024 * 1024); +#endif return nix::handleExceptions(argv[0], [&]() { nix::mainWrapped(argc, argv);