LightAtom

"Socket Hang Up" in Node.js: Causes and Fixes

Node's HTTP client saw the connection close before a response arrived, typically because of a server timeout, a reused stale keep-alive socket or a protocol mismatch.

Error: socket hang up

Your request was sent, but the server (or a proxy) closed the connection without sending a response.

Quick checks

  • Does it fail on the first request or after idle time?
  • Are you using `http` to talk to an `https` server (or the reverse)?
  • Is there a proxy in between?

Fixes on Mac

  1. Match protocol and port

    1. Use the https module for HTTPS endpoints; plain HTTP to port 443 often ends in a hang-up.
  2. Handle stale keep-alive sockets

    1. Set the client's keep-alive timeout below the server's (for example, a 4-second free-socket timeout if the server closes after 5).
    2. Retry idempotent requests once on hang-up.
  3. Raise timeouts for slow endpoints

    1. Server and proxy timeouts that are shorter than the work take produce hang-ups; check both ends.

Fixes on Windows

  1. Same causes

    1. The fixes are platform-independent; also check corporate proxies.

Other devices

  1. Servers

    1. Look for crashes or timeouts in the server logs at the time of the hang-up.

Still not working?

ETIMEDOUT is the neighbouring error: the connection or response didn't arrive within the timeout. Check reachability (nc -vz host port) and the server's response time.

See what’s happening

LightAtom's connections view shows connections from your Node process opening and closing, which makes keep-alive reuse visible.

Frequently asked questions

What causes “socket hang up”?

The server or a proxy closed the connection before responding: timeouts, crashes, a stale keep-alive socket, or speaking HTTP to an HTTPS port.

How do I fix socket hang up in Axios or node-fetch?

Check the URL's protocol and port, set sensible timeouts, retry idempotent requests once, and keep client keep-alive timeouts shorter than the server's.

What is ETIMEDOUT?

A timeout: the connection or response took longer than allowed. It points to network reachability or a slow server.

LightAtom team

The people who build LightAtom, a network monitor for macOS.