Skip to content

MaxRetries allows an extra connection attempt: DataSubscriber::Connect resets connector attempt count #7

Open

Description

@ritchiecarroll

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 = 0

The sequence with MaxRetries = 1:

  1. m_connectAttempt++ sets it to 1, then DataSubscriber::Connect(..., false) resets it to 0.
  2. The connect fails. The message has no attempt number (Connection to "host:port" failed: ...), and the retry interval is 0 because m_connectAttempt is 0.
  3. The loop checks 0 >= 1, which is false, so it tries again: m_connectAttempt++ sets it to 1 and DataSubscriber::Connect(..., true) runs without a reset.
  4. The second attempt fails (Connection attempt 1 to ...), then Maximum connection retries attempted, then All 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions