Diagnosing network failures: curl, dig and error classes
A network failure is narrowed to a layer by following the request order: DNS, TCP, TLS, HTTP, application, stopping at the first that fails. ENOTFOUND points to DNS, ECONNREFUSED to a port nobody listens on, a connection timeout to dropped packets; 502 and 504 come from a proxy or load balancer. curl -w values are cumulative, so a stage's duration is the difference of adjacent values. A retry hides the cause.
Networking · Testing
A CI test fails with "network error" once in ten runs. Where do you start: the test code or the network?
The message "network error" says nothing: it can be DNS, a port, a certificate, a proxy or the application itself. Someone who can quickly narrow a failure to a layer does not guess and does not cure with a retry what a retry only hides. This lesson gives a diagnosis order, a table of symptoms and a set of commands to check them.
How it works and why. A request goes through a sequence of steps and each can fail on its own: a name becomes an IP, a TCP connection is made, TLS is negotiated, HTTP flows, the application answers. A failure at an early step makes later ones unreachable, so you go top down and stop at the first layer that does not work.
Diagnosis by layer
| Symptom | Layer and typical cause |
|---|---|
ENOTFOUND, ERR_NAME_NOT_RESOLVED | DNS: a typo in the name, no record, a resolver or VPN |
ECONNREFUSED | TCP: nobody listens on the port (service not started, wrong port, listens on 127.0.0.1 only) |
Timeout with no answer (ETIMEDOUT) | TCP/network: a firewall drops packets, the host is unreachable |
ECONNRESET, socket hang up | The other side cut the connection: a process crash, idle timeout, overload |
ERR_CERT_*, UNABLE_TO_VERIFY_LEAF_SIGNATURE | TLS: an expired, self-signed or incomplete chain, a name mismatch |
| 502 Bad Gateway | The proxy got an invalid response or could not connect to the upstream |
| 503 Service Unavailable | The service is unavailable or overloaded (no healthy backends) |
| 504 Gateway Timeout | The proxy did not get an upstream answer within its timeout |
Important: 502 and 504 come from a proxy or balancer (503 often too, but the application itself can return it), so the connection to it works and the problem is behind it. The response body (nginx or CDN HTML instead of JSON) and the Server and Via headers show who returned it.
Tools.
curl -v https://example.com/health # DNS, TLS, request and response headers
curl -i https://example.com/health # status and response headers (Server, Via)
curl --resolve example.com:443:203.0.113.10 https://example.com
# bypass DNS, force an IP
dig +short example.com A # what DNS says
dig @1.1.1.1 example.com # another resolver
nc -vz example.com 443 # is the port open
ping example.com # ICMP may be disabled
traceroute example.com # on Windows: tracert
# Windows PowerShell: is the port open
Test-NetConnection -ComputerName example.com -Port 443
Example: where exactly the time goes. The -w variables in curl are cumulative: each counts from the start of the request, not from the previous stage. So tcp includes DNS and tls includes TCP. The duration of a single stage is the difference between neighbouring values.
curl -sS -o /dev/null -w \
"dns=%{time_namelookup} tcp=%{time_connect} tls=%{time_appconnect} ttfb=%{time_starttransfer} total=%{time_total}\n" \
https://example.com/health
# illustrative output, values are cumulative (from the start of the request)
# dns=0.012 tcp=0.045 tls=0.130 ttfb=1.210 total=1.260
From this output: DNS 12 ms; TCP connect 45 − 12 = 33 ms; TLS 130 − 45 = 85 ms; backend wait (ttfb − tls) 1210 − 130 = 1080 ms; body download (total − ttfb) 50 ms. The slow part here is the backend, not the network path: no point spending time on DNS. A large dns would point at the resolver, a large total − ttfb at a long body download.
Approach to a flaky network. First look for a pattern: time of day, a particular runner or region, the first request after idle (a cold connection, DNS), exactly N seconds (a match with a timeout on some node). Then add to the test log the URL, time, status, x-request-id and a trace or HAR so the next failure is explainable. A retry does not cure the cause but hides it, so it is enabled deliberately and only after diagnosis. In CI keep explicit client timeouts, keep-alive and a connection pool.
Limits and alternatives. ping shows only ICMP, which is often blocked, so no reply proves nothing. nslookup and dig query a resolver directly, while the application may use its own cache or system settings, so the result can differ from what the test sees. From a container or a CI runner the network may differ from a laptop: run diagnosis where the test fails.
Typical mistakes.
- Reading
time_connectas the TCP duration without subtracting DNS. - Adding a retry instead of finding the cause.
- Taking 502 and 504 for an application error rather than a proxy or upstream one.
- Diagnosing from a laptop when only the CI runner fails.
- Not logging the request id and later failing to find the request in server logs.
Check yourself
curl -w shows tcp=0.045 tls=0.130. How long did the TLS negotiation take?
Answer
0.130 − 0.045 = 0.085 s, that is 85 ms. curl variables are cumulative, so a stage’s duration is the difference between neighbouring values.
The API answers 504 with an nginx HTML page instead of JSON. Where do you look?
Answer
The proxy returned it, so the connection to it works and the upstream did not answer within the proxy timeout. Look at backend response time, the proxy timeout and the upstream logs.
How does ECONNREFUSED differ from a timeout with no answer?
Answer
ECONNREFUSED comes quickly: the host answered that nobody listens on the port. A timeout means no answer at all: a firewall drops packets or the host is unreachable.