Bug 1: If the daemon does not have enough file descriptors (we’ll call them FDs), it is not asserted.
The daemon only aborts if FD count < MIN_CORE_FILEDESCRIPTORS(150)
.
Bug 2: (Unsure how to reproduce undefined behavior) If the system ulimit setting is a low number, the daemon does not allocate the right amount of FDs during init via RaiseFileDescriptorLimit
. But it’s close.
Steps to produce Bug 1: Run ulimit -n 150
, then start bitcoind
in the same shell.
Result: bitcoind runs normally and displays an erroneous warning:
0Warning: Reducing -maxconnections from 125 to -8, because of system limitations.
1...
2Using at most -8 automatic connections (150 file descriptors available)
Expected: bitcoind should not start, as it needs roughly 163~172~(?) FDs.
-MIN_CORE_FILEDESCRIPTORS = 150
-MAX_ADDNODE_CONNECTIONS = 8
(new in assertion)
~-MAX_OUTBOUND_CONNECTIONS = 8
~
~-CConnman::Options:nMaxFeeler = 1
~(Edit: these exist within the bounds of -maxconnections, if it’s high enough)
-Number of -bind
interfaces, default = 1 (new in allocation)
-Number of -rpcthreads
, default = 4 (new in allocation and assertion)
150 + 8 + 1 + 4 = 163 minimum +125 maxconnections = 288 speculative maximum
Coverage with this patch + ulimit -n 150
:
0Bitcoin Core version v0.18.99.0-50ccaa56f -dirty (release build)
1Error: Not enough file descriptors available. 150 available, 163 required.
daemon closes
Coverage with this patch + ulimit -n 163
:
0Bitcoin Core version v0.18.99.0-50ccaa56f (release build)
1There are 163 file descriptors available, 163 required, 163 reserved, and 288 requested.
2Warning: Reducing -maxconnections from 125 to 0, because of file descriptor limitations.
3...
4Using at most 0 automatic connections (163 file descriptors available)
Coverage with this patch + ulimit -n 203
:
0Bitcoin Core version v0.18.99.0-50ccaa56f (release build)
1There are 203 file descriptors available, 163 required, 203 reserved, and 288 requested.
2Warning: Reducing -maxconnections from 125 to 40, because of file descriptor limitations.
3...
4Using at most 40 automatic connections (203 file descriptors available)
Coverage with this patch + 1024 ulimit (matches most Linux systems)
0Bitcoin Core version v0.18.99.0-50ccaa56f (release build)
1There are 1024 file descriptors available, 163 required, 288 reserved, and 288 requested.
2...
3Using at most 125 automatic connections (1024 file descriptors available)
The replaced (old) arithmetic smells strange if you consider the subtraction can subtract negative values to increase their value, and any unintended behavior will then be silenced by std::max(x,0). Now there should be less likelyhood of suppressed failure.
Possible fix for #14870
Edit: thanks to IRC chat for the tips!