ECONNRESET
The connection was abruptly closed by the remote peer server or intermediary socket.
Usually happens because:
- ☑ Remote Server Crash
- ☑ Inactivity Timeout
- ☑ Malformed Requests
🔍 Quick Checklist:
What is ECONNRESET?
ECONNRESET stands for 'Connection Reset by Peer'. In Node.js networking, it indicates that an established TCP connection was abruptly closed by the remote server, intermediate load balancer, firewall, or proxy before the data exchange could be fully completed. Unlike normal socket closures, a reset indicates that the remote peer sent a TCP RST (reset) packet, discarding the connection immediately due to an error, timeout, or shutdown state. This is a common network exception when dealing with unstable APIs, database connection pools, or large file uploads.
Common Causes
- Remote Server Crash: The destination API server crashed, restarted, or terminated the process mid-transaction.
- Inactivity Timeout: Firewalls, load balancers, or proxies closed idle connections to reclaim system resources.
- Malformed Requests: The client sent invalid request frames, forcing the server to issue a TCP RST packet to clean the socket.
- Connection Pool Recycling: Databasing pools or keep-alive sockets closed stale connections on the remote end.
| Cause | Frequency |
|---|---|
| Remote Server Crash | ⭐⭐⭐⭐⭐ |
| Inactivity Timeout | ⭐⭐⭐⭐ |
| Malformed Requests | ⭐⭐⭐ |
Common Mistakes
- Assuming ECONNRESET indicates a bug in your code (most of the time it is caused by remote server crashes or network middlebox timeouts).
- Failing to handle socket error events, causing Node applications to crash on temporary network drops.
How to Fix
Framework-Specific Examples
Checking for client aborts and TCP socket closures within an Express route.
app.get('/stream-data', (req, res) => {
const interval = setInterval(() => {
res.write('Data chunk...\n');
}, 1000);
req.on('close', () => {
console.log('Client aborted request or socket reset.');
clearInterval(interval);
res.end();
});
});Server Configuration Examples
Aligning Nginx keepalive timeouts to match Node's server limits.
http {
keepalive_timeout 65;
keepalive_requests 100;
}Best Practices
- Use robust client request libraries that support automatic retries (like Got or Axios).
- Implement circuit breakers (e.g. Opossum) for third-party microservices.
- Monitor proxy logs for upstream timeouts.
Frequently Asked Questions (FAQ)
Q: What is the difference between ECONNRESET and ECONNREFUSED?
ECONNRESET means the connection was established but was forcibly closed *during* the transaction. ECONNREFUSED means the target server refused to establish a connection *at all* (offline or blocked).
Q: Why does a firewall cause ECONNRESET?
If a connection is idle for too long, a stateful firewall may remove it from its tracking table. When a new packet arrives, the firewall replies with a TCP RST packet, triggering ECONNRESET on the client.
Q: How do I test my error handling for ECONNRESET?
You can create a local TCP server that accepts a connection and immediately calls `socket.destroy()` without sending any data.
Q: Does Node.js automatically retry ECONNRESET errors?
No. You must implement retry logic in your application code or use a third-party HTTP client that supports automatic retries.