Learning center

Occasional socket hang up on reused HTTP keep-alive connections

Rare resets on reused HTTP connections usually mean the server's keep-alive timeout is shorter than the time your client keeps idle connections. The server closes an idle connection at the same moment the client picks it from the pool and sends a request on it. The fix is to make the client give up idle connections before the server does, which you can confirm by comparing the two timeouts and watching which side closes.

The symptom#

A small fraction of calls between two healthy services fail with a reset, a "socket hang up" in Node.js, a "server closed idle connection" error in Go, or a 502 from a load balancer. The rate rises when traffic is light enough that connections sit idle for a few seconds, and drops at peak. Retries almost always succeed. Nothing in the server's logs matches the failing requests, because the server never read them.

Node.js documents the exact race: when a keep-alive agent reuses a socket and the server closes the connection at the same time, the request may fail with ECONNRESET. When the connection closes before a response arrives, the request emits an error with the message socket hang up and code ECONNRESET. Go's transport has a dedicated error, http: server closed idle connection; its source notes that users do not see it for idempotent requests (the transport retries them) but may see it when the server's FIN crosses a request body already written. Node.js request.reusedSocket · Node.js http.request events · Go transport source

Why it happens#

Each side has its own idle timer. The server closes a connection that has been idle for its keep-alive timeout. The client returns a connection to its pool after each response and reuses it until its own idle timeout. If the client's limit is longer than the server's, there is a window where the client believes a connection is usable and the server is closing it. A request written into that window meets the server's FIN, or a reset if the server has already closed the socket.

HTTP expects this. RFC 9112 says connections can be closed at any time and implementations ought to anticipate asynchronous close events; RFC 9110 says a client should not automatically retry a non-idempotent request, which is why a client library may retry a GET silently and surface a failed POST. RFC 9112, retrying requests · RFC 9110, idempotent methods

Defaults make the mismatch common:

ComponentIdle default
Node.js server.keepAliveTimeout5000 ms, plus keepAliveTimeoutBuffer of 1000 ms on recent versions
Node.js http.globalAgent (client)keep-alive on with a 5 second timeout since v19
Go http.DefaultTransport IdleConnTimeout90 seconds
AWS Application Load Balancer idle timeout60 seconds

A Go client with the default transport calling a Node.js server with defaults keeps connections up to 90 seconds; the server closes them after about 5. Node's agent reads the server's Keep-Alive: timeout=... hint and subtracts agentKeepAliveTimeoutBuffer (1000 ms by default) so it closes first, and keepAliveTimeoutBuffer on the server adds the same margin from the other side. Other clients may ignore the hint. Node.js server.keepAliveTimeout · Node.js new Agent options · Node.js http.globalAgent · Go net/http Transport

Behind an Application Load Balancer, the load balancer is the client of your targets. AWS recommends configuring the application's idle timeout to be larger than the load balancer's; otherwise the load balancer may send a request on a connection the target is closing and return 502 to the caller. Its troubleshooting page lists a target that closed the connection with a reset or FIN while a request was outstanding, and asks whether the target's keep-alive duration is shorter than the idle timeout. A Node.js target with its 5 second default behind a 60 second ALB is the textbook case. ALB connection idle timeout · ALB HTTP 502 causes · ALB 504s and the idle timeout

Observe which side closes first#

Read both timeouts from configuration: the server's keep-alive setting (or the framework's default for the version in production) and the client pool's idle timeout. For an ALB, read idle_timeout.timeout_seconds. describe-load-balancer-attributes

aws elbv2 describe-load-balancer-attributes \
  --profile PROFILE --region REGION --load-balancer-arn LOAD_BALANCER_ARN \
  --query "Attributes[?Key=='idle_timeout.timeout_seconds']" --no-cli-pager

Check whether the server advertises its timeout. A Keep-Alive header with a timeout in the response tells clients that honor it when to stop reusing the connection.

curl -sv -o /dev/null http://orders.example.internal:8080/healthz 2>&1 | grep -i '^< \(connection\|keep-alive\)'

On the client host, a socket in CLOSE-WAIT has received the peer's FIN and is waiting for the local application to close it. Pooled sockets in that state toward the server are connections the server already closed and the pool has not noticed. RFC 9293, connection states · ss(8)

ss -tn state close-wait 'dst 10.0.12.34'

For proof, capture only FIN and RST segments for the server's address and line them up with failing requests. A FIN from the server followed within milliseconds by a request from the client, then a reset, is the race. Capturing needs privileges on the host, but it reads traffic only. pcap-filter(7)

tcpdump -nn -i any 'host 10.0.12.34 and tcp[tcpflags] & (tcp-fin|tcp-rst) != 0'

In Node.js, log req.reusedSocket with each failure. If every ECONNRESET is on a reused socket and none on a fresh one, the cause is reuse, not the server's health.

Decision table#

ObservationPoints to
Failures only on reused sockets, server keep-alive shorter than client idleKeep-alive reuse race
Server FIN just before the failing request in the captureKeep-alive reuse race
ALB 502s, target keep-alive below ALB idle timeoutTarget closes before the ALB
Failures after minutes of idleness through NAT or NLBMiddlebox idle timeout, see the idle reset guide
Fresh connections fail tooThe server or the path, see refused or timed out

A worked example with two outcomes#

This example is illustrative. The fictional checkout service (Go, default transport) calls orders (Node.js 22) at about 2 requests per second off-peak. About 0.1% of POST calls fail with http: server closed idle connection and the orders logs show nothing for them.

In the first outcome, orders runs with the default keepAliveTimeout of 5000 ms and checkout keeps idle connections for 90 seconds. A capture shows a FIN from 10.0.12.34 at 03:14:07.002 UTC and a request from checkout on the same connection at 03:14:07.004, followed by a reset. Setting the client's IdleConnTimeout below 5 seconds, or raising the server's keep-alive above the client's, ends the failures.

In the second outcome, orders sits behind an ALB with a 60 second idle timeout, and checkout sees 502s from the ALB at a similar rate. The ALB's HTTPCode_ELB_502_Count rises and the target's keep-alive is 5 seconds. Here the ALB is the client that loses the race; raising orders' keep-alive timeout above 60 seconds is the change.

Limits#

A capture shows that a close raced a request, not which setting caused it, and some client libraries retry idempotent requests on reused connections without logging anything, which hides the rate. Server defaults change between versions; read the documentation for the version in production. HTTP/2 multiplexes requests on one connection and closes with GOAWAY frames, so the race looks different there. For other ways a dependency call fails at runtime, see the hub.

Questions#

Should the client or the server have the longer timeout?

The server, or anything acting as one, should keep idle connections longer than its clients reuse them. Then the client always closes first and never sends on a connection the server is closing.

Why does it only happen when traffic is low?

At high rates every pooled connection is reused within the server's timeout. At low rates some connections sit idle long enough for the server to close them just as the client picks them up.

Is retrying the failed request enough?

For idempotent requests a retry on a fresh connection works and many libraries do it. For non-idempotent requests HTTP advises against automatic retries, so align the timeouts instead.

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