microsoft/FASTER · error · Exception

Failed to connect server.

Error message

Failed to connect server.

What it means

The ClientSession constructor attempted an asynchronous TCP connect to the FASTER server with a timeout; when the wait completed, the socket was still not in the Connected state. The library closes the socket and throws, meaning the server was unreachable or refused/did not complete the handshake within the timeout.

Solutions

  1. Verify the server endpoint (host/IP and port) matches the actual listening address of the FASTER server.
  2. Ensure the server is up and listening before constructing the client (add startup/health-check ordering).
  3. Increase the connect timeout argument, and retry connection with backoff.
  4. Check network paths: firewalls, security groups, Docker/Kubernetes network policy, and DNS resolution.

Example fix

// before
var session = new ClientSession<...>(endPoint, ...);

// after: retry with backoff
ClientSession<...> session = null;
for (var attempt = 0; attempt < 5; attempt++)
{
    try { session = new ClientSession<...>(endPoint, ..., timeoutMs: 10000); break; }
    catch (Exception) when (attempt < 4) { Thread.Sleep(1000 * (attempt + 1)); }
}
Defensive patterns

Strategy: retry

Validate before calling

// probe TCP reachability before constructing the session
using (var probe = new TcpClient())
{
    var ok = probe.ConnectAsync(host, port).Wait(5000);
    if (!ok) throw new InvalidOperationException($"FASTER server {host}:{port} unreachable");
}

Try / catch

ClientSession<...> session = null;
for (var attempt = 0; attempt < 5 && session == null; attempt++)
{
    try { session = new ClientSession<...>(endPoint, ..., 10000); }
    catch (Exception ex) when (ex.Message == "Failed to connect server.") { Thread.Sleep(2000 * (attempt + 1)); }
}

Prevention

When it happens

Trigger: Calling new ClientSession(...) with a millisecondsTimeout where the server is down, listening on a different host/port, blocked by a firewall, or too slow to accept within the timeout.

Common situations: Wrong endpoint/IP or port in configuration; server process not started yet (startup ordering in docker-compose/k8s); firewall or security-group blocking the port; container networking/DNS misconfiguration; overloaded server exceeding the connect timeout.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of microsoft/FASTER@321d872eab (2026-09-15). Data as JSON: /api/errors/f2461babcf12832c. Report an issue: GitHub.

Appendix: source

Thrown at cs/remote/src/FASTER.client/ClientSession.cs:684

        private Socket GetSendSocket(string address, int port, int millisecondsTimeout = -2)
        {
            var ip = IPAddress.Parse(address);
            var endPoint = new IPEndPoint(ip, port);
            var socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                NoDelay = true
            };

            if (millisecondsTimeout != -2)
            {
                IAsyncResult result = socket.BeginConnect(endPoint, null, null);
                result.AsyncWaitHandle.WaitOne(millisecondsTimeout, true);
                if (socket.Connected)
                    socket.EndConnect(result);
                else
                {
                    socket.Close();
                    throw new Exception("Failed to connect server.");
                }
            }
            else
            {
                socket.Connect(endPoint);
            }

            // Ok to create new event args on accept because we assume a connection to be long-running
            var receiveEventArgs = new SocketAsyncEventArgs();
            var bufferSize = BufferSizeUtils.ServerBufferSize(maxSizeSettings);
            receiveEventArgs.SetBuffer(new byte[bufferSize], 0, bufferSize);
            receiveEventArgs.UserToken =
                new ClientNetworkSession<Key, Value, Input, Output, Context, Functions, ParameterSerializer>(socket, this);
            receiveEventArgs.Completed += RecvEventArg_Completed;
            var response = socket.ReceiveAsync(receiveEventArgs);
            Debug.Assert(response);
            return socket;
        }

View on GitHub (pinned to 321d872eab)