Skip to content

Commit c9a6150

Browse files
committed
Merge bitcoin/bitcoin#31979: torcontrol: Limit reconnect timeout to max seconds and log delay in whole seconds
f708498 torcontrol: Limit reconnect timeout to max seconds and log delay in whole seconds (Eval EXEC) Pull request description: I'm reviewing the Tor controller's reconnect-related code and noticed that the reconnect timeout had no limit. This could lead to excessively long delays. This PR introduces a maximum reconnect timeout of 600 seconds (10 minutes) to prevent excessive delays in reconnection attempts. It also updates the log message to display the retry delay in whole seconds for better readability. ACKs for top commit: mabu44: ACK f708498 laanwj: Code review ACK f708498 luke-jr: utACK f708498 Tree-SHA512: 8f18c6c84da6b4e7328638fd74539fbd3dd44f46c5107638de56b72fc079487690861199ceba1197ca34421dcedf79a1ca6bacf2a918a683e71bce9ff710b5d4
2 parents b43cfa2 + f708498 commit c9a6150

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/torcontrol.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <algorithm>
2828
#include <cassert>
29+
#include <cstdint>
2930
#include <cstdlib>
3031
#include <deque>
3132
#include <functional>
@@ -60,6 +61,8 @@ static const std::string TOR_SAFE_CLIENTKEY = "Tor safe cookie authentication co
6061
static const float RECONNECT_TIMEOUT_START = 1.0;
6162
/** Exponential backoff configuration - growth factor */
6263
static const float RECONNECT_TIMEOUT_EXP = 1.5;
64+
/** Maximum reconnect timeout in seconds to prevent excessive delays */
65+
static const float RECONNECT_TIMEOUT_MAX = 600.0;
6366
/** Maximum length for lines received on TorControlConnection.
6467
* tor-control-spec.txt mentions that there is explicitly no limit defined to line length,
6568
* this is belt-and-suspenders sanity limit to prevent memory exhaustion.
@@ -631,13 +634,15 @@ void TorController::disconnected_cb(TorControlConnection& _conn)
631634
if (!reconnect)
632635
return;
633636

634-
LogDebug(BCLog::TOR, "Not connected to Tor control port %s, trying to reconnect\n", m_tor_control_center);
637+
LogDebug(BCLog::TOR, "Not connected to Tor control port %s, retrying in %.2f s\n",
638+
m_tor_control_center, reconnect_timeout);
635639

636-
// Single-shot timer for reconnect. Use exponential backoff.
640+
// Single-shot timer for reconnect. Use exponential backoff with a maximum.
637641
struct timeval time = MillisToTimeval(int64_t(reconnect_timeout * 1000.0));
638642
if (reconnect_ev)
639643
event_add(reconnect_ev, &time);
640-
reconnect_timeout *= RECONNECT_TIMEOUT_EXP;
644+
645+
reconnect_timeout = std::min(reconnect_timeout * RECONNECT_TIMEOUT_EXP, RECONNECT_TIMEOUT_MAX);
641646
}
642647

643648
void TorController::Reconnect()

0 commit comments

Comments
 (0)