ConnectionResetError
PythonERRORNotableConnection ErrorHIGH confidence
Connection reset by peer
Production Risk
Common with long-lived connections; implement reconnect with exponential backoff.
What this means
A subclass of ConnectionError (errno ECONNRESET) raised when the remote peer forcibly resets a TCP connection by sending a RST packet.
Why it happens
- 1Remote server crashed or restarted mid-request
- 2Firewall or load balancer terminated an idle connection
- 3Client sent data after the server closed its write end
How to reproduce
Reading from a socket after the server resets the connection.
trigger — this will error
trigger — this will error
import socket
s = socket.create_connection(('host', 8080))
# Server resets connection
data = s.recv(4096) # Raises ConnectionResetErrorexpected output
ConnectionResetError: [Errno 104] Connection reset by peer
Fix
Implement reconnect logic
WHEN After ConnectionResetError
Implement reconnect logic
import socket
import time
def resilient_request(host, port, data, retries=3):
for attempt in range(retries):
try:
with socket.create_connection((host, port), timeout=10) as s:
s.sendall(data)
return s.recv(65536)
except ConnectionResetError:
if attempt < retries - 1:
time.sleep(2 ** attempt) # Exponential backoff
raise ConnectionResetError("All retries failed")Why this works
ConnectionResetError is always terminal for that connection; close and reconnect.
Code examples
Triggerpython
import socket
s = socket.create_connection(("host", 8080))
# server resets connection
data = s.recv(4096) # ConnectionResetErrorHandle with try/exceptpython
try:
data = sock.recv(4096)
except ConnectionResetError:
sock.close()
sock = reconnect()Avoid with reconnect logicpython
import time, socket
for attempt in range(3):
try:
with socket.create_connection((host, port)) as s:
return s.recv(4096)
except ConnectionResetError:
time.sleep(2 ** attempt)What not to do
✕ Retry on the same socket after ConnectionResetError
The connection is permanently broken; create a new socket.
Same error in other languages
Sources
Official documentation ↗
Python Docs — Built-in Exceptions
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev