Skip to content

Commit 91b7198

Browse files
committed
init: clamp fd limits to int
When setting the fd limit to 1 >> 31, the node fails to start: ulimit -n 214748364 build/bin/bitcoind Error: Not enough file descriptors available. -2147483648 available, 160 required. Similar to the previous commit, this is fixed by capping the limit to std::numeric_limits<int>::max().
1 parent 3cd0cbd commit 91b7198

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/util/fs_helpers.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,12 @@ int RaiseFileDescriptorLimit(int min_fd)
158158
#else
159159
struct rlimit limitFD;
160160
if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
161-
if (limitFD.rlim_cur == RLIM_INFINITY) {
161+
if (limitFD.rlim_cur == RLIM_INFINITY ||
162+
limitFD.rlim_cur >= static_cast<rlim_t>(std::numeric_limits<int>::max())) {
162163
// Some platforms implement RLIM_INFINITY as the maximum uint64,
163164
// others as int64 (-1). Avoid casting even if the return type
164-
// is changed to uint64_t.
165+
// is changed to uint64_t. We also cap unlikely but possible values
166+
// that would overflow int.
165167
return std::numeric_limits<int>::max();
166168
}
167169
if (limitFD.rlim_cur < static_cast<rlim_t>(min_fd)) {

test/functional/feature_init.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,23 @@ def init_rlimit_test(self):
348348
self.log.info("Testing node startup with RLIM_INFINITY fd limit")
349349
self.restart_node_with_fd_limit(self.RLIM_INFINITY)
350350

351+
def init_rlimit_large_test(self):
352+
"""Test that bitcoind starts correctly when the soft RLIMIT_NOFILE limit is above INT_MAX."""
353+
if self.RLIM_INFINITY is None:
354+
self.log.info("Skipping: resource module not available")
355+
return
356+
357+
self.log.info("Testing node startup with fd limit above INT_MAX")
358+
self.restart_node_with_fd_limit(1 << 31)
359+
351360
def run_test(self):
352361
self.init_pid_test()
353362
self.init_stress_test_interrupt()
354363
self.init_stress_test_removals()
355364
self.break_wait_test()
356365
self.init_empty_test()
357366
self.init_rlimit_test()
367+
self.init_rlimit_large_test()
358368

359369

360370
if __name__ == '__main__':

0 commit comments

Comments
 (0)