Skip to content
Latchkey

Node.js "Error: socket hang up" in CI

Node reports socket hang up when a connection closes before the HTTP response is fully received. It is a special case of ECONNRESET, common with slow servers, timeouts, or transient network drops.

What this error means

An HTTP request fails with Error: socket hang up. The failure is intermittent - the same request succeeds on retry - when the cause is a transient drop rather than a logic error.

node
Error: socket hang up
    at connResetException (node:internal/errors:720:14)
    at TLSSocket.socketOnEnd (node:_http_client:519:23)
  code: 'ECONNRESET'

Common causes

The server closed before responding

A slow or overloaded server, or one that hit its own timeout, closed the socket before sending a full response. The client sees a hang up.

A transient network drop

A momentary disruption between the runner and host tears down the connection mid-response. It is not deterministic.

How to fix it

Add timeouts and retry transient hang-ups

Set an explicit request timeout and retry the request when it fails with a hang up.

src/http.ts
const res = await fetch(url, {
  signal: AbortSignal.timeout(10000),
});

Wait for the dependency to be ready

Ensure the target server is fully up and not still warming before issuing requests in CI.

  1. Add a readiness/health check before the first request.
  2. Increase the server response timeout if it is genuinely slow.
  3. Retry on hang up with a small backoff.

How to prevent it

  • Set explicit request timeouts so a slow server fails fast and predictably.
  • Retry transient hang-ups with bounded backoff.
  • Gate tests on a readiness check for dependent services.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →