Skip to content
Latchkey

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 peer

Common 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

  1. Wait for a readiness signal (port open / health check) before making requests.
  2. Ensure the server fixture stays alive for the whole test, with teardown after.
  3. 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →