Skip to content

Fall back to the next address when one silently drops connections - #99

Open
khanak0509 wants to merge 5 commits into
prooflinedev:mainfrom
nines-lab:sync-connect-fallback
Open

khanak0509 wants to merge 5 commits into
prooflinedev:mainfrom
nines-lab:sync-connect-fallback

Conversation

@khanak0509

Copy link
Copy Markdown

Problem

ProoflineClient resolves the host itself and tries each address in turn, but all attempts share
one deadline. If the first address silently drops the SYN (broken IPv6, or a dead node behind
round-robin DNS), that attempt uses up the whole timeout, the next address is never tried, and the
call fails with urn:proofline:sdk:timeout.

proofline.sh has two AAAA and two A records, and dual-stack hosts try IPv6 first. So on a network
with broken IPv6, every sync call fails, while AsyncProoflineClient (anyio Happy Eyeballs) and
stock httpx recover. Fast failures such as "connection refused" already fell back correctly.

Approach

RFC 8305-style racing in NetworkBackend.connect_tcp, only when a name has more than one address:

  • addresses alternate between IPv6 and IPv4;
  • each attempt starts 250 ms after the previous one (anyio's delay), or at once when one fails;
  • the first connection wins and the others are closed; the request deadline bounds the whole race.

Each attempt still goes through httpcore2.SyncBackend.connect_tcp, so socket options, scoped
IPv6 and error mapping are unchanged. Single-address hosts and IP literals keep the old path.

Verification

  • python3 scripts/check.py passes, on Python 3.11 and 3.13.
  • tests/test_sync_connect.py: 19 tests covering fallback, deadlines, cleanup and ordering.
    Each fix from the review below has a test that fails without it.
  • Real proofline.sh, with IPv6 simulated as black-holed: main timed out after 5.00 s; this
    branch connected in about 0.8 s.
Repro script (run from the repository root)
"""Sync client vs the real proofline.sh when IPv6 silently drops packets (simulated).

IPv6 attempts behave like an unanswered SYN: they wait out the connect timeout.
IPv4 attempts are real TCP+TLS to proofline.sh. A dummy token is used, so the
expected success is a 401 problem from the server (= the connection worked).
Run:  .venv/bin/python -I ../repro/real_world_broken_ipv6.py   (from sdk-python/)
"""
import sys, time, uuid
sys.path.insert(0, ".")
import httpcore2
from proofline import ProoflineClient, ProoflineError, _sync_http

real_connect = httpcore2.SyncBackend.connect_tcp
def connect(self, host, port, timeout=None, *args):
    if ":" in host:  # IPv6: black-holed
        print(f"  try {host:<28} (IPv6, no answer)")
        time.sleep(timeout or 0)
        raise httpcore2.ConnectTimeout("timed out")
    print(f"  try {host:<28} (IPv4, real)")
    return real_connect(self, host, port, timeout, *args)
httpcore2.SyncBackend.connect_tcp = connect

# A dual-stack host resolves proofline.sh's AAAA records first (RFC 6724). This machine may
# have no IPv6, so put the published AAAA records (dig AAAA proofline.sh) in front.
real_resolve = _sync_http.resolve
AAAA = ["2606:4700:3031::6815:3831", "2606:4700:3034::ac43:b191"]
_sync_http.resolve = lambda host, port, timeout: AAAA + [
    a for a in real_resolve(host, port, timeout) if a not in AAAA
]

print("resolved:", _sync_http.resolve("proofline.sh", 443, 5))
with ProoflineClient("demo-token-not-real", timeout=5) as client:
    started = time.monotonic()
    try:
        client.list_repositories(uuid.uuid4())
    except ProoflineError as error:
        print(f"=> HTTP {error.status} {error.type} after {time.monotonic() - started:.2f}s")

Proofline review

I ran Proofline on this change in my own organization
(nines-lab/sdk-python#1), over four rounds.
I reproduced each finding with a failing test or a mutation before changing anything.

Finding Outcome
A failure landing just after the stagger wait delayed the next attempt by 250 ms Reproduced (256 ms), fixed
A late ConnectError could replace the deadline timeout Reproduced, fixed
A failed thread start could leave the winning connection open Reproduced, fixed
A worker thread that started late could connect past the deadline Fixed: the budget is now measured when the worker runs
A connection completing as the deadline passes should be discarded Disagreed. It connected within its budget, and discarding a working connection would make the client less reliable. Documented in a code comment.
Several test gaps (unbounded polling, order recorded on the wrong thread, coverage holes) Fixed, each confirmed by a mutation the new test catches

I stopped after the fourth round. Proofline had confirmed the earlier fixes, and the remaining
findings were narrow scheduling edge cases.

Copilot AI balanced review requested due to automatic review settings November 24, 2026 06:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants