Repository navigation
Description
Summary
SubscriberConnector makes one more connection attempt than MaxRetries allows. With MaxRetries = 1 and auto-reconnect disabled, an unreachable publisher is tried twice, so a connection failure takes twice the TCP connect timeout to report (about 42 s instead of 21 s on Windows).
Cause
SubscriberConnector::Connect increments m_connectAttempt and then calls DataSubscriber::Connect(hostname, port, autoReconnecting). On the initial user-requested connection autoReconnecting is false, so DataSubscriber::Connect resets the connector:
// src/lib/transport/DataSubscriber.cpp (origin/main, ~line 1111)
if (!autoReconnecting)
m_connector.ResetConnection(); // sets m_connectAttempt = 0The sequence with MaxRetries = 1:
m_connectAttempt++sets it to 1, thenDataSubscriber::Connect(..., false)resets it to 0.- The connect fails. The message has no attempt number (
Connection to "host:port" failed: ...), and the retry interval is 0 becausem_connectAttemptis 0. - The loop checks
0 >= 1, which is false, so it tries again:m_connectAttempt++sets it to 1 andDataSubscriber::Connect(..., true)runs without a reset. - The second attempt fails (
Connection attempt 1 to ...), thenMaximum connection retries attempted, thenAll connection attempts failed.
The same reset makes the first retry skip the back-off delay, and every sequence reports one extra attempt.
Reproduce
subscriber.SetAutoReconnect(false);
subscriber.SetMaxRetries(1);
subscriber.SetRetryInterval(0);
subscriber.Initialize("192.0.2.50", 7165); // non-routable TEST-NET-1 address
subscriber.ConnectAsync();Two TCP connection attempts are made. All connection attempts failed arrives after about 42 s on Windows instead of about 21 s. A refused port (for example 127.0.0.1:<closed port>) also makes two attempts, but they fail immediately.
Suggested fix
Reset the attempt count once, at the start of a user-initiated connection sequence, instead of inside each DataSubscriber::Connect call. For example, SubscriberInstance::Connect already calls connector.ResetConnection() before connector.Connect(...). Alternatively, DataSubscriber::Connect could skip the reset when it is called from the connector loop. MaxRetries = N should then produce exactly N attempts.
Context
Found while building an STTP connectivity test with the .NET wrapper (sttp/net-cppapi). The test currently works around this by treating the first Connection ... failed error as the failure and ignoring the extra attempt. Observed on submodule 07c8327; the reset is unchanged on origin/main (2ec7d8e, v1.1.4).
Activity