From ec0272fa117b450020fea576fa794fd4a647b706 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Tue, 7 Mar 2023 10:19:18 +0100 Subject: [PATCH] [runtime/6.0] [mono] Raise soft RLIMIT_NOFILE on Linux Manual backport of https://github.com/dotnet/runtime/pull/82429. Replace darwin_change_default_file_handles by a new routine increase_descriptor_limit, which mirrors the logic in the CoreCLR INIT_IncreaseDescriptorLimit routine. Fixes https://github.com/dotnet/runtime/issues/82428. --- src/mono/mono/mini/driver.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/mono/mono/mini/driver.c b/src/mono/mono/mini/driver.c index ce42a3ed0cf946..64d8cc0b3e9180 100644 --- a/src/mono/mono/mini/driver.c +++ b/src/mono/mono/mini/driver.c @@ -69,7 +69,7 @@ #include #include #include -#if TARGET_OSX +#ifdef HAVE_SYS_RESOURCE_H # include #endif @@ -1940,24 +1940,29 @@ switch_gc (char* argv[], const char* target_gc) #endif } -#ifdef TARGET_OSX - -/* - * tries to increase the minimum number of files, if the number is below 1024 - */ static void -darwin_change_default_file_handles () +increase_descriptor_limit (void) { +#if defined(HAVE_GETRLIMIT) && !defined(DONT_SET_RLIMIT_NOFILE) struct rlimit limit; - if (getrlimit (RLIMIT_NOFILE, &limit) == 0){ - if (limit.rlim_cur < 1024){ - limit.rlim_cur = MAX(1024,limit.rlim_cur); - setrlimit (RLIMIT_NOFILE, &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 } +#ifdef TARGET_OSX + static void switch_arch (char* argv[], const char* target_arch) { @@ -2113,9 +2118,7 @@ mono_main (int argc, char* argv[]) setlocale (LC_ALL, ""); -#if TARGET_OSX - darwin_change_default_file_handles (); -#endif + increase_descriptor_limit (); if (g_hasenv ("MONO_NO_SMP")) mono_set_use_smp (FALSE);