Skip to content

Error explained

Error: socket hang up

"socket hang up" means the connection closed before a response arrived. It is Node’s message for a reset or an early close, usually with code ECONNRESET.

3 min read9 sectionsUpdated

On this page

Key points

  • The server closed the connection before sending a full response.
  • Check the error code: ECONNRESET is the usual companion.
  • Timeouts that destroy the socket on either side cause it too.
  • Look at the server side first: crashes and body limits are common.

Causes

CauseWhere to look
Server process crashed or restartedServer logs and restart counts at that time
Keep-alive socket reused after the server closed itIdle timeouts on server, proxy and client agent
A timeout destroyed the requestClient timeout options; server request timeouts
Proxy rejected or closed the requestProxy logs: body size limits, idle timeouts
Calling HTTPS on an HTTP port (or the reverse)The URL scheme and port

Debugging

javascript
req.on('error', (err) => {
  console.error({ msg: 'request failed', code: err.code, message: err.message, host: url.host });
});

// See exactly what happens on the wire
// NODE_DEBUG=http,net node app.js

Fixes

  • Align keep-alive timeouts so the server keeps sockets open longer than clients and proxies do. See ECONNRESET.
  • Make servers shut down gracefully, finishing in-flight requests before exiting.
  • Set sensible timeouts on both sides and retry idempotent calls with backoff.
  • Check request size limits on proxies (for example Nginx client_max_body_size) for large uploads.

Catching it in production

Traces mark the failed client span and the target; if the server is yours, its trace (or the lack of one) tells you whether the request ever arrived.

VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.

Behind a load balancer

Cloud load balancers close idle connections after a fixed time (often 60 seconds, configurable). If your Node server's keepAliveTimeout is shorter than that, the server may close a connection the load balancer is about to reuse, and the load balancer reports a 502 while the client sees a reset. Setting the server's keepAliveTimeout above the load balancer's idle timeout removes most of these.

Frequently asked questions

Is socket hang up the same as ECONNRESET?

Closely related. Node reports "socket hang up" when the socket closed before the response, and the error usually carries code ECONNRESET.

Why only with axios or got?

Any HTTP client in Node can surface it. Agent and keep-alive settings differ between clients, which changes how often you see it.

  • Error: connect ETIMEDOUT: What ETIMEDOUT means, how it differs from ECONNREFUSED, the usual causes such as firewalls, security groups and wrong IPs, and how to set timeouts.
  • ENOTFOUND getaddrinfo: how to fix DNS errors in Node.js: Fix getaddrinfo ENOTFOUND and EAI_AGAIN in Node.js: wrong hostnames, a URL passed as host, container DNS and thread pool exhaustion, with test commands.
  • SyntaxError: Cannot use import statement outside a module: Why Node.js throws "Cannot use import statement outside a module", and the fixes: type module in package.json, .mjs files, TypeScript and Jest settings.
  • Sentry vs Datadog: Sentry vs Datadog compared: developer-focused error tracking against a full observability platform, covering errors, tracing, replay, logs and cost.
  • Cloudflare error 523: origin is unreachable: Fix Cloudflare error 523 Origin is Unreachable: wrong DNS IPs, routing problems at the host and firewalls, with tests that confirm where traffic stops.
  • OpenTelemetry for NestJS: Trace a NestJS service with OpenTelemetry: load the SDK before Nest boots, instrument HTTP, Express and NestJS core, and export JSON to VitalRix.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.