ECONNRESET 🔌 API

Error: socket hang up / read ECONNRESET

The other side closed the TCP connection abruptly while your client was still using it.

Seen on: Node.js Python AWS

Meaning

The connection was established, then reset. Typical culprits are keep-alive connections reused after the server (or a load balancer) already closed them, servers crashing mid-response, and proxies/firewalls killing long or idle connections.

Common causes

  • Reusing a keep-alive socket the server already closed (server keep-alive timeout < client’s)
  • Upstream crashed or restarted while handling the request
  • Load balancer/NAT idle timeout dropping the connection
  • TLS/protocol mismatch (HTTP to an HTTPS port)
  • Request body too large → server closes early

⚡ Quick fix

  1. Retry idempotent requests once on ECONNRESET
  2. Make server keep-alive timeout longer than the load balancer’s idle timeout
  3. Check server logs for crashes at the same time
  4. Verify protocol/port (https on 443)

Detailed fix by platform

Node.js

  1. Server keep-alive longer than the ALB idle timeout (60s):
    javascript
    const server = app.listen(3000);
    server.keepAliveTimeout = 65_000;
    server.headersTimeout = 66_000;   // must be greater than keepAliveTimeout

Python

  1. requests: mount an HTTPAdapter with Retry(total=3, allowed_methods=None, backoff_factor=0.5) to retry connection resets.

How to diagnose

  1. When — First request or reused connection?
  2. Server — Crash/restart logs at that time?
  3. Timeouts — Keep-alive vs LB idle timeout
  4. Protocol — Right scheme and port?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.