StackExchange / StackExchange/StackExchange.Redis

Reconnection is attempted continuously regardless of ReconnectRetryPolicy

Open
#2,557 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

⚙️ area:connection 🪲 bug
Dominant language
C#
Stars
6.2k
Forks
1.6k
Avg merge
1d 15h
Merged PRs (30d)
43

Description

I'm creating my connection like this:

_readConn = ConnectionMultiplexer.Connect(
    new ConfigurationOptions
    {
        EndPoints = { { "localhost", 6380 } },
        Password = dbPass,
        Ssl = false,
        AbortOnConnectFail = true,
        ConnectRetry = 3,
        ReconnectRetryPolicy = new MyPolicy(),
        ConnectTimeout = 2000,
        KeepAlive = 5,
    }
);
_readConn.ConnectionFailed += (object? sender, ConnectionFailedEventArgs args) =>
{
    _logger.LogDebug("ConnectionFailed");
};
_readConn.ConnectionRestored += (object? sender, ConnectionFailedEventArgs args) =>
{
    _logger.LogDebug("ConnectionRestored");
};


public class MyPolicy : IReconnectRetryPolicy
{
    public MyPolicy() { }
    public bool ShouldRetry(long currentRetryCount, int timeElapsedMillisecondsSinceLastRetry)
    {
        return false;
    }
}

I then do two different tests:

  • I drop all packets between my .NET microservice and the database using clumsy
  • I kill the database service

in both cases the multiplexer continuously attempts to reconnect even though the policy says not to.

I can verify this by:

  • putting a breakpoint on PhysicalBridge.TryConnect (it's fired approx every second or so)
  • bringing the DB back up / stopping network degradation in clumsy (the connection is immediately restored)

Inspecting the code a bit more, it looks like when the connection is lost, ReadFromPipe will call OnDisconnected, which will then call TryConnect, which, when that connection attempt fails (inside BeginConnectAsync) will once again call OnDisconnected, etc etc.

Shouldn't the policy be applied here and prevent any new connections? Or am I misunderstanding and the reconnect policy is meant for some other case?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the reported path through PhysicalBridge.TryConnect, ReadFromPipe, OnDisconnected, and BeginConnectAsync while reproducing both packet loss and a stopped Redis service. Compare where ReconnectRetryPolicy is consulted with the repeated connection attempts, and use the existing connection-failure behavior as the completion check: returning false should no longer allow continuous retries.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, redis
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.