python network_error ai_generated partial

httpx.ConnectError: [Errno 104] Connection reset by peer while connecting to 'https://unstable.example.com'

ID: python/httpx-connecterror-connection-reset-by-peer

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-10-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The remote server abruptly closed the TCP connection, often due to server overload or network issues.

generic

中文

远程服务器突然关闭TCP连接,通常是由于服务器过载或网络问题。

Workarounds

  1. 85% success Implement retry with exponential backoff and jitter
    import time, random
    for i in range(3):
        try:
            response = httpx.get('https://unstable.example.com')
            break
        except httpx.ConnectError:
            time.sleep(2 ** i + random.uniform(0, 1))
  2. 70% success Use a connection pool with keep-alive to reduce connection resets
    client = httpx.Client()
    response = client.get('https://unstable.example.com')

Dead Ends

Common approaches that don't work:

  1. Retrying immediately without any delay 80% fail

    The server may still be overloaded, causing immediate reset again.

  2. Using a different HTTP version 90% fail

    The issue is at the TCP level, not HTTP version.