Skip to content

Commit 29008a7

Browse files
committed
init: fixes fd accounting regarding poll/select
We are computing our file descriptors limits based on whether we use poll or select. However, we are taking that into account only partially (subtracting from fd_max in one case, but from nFD later on). Moreover, nBind is also only accounted for partially. Simplify and fix this logic
1 parent d661e2b commit 29008a7

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/init.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -993,18 +993,16 @@ bool AppInitParameterInteraction(const ArgsManager& args)
993993
nMaxConnections = std::max(nUserMaxConnections, 0);
994994

995995
nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS + MAX_ADDNODE_CONNECTIONS + nBind + NUM_FDS_MESSAGE_CAPTURE);
996-
997-
#ifdef USE_POLL
998-
int fd_max = nFD;
999-
#else
1000-
int fd_max = FD_SETSIZE;
996+
// If we are using select instead of poll, our actual limit may be even smaller
997+
#ifndef USE_POLL
998+
nFD = std::min(FD_SETSIZE, nFD);
1001999
#endif
1002-
// Trim requested connection counts, to fit into system limitations
1003-
// <int> in std::min<int>(...) to work around FreeBSD compilation issue described in #2695
1004-
nMaxConnections = std::max(std::min<int>(nMaxConnections, fd_max - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS - NUM_FDS_MESSAGE_CAPTURE), 0);
10051000
if (nFD < MIN_CORE_FILEDESCRIPTORS)
10061001
return InitError(_("Not enough file descriptors available."));
1007-
nMaxConnections = std::min(nFD - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS - NUM_FDS_MESSAGE_CAPTURE, nMaxConnections);
1002+
1003+
// Trim requested connection counts, to fit into system limitations
1004+
// <int> in std::min<int>(...) to work around FreeBSD compilation issue described in #2695
1005+
nMaxConnections = std::max(std::min<int>(nMaxConnections, nFD - nBind - MIN_CORE_FILEDESCRIPTORS - MAX_ADDNODE_CONNECTIONS - NUM_FDS_MESSAGE_CAPTURE), 0);
10081006

10091007
if (nMaxConnections < nUserMaxConnections)
10101008
InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), nUserMaxConnections, nMaxConnections));

0 commit comments

Comments
 (0)