Skip to content

Commit 3d66cc3

Browse files
committed
src: raise the maximum open files limit on Windows
On Windows, libuv uses the CRT's _open_osfhandle when opening files. This is constrained by the CRT's maxstdio limit, which defaults to 512, often causing "EMFILE: too many open files" errors for heavy workloads. This commit raises this limit on Windows up to the CRT maximum (8192) by invoking _setmaxstdio during PlatformInit, mirroring the POSIX behavior. Fixes: #44832 Signed-off-by: 郑苏波 (Super Zheng) <superzheng@tencent.com>
1 parent dbec31c commit 3d66cc3

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/node.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,23 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) {
638638
}
639639
#endif // __POSIX__
640640
#ifdef _WIN32
641+
if (!(flags & ProcessInitializationFlags::kNoAdjustResourceLimits)) {
642+
// Raise the open file descriptor limit on Windows.
643+
// By default, up to 512 files can be open simultaneously at the stream I/O level.
644+
// We increase this limit to the maximum possible (up to 8192) using _setmaxstdio.
645+
// Since libuv internally calls _open_osfhandle when opening files on Windows, which
646+
// is provided by the CRT and constrained by the maxstdio limit, raising this limit
647+
// helps prevent "EMFILE: too many open files" errors.
648+
// We try to set the limit starting from the maximum of 8192 down to 512.
649+
static constexpr int MIN_OPEN_FILES_LIMIT = 512;
650+
static constexpr int MAX_OPEN_FILES_LIMIT = 8192;
651+
static constexpr int STEP_OPEN_FILES_LIMIT = 512;
652+
for (int i = MAX_OPEN_FILES_LIMIT; i >= MIN_OPEN_FILES_LIMIT; i -= STEP_OPEN_FILES_LIMIT) {
653+
if (_setmaxstdio(i) != -1)
654+
break;
655+
}
656+
}
657+
641658
if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) {
642659
for (int fd = 0; fd <= 2; ++fd) {
643660
auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));

0 commit comments

Comments
 (0)