Skip to content

Commit c57d96f

Browse files
author
Kasper Overgård Nielsen
authored
RDART-1045: Expose setrlimit ios (#1700)
* Expose setrlimit on iOS * Use setrlimit when running test on iOS * Revert "Run iOS test on macos-12" This reverts commit 1e6a379. * Throw and handle exceptions on error for good measure
1 parent e2d9187 commit c57d96f

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ jobs:
302302
differentiator: fi${{ github.run_id }}${{ github.run_attempt }}
303303

304304
flutter-tests-ios:
305-
runs-on: macos-12
305+
runs-on: macos-14
306306
name: Flutter Tests iOS
307307
timeout-minutes: 45
308308
needs:

packages/realm_dart/lib/src/native/realm_bindings.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,6 +3971,33 @@ class RealmLibrary {
39713971
late final _realm_dart_scheduler_invoke = _realm_dart_scheduler_invokePtr
39723972
.asFunction<void Function(int, ffi.Pointer<ffi.Void>)>();
39733973

3974+
/// implemented for iOS only (for now - valid for all posix)
3975+
/// /**
3976+
/// * Set the soft limit on number of open files
3977+
/// * @param limit The requested limit. If less than zero no attempt is made.
3978+
/// * @param[out] out_limit The actual limit set.
3979+
/// *
3980+
/// * @return true if no error occurred.
3981+
/// *
3982+
/// * @throws RLM_ERR_FILE_PERMISSION_DENIED if the operation was not permitted.
3983+
/// */
3984+
bool realm_dart_set_and_get_rlimit(
3985+
int limit,
3986+
ffi.Pointer<ffi.Long> out_limit,
3987+
) {
3988+
return _realm_dart_set_and_get_rlimit(
3989+
limit,
3990+
out_limit,
3991+
);
3992+
}
3993+
3994+
late final _realm_dart_set_and_get_rlimitPtr = _lookup<
3995+
ffi
3996+
.NativeFunction<ffi.Bool Function(ffi.Long, ffi.Pointer<ffi.Long>)>>(
3997+
'realm_dart_set_and_get_rlimit');
3998+
late final _realm_dart_set_and_get_rlimit = _realm_dart_set_and_get_rlimitPtr
3999+
.asFunction<bool Function(int, ffi.Pointer<ffi.Long>)>();
4000+
39744001
bool realm_dart_sync_after_reset_handler_callback(
39754002
ffi.Pointer<ffi.Void> userdata,
39764003
ffi.Pointer<realm_t> before_realm,
@@ -11833,6 +11860,11 @@ class _SymbolAddresses {
1183311860
.NativeFunction<ffi.Void Function(ffi.Uint64, ffi.Pointer<ffi.Void>)>>
1183411861
get realm_dart_scheduler_invoke =>
1183511862
_library._realm_dart_scheduler_invokePtr;
11863+
ffi.Pointer<
11864+
ffi
11865+
.NativeFunction<ffi.Bool Function(ffi.Long, ffi.Pointer<ffi.Long>)>>
11866+
get realm_dart_set_and_get_rlimit =>
11867+
_library._realm_dart_set_and_get_rlimitPtr;
1183611868
ffi.Pointer<
1183711869
ffi.NativeFunction<
1183811870
ffi.Bool Function(

packages/realm_dart/lib/src/native/realm_core.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,12 @@ class RealmCore {
188188
String _getFilesPath() {
189189
return realmLib.realm_dart_get_files_path().cast<Utf8>().toRealmDartString()!;
190190
}
191+
192+
int setAndGetRLimit(int limit) {
193+
return using((arena) {
194+
final outLimit = arena<Long>();
195+
realmLib.realm_dart_set_and_get_rlimit(limit, outLimit).raiseLastErrorIfFalse();
196+
return outLimit.value;
197+
});
198+
}
191199
}

packages/realm_dart/src/ios/platform.mm

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
#import <Foundation/Foundation.h>
2020
#import <UIKit/UIKit.h>
2121

22-
#include <string>
2322
#include "../realm_dart.h"
24-
#import <sys/utsname.h>
23+
#include <string>
24+
#include <sys/utsname.h>
25+
#include <sys/resource.h>
26+
#include <system_error>
27+
#include <realm/object-store/c_api/util.hpp>
2528

2629
static std::string filesDir;
2730
static std::string deviceModel;
@@ -48,7 +51,6 @@
4851
}
4952
}
5053

51-
5254
RLM_API const char* realm_dart_get_files_path() {
5355
if (filesDir == "") {
5456
filesDir = default_realm_file_directory();
@@ -74,3 +76,33 @@
7476

7577
return deviceVersion.c_str();
7678
}
79+
80+
namespace {
81+
using namespace realm::c_api;
82+
83+
rlimit get_rlimit() {
84+
rlimit rlim;
85+
int status = getrlimit(RLIMIT_NOFILE, &rlim);
86+
if (status < 0)
87+
throw std::system_error(errno, std::system_category(), "getrlimit() failed");
88+
return rlim;
89+
}
90+
91+
long set_and_get_rlimit(long limit) {
92+
if (limit > 0) {
93+
auto rlim = get_rlimit();
94+
rlim.rlim_cur = limit;
95+
int status = setrlimit(RLIMIT_NOFILE, &rlim);
96+
if (status < 0)
97+
throw std::system_error(errno, std::system_category(), "setrlimit() failed");
98+
}
99+
return get_rlimit().rlim_cur;
100+
}
101+
}
102+
103+
RLM_API bool realm_dart_set_and_get_rlimit(long limit, long* out_limit) {
104+
return wrap_err([&]() {
105+
*out_limit = set_and_get_rlimit(limit);
106+
return true;
107+
});
108+
}

packages/realm_dart/src/realm_dart.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ RLM_API const char* realm_dart_get_device_version();
3636
// implemented for Android only
3737
RLM_API const char* realm_dart_get_bundle_id();
3838

39+
// implemented for iOS only (for now - valid for all posix)
40+
/**
41+
* Set the soft limit on number of open files
42+
* @param limit The requested limit. If less than zero no attempt is made.
43+
* @param[out] out_limit The actual limit set.
44+
*
45+
* @return true if no error occurred.
46+
*
47+
* @throws RLM_ERR_FILE_PERMISSION_DENIED if the operation was not permitted.
48+
*/
49+
RLM_API bool realm_dart_set_and_get_rlimit(long limit, long* out_limit);
50+
3951
RLM_API const char* realm_get_library_cpu_arch();
4052

4153
typedef struct realm_dart_userdata_async* realm_dart_userdata_async_t;

packages/realm_dart/test/test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ void setupTests() {
430430
printOnFailure('${record.category} ${record.level.name}: ${record.message}');
431431
});
432432

433+
if (Platform.isIOS) {
434+
final maxFiles = realmCore.setAndGetRLimit(1024);
435+
print('Max files: $maxFiles');
436+
}
437+
433438
// Enable this to print platform info, including current PID
434439
_printPlatformInfo();
435440
});

0 commit comments

Comments
 (0)