Python "ConnectionResetError" from a test client in CI
ConnectionResetError: [Errno 104] Connection reset by peer means the remote side abruptly closed the connection. In tests it usually indicates the server under test exited, crashed, or closed the socket early.
What this error means
A test that talks to a local or remote server fails with "ConnectionResetError: [Errno 104] Connection reset by peer".
pytest
ConnectionResetError: [Errno 104] Connection reset by peerCommon causes
The server process crashed or exited mid-request
The test client was reading when the backing server died, resetting the socket.
A race starting the server fixture
The client connected before the server was fully ready, or after it began shutting down.
How to fix it
Stabilise the server fixture and retry transient resets
- Wait for a readiness signal (port open / health check) before making requests.
- Ensure the server fixture stays alive for the whole test, with teardown after.
- Retry the request once on a reset for genuinely flaky remote endpoints.
Python
import socket, time
def wait_ready(host, port, timeout=10):
end = time.time() + timeout
while time.time() < end:
try:
socket.create_connection((host, port), timeout=1).close()
return
except OSError:
time.sleep(0.1)
raise TimeoutError("server not ready")How to prevent it
- Latchkey managed runners auto-retry transient connection resets, so a flaky peer reset does not by itself fail the job.
- Gate test clients on a server readiness check.
- Keep server fixtures alive for the full test scope.
Related guides
Python "TimeoutError" on a socket in CIFix "TimeoutError" / "socket.timeout" in CI - a socket operation exceeded its timeout, usually a slow or unre…
Python "ssl.SSLError: WRONG_VERSION_NUMBER" in CIFix "ssl.SSLError: [SSL: WRONG_VERSION_NUMBER]" in CI - a TLS handshake was attempted against a plaintext end…
Python "aiohttp.ClientConnectorError" in CIFix "aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host" in CI - the aiohttp client could…