When setting the fd limit to unlimited, the node fails to start:
0ulimit -n unlimited
1build/bin/bitcoind
2Error: Not enough file descriptors available. -1 available, 160 required.
This was caused by RaiseFileDescriptorLimit() (introduced in #2568) casting limitFD.rlim_cur to int, which for RLIM_INFINITY overflows to -1. Fix it by returning std::numeric_limits<int>::max() instead.
Some platforms implement RLIM_INFINITY as the maximum uint64, others as int64 (-1). So simply changing the return type to uint64_t wouldn’t work.
Similarly, though unlikely to actually happen:
0ulimit -n 214748364
1build/bin/bitcoind
2Error: Not enough file descriptors available. -2147483648 available, 160 required.
The second commit expands the fix by clamping all values above std::numeric_limits<int>::max() instead of letting them overflow.
This PR also expands test/functional/feature_init.py to cover these, using resource.setrlimit. The check is skipped on environments with a hard limit below infinity (or that don’t have the Python Resource module).
macOS by default has a hard limit of infinity, but on e.g. Ubuntu the default hard limit is 524288.
The third commit applies a similar fix to PosixLockedPageAllocator::GetLimit() for 32-bit systems, but without a test.