Connection Reset
Peer
Internet Protocol
Network Troubleshooting
Error Messages

What does "connection reset by peer" mean?

Codemia Knowledge Hub ·

"Connection reset by peer" means the remote side of a TCP connection closed it abruptly by sending a RST (reset) packet instead of going through the normal TCP shutdown. The server (or client) on the other end forcibly terminated the connection, so your application got an error instead of the data it expected.

Normal close vs. reset

A graceful TCP close is a four-step FIN handshake:

  1. Side A sends a FIN packet ("I'm done sending")
  2. Side B acknowledges the FIN
  3. Side B sends its own FIN ("I'm done too")
  4. Side A acknowledges

That lets both sides finish processing remaining data. A reset skips all of it: the peer sends one RST packet that says "this connection no longer exists, stop immediately."

What triggers a RST packet

At the TCP level, a RST is sent in these situations:

 
1SYN to a closed port          -> RST from the receiver
2Data sent to a half-open conn -> RST from the receiver
3Application calls close()     -> RST if unread data in buffer
4Firewall intercepts packet    -> RST injected into the stream

Here is how it surfaces in application code.

Python:

python
1import socket
2
3sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4try:
5    sock.connect(('api.example.com', 443))
6    sock.sendall(b'GET / HTTP/1.1\r\nHost: api.example.com\r\n\r\n')
7    data = sock.recv(4096)
8except ConnectionResetError as e:
9    print(f"Connection reset by peer: {e}")
10finally:
11    sock.close()

Java:

java
1try {
2    Socket socket = new Socket("api.example.com", 443);
3    OutputStream out = socket.getOutputStream();
4    out.write("GET / HTTP/1.1\r\nHost: api.example.com\r\n\r\n".getBytes());
5    InputStream in = socket.getInputStream();
6    int data = in.read();  // throws if reset
7} catch (SocketException e) {
8    System.out.println("Connection reset: " + e.getMessage());
9}

curl:

bash
1curl -v https://api.example.com/endpoint
2# * Recv failure: Connection reset by peer
3# * Closing connection 0
4# curl: (56) Recv failure: Connection reset by peer

Causes and how to confirm each

CauseSymptomsHow to Confirm
Server crashed or restartedSudden reset during active requestCheck server uptime logs, look for OOM kills
Server-side timeoutReset after idle periodCompare idle time against server's keepalive_timeout
Load balancer dropped connectionIntermittent resets under loadCheck LB idle timeout settings (often 60s on AWS ALB)
Firewall or IDS killed connectionResets on specific request patternsPacket capture shows RST from intermediate IP
Server rejected TLS handshakeReset during SSL negotiationopenssl s_client -connect host:port fails
Server's listen backlog fullSlow or timed-out connects during a spike; resets only if net.ipv4.tcp_abort_on_overflow=1 (Linux drops the SYN by default)netstat -s shows listen queue overflows
Client sent data after server closedReset on write after partial readApplication logs show incomplete reads

Debugging a reset step by step

Step 1: Capture the packets

Looking at the actual packets is the most reliable diagnosis:

bash
1# Linux/macOS - capture traffic to a specific host
2sudo tcpdump -i any host api.example.com -w reset_debug.pcap
3
4# Then reproduce the error and open in Wireshark
5wireshark reset_debug.pcap

Find the RST and check what came right before it. That preceding packet usually tells you the cause.

Step 2: Check server-side logs

bash
1# Nginx
2tail -f /var/log/nginx/error.log | grep "reset"
3
4# Apache
5tail -f /var/log/apache2/error.log | grep "reset\|RST"
6
7# Application server (e.g., Gunicorn)
8tail -f /var/log/gunicorn/error.log

Step 3: Check connection limits and timeouts

bash
1# Linux - check current connections
2ss -s
3
4# Check kernel connection limits
5sysctl net.core.somaxconn
6sysctl net.ipv4.tcp_max_syn_backlog
7
8# Check file descriptor limits
9ulimit -n

Step 4: Test with different tools

bash
1# Test basic TCP connectivity
2nc -zv api.example.com 443
3
4# Test with explicit timeout
5curl --connect-timeout 5 --max-time 30 -v https://api.example.com/
6
7# Test TLS specifically
8openssl s_client -connect api.example.com:443

Handling resets in your code

The right response depends on whether the operation is idempotent (safe to retry).

Python with retry logic:

python
1import time
2import requests
3from requests.adapters import HTTPAdapter
4from urllib3.util.retry import Retry
5
6session = requests.Session()
7retries = Retry(
8    total=3,
9    backoff_factor=0.5,          # 0s, 1s, 2s
10    status_forcelist=[502, 503],
11    allowed_methods=["GET", "PUT"],  # only retry idempotent methods
12)
13session.mount("https://", HTTPAdapter(max_retries=retries))
14
15try:
16    response = session.get("https://api.example.com/data")
17except requests.exceptions.ConnectionError as e:
18    print(f"All retries failed: {e}")

Node.js with retry:

javascript
1async function fetchWithRetry(url, maxRetries = 3) {
2  for (let attempt = 1; attempt <= maxRetries; attempt++) {
3    try {
4      const response = await fetch(url);
5      return response;
6    } catch (err) {
7      if (err.cause?.code === 'ECONNRESET' && attempt < maxRetries) {
8        const delay = Math.pow(2, attempt) * 100;
9        console.log(`Connection reset, retrying in ${delay}ms...`);
10        await new Promise(r => setTimeout(r, delay));
11        continue;
12      }
13      throw err;
14    }
15  }
16}

Reset vs. refused, timed out and broken pipe

ErrorWhat It MeansTCP Behavior
Connection reset by peerRemote side sent RSTAbrupt close, no FIN handshake
Connection refusedNo process listening on portRST in response to SYN
Connection timed outNo response at allNo packets received
Broken pipeWrite to a closed connectionLocal side detects closed socket
EOF / Connection closedRemote side sent FINGraceful close

The fix differs for each. "Connection refused" means the service is not running. "Connection reset" means it was running but dropped you.

Mistakes that make resets worse

  • Retrying non-idempotent requests. If a POST is reset, the server may already have processed it, and a blind retry can create duplicates. Only retry idempotent operations (GET, PUT, DELETE) automatically.
  • Ignoring keep-alive mismatches. If the client holds a connection for 120 seconds but the server's keep-alive timeout is 60, the server closes it and your next request on it gets a reset. Keep client timeouts shorter than server timeouts.
  • Blaming the network for an application problem. A reset at the same point every time (for example, after a specific header or payload) usually means the server is rejecting the request.
  • Skipping intermediate proxies. Load balancers, CDNs and reverse proxies have their own idle timeouts. An AWS ALB defaults to a 60-second idle timeout; if the backend takes longer to respond, the ALB returns 504 Gateway Timeout to the client rather than a reset, and it closes client connections that sit idle past the timeout. Check every hop's timeout, not just the server's.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions