Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/coreclr/nativeaot/Bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#include <stdint.h>
#include <minipal/descriptorlimit.h>

#if defined(DEBUG) && defined(_WIN32)
#include <process.h>
Expand Down Expand Up @@ -223,6 +224,8 @@ int main(int argc, char* argv[])
if (initval != 0)
return initval;

minipal_increase_descriptor_limit();

#if defined(DEBUG) && defined(_WIN32)
// work around Debug UCRT shutdown issues: https://github.com/dotnet/runtime/issues/108640
int exitCode = __managed__Main(argc, argv);
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ if(CLR_CMAKE_HOST_ARCH_ARM64 AND CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_TARGET
endif(CLR_CMAKE_HOST_ARCH_ARM64 AND CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_TARGET_LINUX_MUSL)

if(CLR_CMAKE_TARGET_LINUX_MUSL)
# Setting RLIMIT_NOFILE breaks debugging of coreclr on Alpine Linux for some reason
add_definitions(-DDONT_SET_RLIMIT_NOFILE)
# On Alpine Linux, we need to ensure that the reported stack range for the primary thread is
# larger than the initial committed stack size.
add_definitions(-DENSURE_PRIMARY_STACK_SIZE)
Expand Down
51 changes: 2 additions & 49 deletions src/coreclr/pal/src/init/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SET_DEFAULT_DEBUG_CHANNEL(PAL); // some headers have code with asserts, so do th
#include "pal/cgroup.h"
#include <minipal/getexepath.h>
#include <minipal/memorybarrierprocesswide.h>
#include <minipal/descriptorlimit.h>

#if HAVE_MACH_EXCEPTIONS
#include "../exception/machexception.h"
Expand All @@ -48,7 +49,6 @@ SET_DEFAULT_DEBUG_CHANNEL(PAL); // some headers have code with asserts, so do th
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <limits.h>
#include <string.h>
Expand Down Expand Up @@ -116,7 +116,6 @@ static minipal_mutex* init_critsec = NULL;
static DWORD g_initializeDLLFlags = PAL_INITIALIZE_DLL;

static int Initialize(int argc, const char *const argv[], DWORD flags);
static BOOL INIT_IncreaseDescriptorLimit(void);
static LPWSTR INIT_FormatCommandLine (int argc, const char * const *argv);
static LPWSTR INIT_GetCurrentEXEPath();
static BOOL INIT_SharedFilesPath(void);
Expand Down Expand Up @@ -401,7 +400,7 @@ Initialize(
goto CLEANUP1;
}

if (!INIT_IncreaseDescriptorLimit())
if (!minipal_increase_descriptor_limit())
{
ERROR("Unable to increase the file descriptor limit!\n");
// We can continue if this fails; we'll just have problems if
Expand Down Expand Up @@ -906,52 +905,6 @@ void PALInitUnlock(void)

/* Internal functions *********************************************************/

/*++
Function:
INIT_IncreaseDescriptorLimit [internal]

Abstract:
Calls setrlimit(2) to increase the maximum number of file descriptors
this process can open.

Return value:
TRUE if the call to setrlimit succeeded; FALSE otherwise.
--*/
static BOOL INIT_IncreaseDescriptorLimit(void)
{
#ifdef TARGET_WASM
// WebAssembly cannot set limits
return TRUE;
#endif
#ifndef DONT_SET_RLIMIT_NOFILE
struct rlimit rlp;
int result;

result = getrlimit(RLIMIT_NOFILE, &rlp);
if (result != 0)
{
return FALSE;
}
// Set our soft limit for file descriptors to be the same
// as the max limit.
rlp.rlim_cur = rlp.rlim_max;
#ifdef __APPLE__
// Based on compatibility note in setrlimit(2) manpage for OSX,
// trim the limit to OPEN_MAX.
if (rlp.rlim_cur > OPEN_MAX)
{
rlp.rlim_cur = OPEN_MAX;
}
#endif
result = setrlimit(RLIMIT_NOFILE, &rlp);
if (result != 0)
{
return FALSE;
}
#endif // !DONT_SET_RLIMIT_NOFILE
return TRUE;
}

/*++
Function:
INIT_FormatCommandLine [Internal]
Expand Down
2 changes: 0 additions & 2 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,6 @@ set(FULL_VERSION ${product_version_string})
######################################

if(CLR_CMAKE_TARGET_LINUX_MUSL)
# Setting RLIMIT_NOFILE breaks debugging of coreclr on musl-libc for some reason
add_definitions(-DDONT_SET_RLIMIT_NOFILE)
# On musl-libc, we need to ensure that the reported stack range for the primary thread is
# larger than the initial committed stack size.
add_definitions(-DENSURE_PRIMARY_STACK_SIZE)
Expand Down
28 changes: 3 additions & 25 deletions src/mono/mono/mini/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include <mono/metadata/components.h>
#include <mono/mini/debugger-agent-external.h>

#include <minipal/descriptorlimit.h>

#include "mini.h"
#include <mono/jit/jit.h>
#include "aot-compiler.h"
Expand All @@ -68,9 +70,6 @@
#include <string.h>
#include <ctype.h>
#include <locale.h>
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif

static FILE *mini_stats_fd;

Expand Down Expand Up @@ -1876,27 +1875,6 @@ mono_set_use_smp (int use_smp)
#endif
}

static void
increase_descriptor_limit (void)
{
#if defined(HAVE_GETRLIMIT) && !defined(DONT_SET_RLIMIT_NOFILE)
struct rlimit limit;

if (getrlimit (RLIMIT_NOFILE, &limit) == 0) {
// Set our soft limit for file descriptors to be the same
// as the max limit.
limit.rlim_cur = limit.rlim_max;
#ifdef __APPLE__
// Based on compatibility note in setrlimit(2) manpage for OSX,
// trim the limit to OPEN_MAX.
if (limit.rlim_cur > OPEN_MAX)
limit.rlim_cur = OPEN_MAX;
#endif
setrlimit (RLIMIT_NOFILE, &limit);
}
#endif
}

#define MONO_HANDLERS_ARGUMENT "--handlers="
#define MONO_HANDLERS_ARGUMENT_LEN STRING_LENGTH(MONO_HANDLERS_ARGUMENT)

Expand Down Expand Up @@ -2024,7 +2002,7 @@ mono_main (int argc, char* argv[])

setlocale (LC_ALL, "");

increase_descriptor_limit ();
minipal_increase_descriptor_limit ();

if (g_hasenv ("MONO_NO_SMP"))
mono_set_use_smp (FALSE);
Expand Down
7 changes: 7 additions & 0 deletions src/native/minipal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include(configure.cmake)

set(SOURCES
cpufeatures.c
descriptorlimit.c
memorybarrierprocesswide.c
mutex.c
guid.c
Expand All @@ -15,6 +16,12 @@ set(SOURCES
log.c
)

# Setting RLIMIT_NOFILE breaks debugging of coreclr on Alpine Linux for some reason
# Setting limits in Browser/WASI doesn't make sense
if(CLR_CMAKE_TARGET_LINUX_MUSL OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI)
add_definitions(-DDONT_SET_RLIMIT_NOFILE)
endif()

# Provide an object library for scenarios where we ship static libraries
include_directories(${CLR_SRC_NATIVE_DIR} ${CMAKE_CURRENT_BINARY_DIR})

Expand Down
1 change: 1 addition & 0 deletions src/native/minipal/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include(CheckSymbolExists)
check_include_files("windows.h;bcrypt.h" HAVE_BCRYPT_H)
check_include_files("sys/auxv.h;asm/hwcap.h" HAVE_AUXV_HWCAP_H)
check_include_files("asm/hwprobe.h" HAVE_HWPROBE_H)
check_include_files("sys/resource.h" HAVE_RESOURCE_H)

check_function_exists(sysctlbyname HAVE_SYSCTLBYNAME)
check_function_exists(fsync HAVE_FSYNC)
Expand Down
41 changes: 41 additions & 0 deletions src/native/minipal/descriptorlimit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "minipalconfig.h"

#if HAVE_RESOURCE_H
#include <sys/resource.h>
#endif

#include "descriptorlimit.h"

bool minipal_increase_descriptor_limit(void)
{
#if HAVE_RESOURCE_H && !defined(DONT_SET_RLIMIT_NOFILE)
struct rlimit rlp;
int result;

result = getrlimit(RLIMIT_NOFILE, &rlp);
if (result != 0)
{
return false;
}
// Set our soft limit for file descriptors to be the same
// as the max limit.
rlp.rlim_cur = rlp.rlim_max;
#ifdef __APPLE__
// Based on compatibility note in setrlimit(2) manpage for OSX,
// trim the limit to OPEN_MAX.
if (rlp.rlim_cur > OPEN_MAX)
{
rlp.rlim_cur = OPEN_MAX;
}
#endif
result = setrlimit(RLIMIT_NOFILE, &rlp);
if (result != 0)
{
return false;
}
#endif // HAVE_RESOURCE_H && !DONT_SET_RLIMIT_NOFILE
return true;
}
20 changes: 20 additions & 0 deletions src/native/minipal/descriptorlimit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#ifndef HAVE_MINIPAL_DESCRIPTORLIMIT_H
#define HAVE_MINIPAL_DESCRIPTORLIMIT_H

#include <stdbool.h>

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

bool minipal_increase_descriptor_limit(void);

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // HAVE_MINIPAL_DESCRIPTORLIMIT_H
1 change: 1 addition & 0 deletions src/native/minipal/minipalconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#cmakedefine01 HAVE_ARC4RANDOM_BUF
#cmakedefine01 HAVE_AUXV_HWCAP_H
#cmakedefine01 HAVE_HWPROBE_H
#cmakedefine01 HAVE_RESOURCE_H
#cmakedefine01 HAVE_O_CLOEXEC
#cmakedefine01 HAVE_SYSCTLBYNAME
#cmakedefine01 HAVE_CLOCK_MONOTONIC
Expand Down
Loading