Skip to content

/errors/http/socket-hang-up-econnreset

Error: socket hang up (ECONNRESET) in Node.js HTTP requests

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · curl 8.21.0 (x86_64-w64-mingw32) libcurl/8.21.0 Schannel zlib/1.3.2updated
tool
HTTP
versions
Node.js 18+
os
any

Error: socket hang up (code ECONNRESET) means Node.js sent an HTTP request and the connection was closed before any response arrived. The TCP connection itself worked, so this is not ECONNREFUSED: the server, a proxy or a load balancer accepted the request and then hung up. Look at the server side first; when the error is intermittent, a keep-alive timeout mismatch is the usual cause.

Reproduce it

A local server that destroys the socket without answering:

bash
cat > hangup.js <<'EOF'
const http = require("http");
const srv = http.createServer((req, res) => req.socket.destroy());
srv.listen(4920, "127.0.0.1", () => {
  http.get("http://127.0.0.1:4920/", () => {}).on("error", (e) => {
    console.log(`${e.name}: ${e.message}`, `(code ${e.code})`);
    srv.close();
  });
});
EOF
node hangup.js

Output

Error: socket hang up (code ECONNRESET)

If the server had sent a status line and then closed the connection in the middle of the body, the error would be aborted on the response instead. socket hang up specifically means nothing was received.

Intermittent errors and keep-alive

Since Node.js 19 the default HTTP agent keeps connections alive. If the server closes an idle connection at the same moment the client reuses it, the request fails with socket hang up. Node.js servers close idle keep-alive connections after server.keepAliveTimeout (5 seconds by default); proxies and load balancers have their own idle timeouts.

FixHow
Longer server idle timeoutSet server.keepAliveTimeout (and the proxy idle timeout) above the client idle time
Shorter client idle timenew http.Agent({ keepAlive: true, timeout: 4000 })
Confirm the causeSend the failing request with agent: false (no connection reuse)
Tolerate itRetry idempotent requests once when the error occurs on a reused socket

Libraries

  • axios shows the same error as AxiosError: socket hang up with code: 'ECONNRESET'.
  • fetch() in Node.js (undici) reports TypeError: fetch failed; the reason is in error.cause (for example SocketError: other side closed).
  • Behind nginx, a backend that hangs up produces upstream prematurely closed connection in the nginx error log and a 502 Bad Gateway for the client.

Sources