microsoft/garnet · error · Exception

Failed to connect at {EndPoint}

Error message

Failed to connect at {EndPoint}

What it means

ConnectSendSocket (sync) in GarnetClientSession throws after every candidate endpoint has been tried and none succeeded. For a DnsEndPoint it iterates all resolved addresses and tries TryConnectSocket on each; for any other EndPoint (IPEndPoint or UnixDomainSocketEndPoint) it makes a single attempt. TryConnectSocket swallows connect failures and returns false, so this throw is the terminal 'all retries exhausted' signal. A warning is logged at the {endpoint} template before throwing.

Source

Thrown at libs/client/ClientSession/GarnetClientSession.cs:286

                        NoDelay = true
                    };

                    if (TryConnectSocket(socket, endpoint))
                        return socket;
                }
            }
            else
            {
                var socket = new Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
                if (EndPoint is not UnixDomainSocketEndPoint)
                    socket.NoDelay = true;

                if (TryConnectSocket(socket, EndPoint))
                    return socket;
            }

            logger?.LogWarning("Failed to connect at {endpoint}", EndPoint);
            throw new Exception($"Failed to connect at {EndPoint}");
        }

        /// <inheritdoc cref="ConnectSendSocket()"/>
        private async Task<Socket> ConnectSendSocketAsync(int millisecondsTimeout = 0, CancellationToken cancellationToken = default)
        {
            if (EndPoint is DnsEndPoint dnsEndpoint)
            {
                var hostEntries = await Dns.GetHostEntryAsync(dnsEndpoint.Host, cancellationToken).ConfigureAwait(false);
                // Try all available DNS entries if a hostName is provided
                foreach (var addressEntry in hostEntries.AddressList)
                {
                    var endpoint = new IPEndPoint(addressEntry, dnsEndpoint.Port);
                    var socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
                    {
                        NoDelay = true
                    };

                    if (await TryConnectSocketAsync(socket, endpoint, millisecondsTimeout, cancellationToken).ConfigureAwait(false))

View on GitHub (pinned to 951b0fc683)

Solutions

  1. Verify the Garnet server is running and listening on the configured address/port (e.g. netstat/ss, telnet to the port).
  2. Confirm the EndPoint passed to GarnetClientSession matches the server's bound address, and that the client host can route to it.
  3. If using a DnsEndPoint, check DNS resolution returns reachable addresses and that none are IPv6-only when the host lacks IPv6.
  4. Retry with backoff or use ReconnectAsync for transient network blips; inspect the logged warning for the exact endpoint that failed.

Example fix

// before
var session = new GarnetClientSession(endpoint, ...);
session.Connect();

// after — preflight reachability + retry
if (!await IsReachableAsync(endpoint)) throw new InvalidOperationException($"unreachable {endpoint}");
for (int i = 0; i < 3; i++)
    try { session.Connect(); break; }
    catch (Exception ex) when (i < 2) { await Task.Delay(1 << i); }
Defensive patterns

Strategy: retry

Validate before calling

// Preflight TCP reachability before constructing the session
using var probe = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
var connected = probe.BeginConnect(endpoint, null, null).AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2));

Type guard

// Ensure endpoint is well-formed and reachable before connecting
if (endpoint is null) throw new ArgumentNullException(nameof(endpoint));

Try / catch

try { session.Connect(); }
catch (Exception ex) { logger?.LogError(ex, "connect failed to {Ep}", endpoint); /* retry or fail over */ }

Prevention

When it happens

Trigger: GarnetClientSession.Connect/Reconnect to a server that is down, refuses connections, is firewalled, or whose hostname resolves to addresses that all reject the TCP handshake; or connecting to a Unix domain socket path that does not exist/is not listening.

Common situations: Server process not started or crashed; wrong address/port in options; security group or firewall blocking the port; DNS returns stale entries; connecting from a client whose network namespace cannot reach the endpoint; localhost vs 127.0.0.1 vs container hostname mismatch.

Related errors


AI-assisted analysis of microsoft/garnet@951b0fc683 (2026-08-13). Data as JSON: /api/errors/8e29cd60aaf8f537. Report an issue: GitHub.