{"record":{"id":"8e29cd60aaf8f537","repo":"microsoft/garnet","slug":"failed-to-connect-at-endpoint","errorCode":null,"errorMessage":"Failed to connect at {EndPoint}","messagePattern":"Failed to connect at (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"libs/client/ClientSession/GarnetClientSession.cs","lineNumber":286,"sourceCode":"                        NoDelay = true\n                    };\n\n                    if (TryConnectSocket(socket, endpoint))\n                        return socket;\n                }\n            }\n            else\n            {\n                var socket = new Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);\n                if (EndPoint is not UnixDomainSocketEndPoint)\n                    socket.NoDelay = true;\n\n                if (TryConnectSocket(socket, EndPoint))\n                    return socket;\n            }\n\n            logger?.LogWarning(\"Failed to connect at {endpoint}\", EndPoint);\n            throw new Exception($\"Failed to connect at {EndPoint}\");\n        }\n\n        /// <inheritdoc cref=\"ConnectSendSocket()\"/>\n        private async Task<Socket> ConnectSendSocketAsync(int millisecondsTimeout = 0, CancellationToken cancellationToken = default)\n        {\n            if (EndPoint is DnsEndPoint dnsEndpoint)\n            {\n                var hostEntries = await Dns.GetHostEntryAsync(dnsEndpoint.Host, cancellationToken).ConfigureAwait(false);\n                // Try all available DNS entries if a hostName is provided\n                foreach (var addressEntry in hostEntries.AddressList)\n                {\n                    var endpoint = new IPEndPoint(addressEntry, dnsEndpoint.Port);\n                    var socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)\n                    {\n                        NoDelay = true\n                    };\n\n                    if (await TryConnectSocketAsync(socket, endpoint, millisecondsTimeout, cancellationToken).ConfigureAwait(false))","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/client/ClientSession/GarnetClientSession.cs#L268-L304","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the Garnet server is running and listening on the configured address/port (e.g. netstat/ss, telnet to the port).","Confirm the EndPoint passed to GarnetClientSession matches the server's bound address, and that the client host can route to it.","If using a DnsEndPoint, check DNS resolution returns reachable addresses and that none are IPv6-only when the host lacks IPv6.","Retry with backoff or use ReconnectAsync for transient network blips; inspect the logged warning for the exact endpoint that failed."],"exampleFix":"// before\nvar session = new GarnetClientSession(endpoint, ...);\nsession.Connect();\n\n// after — preflight reachability + retry\nif (!await IsReachableAsync(endpoint)) throw new InvalidOperationException($\"unreachable {endpoint}\");\nfor (int i = 0; i < 3; i++)\n    try { session.Connect(); break; }\n    catch (Exception ex) when (i < 2) { await Task.Delay(1 << i); }","handlingStrategy":"retry","validationCode":"// Preflight TCP reachability before constructing the session\nusing var probe = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);\nvar connected = probe.BeginConnect(endpoint, null, null).AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2));","typeGuard":"// Ensure endpoint is well-formed and reachable before connecting\nif (endpoint is null) throw new ArgumentNullException(nameof(endpoint));","tryCatchPattern":"try { session.Connect(); }\ncatch (Exception ex) { logger?.LogError(ex, \"connect failed to {Ep}\", endpoint); /* retry or fail over */ }","preventionTips":["Verify the server is listening before client startup.","Confirm address/port and network reachability (firewall, routing, DNS).","Retry with exponential backoff for transient failures during cluster bring-up."],"tags":["csharp","network","connection","socket","garnet-client-session"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}