Learning center

Connection refused or timed out: what each error tells you

A refused connection means a host answered and nothing accepted the port; a timeout means nothing answered at all. Refused points at the dependency's process, listener or port, so look at the target. A timeout points at the path: a security group, firewall rule, network ACL or route that dropped the SYN. Unreachable errors mean something on the way reported that it has no route.

Three errors from one connect call#

Every TCP client opens its connection with connect(), and the error your library logs is usually the errno that call returned, wrapped in the runtime's own words. The three families mean different things:

errnoTextWhat happened to the SYN
ECONNREFUSEDConnection refusedA host answered with a reset
ETIMEDOUTConnection timed outNo answer arrived before the retries ran out
EHOSTUNREACHHost is unreachableAn ICMP error or a failed next hop was reported
ENETUNREACHNetwork is unreachableNo route to the destination network

The connect(2) page defines ECONNREFUSED as finding "no one listening on the remote address", and ETIMEDOUT as a timeout while attempting the connection, adding that the server may be too busy to accept new connections. connect(2) · errno(3)

A reset comes from TCP itself. When a segment arrives for a connection that does not exist, the receiving host sends a reset, so a host that is up with no process bound to the port refuses straight away. Firewalls that reject rather than drop also send one. RFC 9293, reset generation

A timeout is slower. Linux retransmits the initial SYN tcp_syn_retries times, 6 by default, which takes about 127 seconds before connect() gives up. Most clients set a shorter connect timeout of their own, so the log shows the library's timeout message, not the kernel's. tcp(7)

Unreachable errors depend on ICMP. RFC 1122 treats destination unreachable codes 0, 1 and 5 (network, host, source route failed) as soft errors that TCP may report only when the connection times out, and codes 2 to 4 as hard errors that abort it. Linux follows that pattern: after retries fail it reports either ETIMEDOUT or the last error it received, which is how a host-unreachable ICMP turns into EHOSTUNREACH after a long wait. RFC 1122, ICMP messages · tcp(7) error handling

Why a cloud network usually times out#

On AWS, security groups contain allow rules only. Traffic that no rule allows gets no reply, and network ACLs, which are stateless and can deny, behave the same way for denied traffic. The EC2 troubleshooting guide lists security group rules, route tables and network ACLs as the checks for a "Connection timed out" error. Google Cloud's VPC firewall ends with an implied deny ingress rule that applies when no allow rule matches. So in a cloud VPC a blocked port almost always shows up as a timeout, while a refused connection tells you the packet got through to a host. Security group rules · Network ACLs · Connection timed out · Cloud NGFW implied rules

A refusal can still come from something in between. A load balancer, proxy or sidecar that is listening on the address can answer for a backend that is gone, so check which address you actually connected to.

Observe it without changing anything#

Run the test from where the failing client runs: the same container, task or pod, not your laptop. Resolve the name first so you know which address you are testing; a stale DNS answer that points at a decommissioned host looks like either error. getaddrinfo failures

getent hosts orders-db.example.internal
nc -vz -w 5 10.0.12.34 5432

-z only checks for a listener without sending data, -v prints the result and -w 5 limits the wait to five seconds. A refusal returns at once; a drop waits the full five seconds. nc(1)

If you can reach the dependency's host, check that something listens on the port and on which address. A service bound to 127.0.0.1 refuses connections that arrive on the VPC address.

ss -lnt 'sport = :5432'

ss -l lists listening sockets, -n keeps addresses numeric and -t limits output to TCP. ss(8)

For a timeout on AWS, read the rules on both ends and the subnet routes. VPC Flow Logs, if enabled, record REJECT for traffic a security group or network ACL refused. describe-security-groups · Flow log records

aws ec2 describe-security-groups \
  --profile PROFILE --region REGION --group-ids DEPENDENCY_SECURITY_GROUP_ID \
  --query 'SecurityGroups[0].IpPermissions[].{from:FromPort,to:ToPort,groups:UserIdGroupPairs[].GroupId,cidrs:IpRanges[].CidrIp}' \
  --output json --no-cli-pager

On Google Cloud, list the firewall rules that apply to the network and compare targets and source ranges with the client's address and tags. gcloud compute firewall-rules list

gcloud compute firewall-rules list --project PROJECT --filter 'network:NETWORK'

Decision table#

Client seesFirst place to look
Refused, instantlyThe target host: is the process running, bound to this address and port?
Refused, only via a load balancerThe load balancer's listener and its healthy targets
Timed out after the client's connect timeoutSecurity groups, firewall rules, network ACLs, routes
Host or network unreachableRoutes and the next hop: subnet route table, VPN or peering
Connects, then fails laterNot this guide: TLS, authentication or the application

A connect that succeeds but takes seconds is a different symptom. If the dependency's accept queue is full, Linux drops the SYN and the client's retransmission eventually gets in. Listen queue overflow

A worked example with two outcomes#

This example is illustrative. The fictional checkout service logs failures calling orders at orders.example.internal, which resolves to 10.0.12.34, on port 8080.

In the first outcome, nc -vz -w 5 10.0.12.34 8080 from a checkout task prints a refusal immediately. On the orders host, ss -lnt 'sport = :8080' returns nothing: the process restarted after a crash and has not finished starting. The path is fine. The fix belongs to orders.

In the second outcome, the same nc waits five seconds and times out. ss on the orders host shows the listener on 0.0.0.0:8080. The orders security group allows port 8080 from sg-0aa1, and the new checkout tasks run in sg-0bb2. Flow logs on the orders interface show REJECT from the new tasks' addresses. The dependency is healthy; the rule is missing. Only new tasks time out

Limits#

A successful nc shows only that TCP opens. A database at its connection limit, an expired certificate or a bad credential still fail after the handshake. Runtimes rename errors: some print ECONNREFUSED verbatim, others print "connection refused" or their own exception type, so look for the underlying errno in the cause chain. A timeout in your library may also be a read or request timeout, not a connect timeout; check which phase timed out before reading this table. Flow logs are delayed and must have been enabled beforehand. For the overall method of separating your release from the dependency, see the runtime dependency failures hub.

Questions#

Why does a blocked port in AWS time out instead of refusing?

Security groups have no deny rules and send nothing back for traffic they do not allow, so the client keeps retransmitting its SYN until its connect timeout fires. Network ACL denies look the same from the client.

Can a refused connection come from a firewall?

Yes. A firewall configured to reject sends a reset or an ICMP error instead of dropping. Cloud security groups and the default Google Cloud ingress behavior do not, so inside a VPC a refusal usually comes from a host or a proxy.

Why did the error say host unreachable after two minutes?

TCP treats ICMP host unreachable as a soft error and keeps retrying. When the retries run out, Linux reports the last error it received in place of a plain timeout.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading