Skip to content
Open
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
83bdc8a
Fix Config::adjust() overflow for unlimited RLIMIT_NOFILE
EslaM-X Jul 31, 2026
21ddb8e
Address review feedback: Use fs::getMaxHandles and restore connection…
EslaM-X Jul 31, 2026
6857b32
Fix overflow in fs::getMaxHandles and improve connection adjustment
EslaM-X Jul 31, 2026
f9fbc49
Fix typo: vvoid -> void in Config::adjust() definition
EslaM-X Jul 31, 2026
ea6e0cc
Fix Config::adjust() overflow and restore deleted functions
EslaM-X Jul 31, 2026
7dd9e10
Update Config.cpp
EslaM-X Jul 31, 2026
472badf
Update Fs.cpp
EslaM-X Jul 31, 2026
d196aeb
Update Config.cpp
EslaM-X Jul 31, 2026
acafa5f
Refactor getOpenHandleCount for platform-specific limits
EslaM-X Jul 31, 2026
402585f
Update Fs.cpp
EslaM-X Jul 31, 2026
640f18d
Update Fs.cpp
EslaM-X Jul 31, 2026
2d76fb8
Update FsTests.cpp
EslaM-X Jul 31, 2026
dbdb9d5
Update ConfigTests.cpp
EslaM-X Jul 31, 2026
c33b51e
Update Fs.cpp
EslaM-X Jul 31, 2026
d4261c8
Update FsTests.cpp
EslaM-X Jul 31, 2026
1430e9a
Add computeSafeMaxHandles function in Fs.h
EslaM-X Jul 31, 2026
adf0089
Update Fs.cpp
EslaM-X Jul 31, 2026
c212def
Add tests for computeSafeMaxHandles function
EslaM-X Jul 31, 2026
27d0578
Add tests for Config::adjust() descriptor limit handling
EslaM-X Jul 31, 2026
ac03d4e
Update Fs.h
EslaM-X Jul 31, 2026
dbfc7b8
Update ConfigTests.cpp
EslaM-X Jul 31, 2026
4edb5ac
Update FsTests.cpp
EslaM-X Aug 1, 2026
613790a
Refine comments in Config::adjust test case
EslaM-X Aug 1, 2026
0da9c66
Improve largeLimit calculation in FsTests.cpp
EslaM-X Aug 1, 2026
c4e4c80
Update ConfigTests.cpp
EslaM-X Aug 1, 2026
ae8da7a
Update FsTests.cpp
EslaM-X Aug 1, 2026
9157b27
Update ConfigTests.cpp
EslaM-X Aug 1, 2026
5ebe76d
Update ConfigTests.cpp
EslaM-X Aug 1, 2026
79a50c2
Enhance getMaxHandles POSIX test with RLIMIT_NOFILE checks
EslaM-X Aug 3, 2026
b0658ef
Update ConfigTests.cpp
EslaM-X Aug 3, 2026
8310179
Refactor ConfigTests by removing redundant test
EslaM-X Aug 3, 2026
9a7434c
Update ConfigTests.cpp
EslaM-X Aug 3, 2026
22f480e
Remove Config::adjust() handle limit test
EslaM-X Aug 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions src/main/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2214,8 +2214,25 @@ Config::processConfig(std::shared_ptr<cpptoml::table> t)
}

void
Config::adjust()
Config::adjust() void Config::adjust()
{
// Use the platform-abstraction function to get the current limit safely.
long maxFsConnections = fs::getMaxHandles();

// Handle the case where the limit is unlimited (RLIM_INFINITY) to prevent
// overflow.
if (maxFsConnections == RLIM_INFINITY)
{
// Set a practical, safe high boundary.
maxFsConnections = 1000000;
LOG_DEBUG(DEFAULT_LOG,
"RLIMIT_NOFILE is unlimited. Capping connection adjustments "
"to {} for safety.",
maxFsConnections);
}

// --- Rest of the original adjust() logic, using the safe maxFsConnections
// value ---
if (MAX_ADDITIONAL_PEER_CONNECTIONS == -1)
{
if (TARGET_PEER_CONNECTIONS <=
Expand Down Expand Up @@ -2243,24 +2260,23 @@ Config::adjust()
limit, MAX_ADDITIONAL_PEER_CONNECTIONS);
}

// Adjust connection limits based on the safe maxFsConnections
auto const originalMaxAdditionalPeerConnections =
MAX_ADDITIONAL_PEER_CONNECTIONS;
auto const originalTargetPeerConnections = TARGET_PEER_CONNECTIONS;
auto const originalMaxPendingConnections = MAX_PENDING_CONNECTIONS;

int maxFsConnections = std::min<int>(
std::numeric_limits<unsigned short>::max(), fs::getMaxHandles());
int maxFs = std::min<int>(std::numeric_limits<unsigned short>::max(),
maxFsConnections);

auto totalAuthenticatedConnections =
TARGET_PEER_CONNECTIONS + MAX_ADDITIONAL_PEER_CONNECTIONS;

int maxPendingConnections = MAX_PENDING_CONNECTIONS;

if (totalAuthenticatedConnections > 0)
{
auto outboundPendingRate =
double(TARGET_PEER_CONNECTIONS) / totalAuthenticatedConnections;

auto doubleToNonzeroUnsignedShort = [](double v) {
auto rounded = static_cast<int>(std::ceil(v));
auto cappedToUnsignedShort = std::min<int>(
Expand All @@ -2269,51 +2285,41 @@ Config::adjust()
std::max<int>(1, cappedToUnsignedShort));
};

// see if we need to reduce maxPendingConnections
if (totalAuthenticatedConnections + maxPendingConnections >
maxFsConnections)
if (totalAuthenticatedConnections + maxPendingConnections > maxFs)
{
maxPendingConnections =
totalAuthenticatedConnections >= maxFsConnections
totalAuthenticatedConnections >= maxFs
? 1
: static_cast<unsigned short>(
maxFsConnections - totalAuthenticatedConnections);
maxFs - totalAuthenticatedConnections);
}

// if we're still over, we scale everything
if (totalAuthenticatedConnections + maxPendingConnections >
maxFsConnections)
if (totalAuthenticatedConnections + maxPendingConnections > maxFs)
{
maxPendingConnections = std::max<int>(MAX_PENDING_CONNECTIONS, 1);

int totalRequiredConnections =
totalAuthenticatedConnections + maxPendingConnections;

auto outboundRate =
(double)TARGET_PEER_CONNECTIONS / totalRequiredConnections;
auto inboundRate = (double)MAX_ADDITIONAL_PEER_CONNECTIONS /
totalRequiredConnections;

TARGET_PEER_CONNECTIONS =
doubleToNonzeroUnsignedShort(maxFsConnections * outboundRate);
doubleToNonzeroUnsignedShort(maxFs * outboundRate);
MAX_ADDITIONAL_PEER_CONNECTIONS =
doubleToNonzeroUnsignedShort(maxFsConnections * inboundRate);
doubleToNonzeroUnsignedShort(maxFs * inboundRate);

auto authenticatedConnections =
TARGET_PEER_CONNECTIONS + MAX_ADDITIONAL_PEER_CONNECTIONS;
maxPendingConnections =
authenticatedConnections >= maxFsConnections
? 1
: static_cast<unsigned short>(maxFsConnections -
authenticatedConnections);
maxPendingConnections = authenticatedConnections >= maxFs
? 1
: static_cast<unsigned short>(
maxFs - authenticatedConnections);
}

MAX_PENDING_CONNECTIONS = static_cast<unsigned short>(std::min<int>(
std::numeric_limits<unsigned short>::max(), maxPendingConnections));

// derive outbound/inbound pending connections
// from MAX_PENDING_CONNECTIONS, using the ratio of inbound/outbound
// connections
if (MAX_OUTBOUND_PENDING_CONNECTIONS == 0 &&
MAX_INBOUND_PENDING_CONNECTIONS == 0)
{
Expand All @@ -2329,6 +2335,7 @@ Config::adjust()
MAX_OUTBOUND_PENDING_CONNECTIONS = 0;
MAX_INBOUND_PENDING_CONNECTIONS = 0;
}

auto warnIfChanged = [&](std::string const name, auto const originalValue,
auto const newValue) {
if (originalValue != newValue)
Expand Down