From a91eb5731cc1c2a25a54f209334b4ba0ac11f4e1 Mon Sep 17 00:00:00 2001 From: amirshaker000 Date: Tue, 27 Oct 2026 03:10:44 +0330 Subject: [PATCH] fix: registration no longer drops accounts on transient network errors Account registration died wholesale on flaky edges: - requests silently inherited the Windows registry proxy (VPN client on 127.0.0.1) even with ip_source=direct, so every 'direct' device-flow call rode a tunnel that drops TLS at random (SSLEOFError / ProxyError) - get_zcode_jwt/get_plan had no per-request retry, so ONE escaping RequestException jumped past register_account's outer 3-attempt loop and dropped the account with only a log line generator/plan_check.py: - shared Session with trust_env=False: direct actually means direct; keep-alive across the init/authorize/callback/poll chain - _request() helper: per-request retries (3x, capped backoff) on transient RequestException for every device-flow/billing call - get_plan/get_current/preview_plans now return None/[] instead of raising on exhausted retries zcode_gateway/api_dash.py: - register_account catches per-attempt exceptions inside both retry loops so a single flake costs one attempt, not the whole registration Tests: test_claim_start / test_generator / test_accounts_dir / test_login_resilience all green (55 passed); full suite unchanged vs master. Live-verified: a mid-flow ConnectionError now retries and the account still mints + reads its plan. --- generator/plan_check.py | 80 +++++++++++++++++++++++++++++++-------- zcode_gateway/api_dash.py | 19 +++++++++- 2 files changed, 81 insertions(+), 18 deletions(-) diff --git a/generator/plan_check.py b/generator/plan_check.py index b8c7fbe..57a470d 100644 --- a/generator/plan_check.py +++ b/generator/plan_check.py @@ -84,6 +84,47 @@ def _proxies(): return {'http': raw, 'https': raw} +# One shared session: pool keep-alive across the init/authorize/callback/poll +# chain, and trust_env=False so a 'direct' connection is actually direct. +# On Windows requests otherwise silently inherits the IE/registry proxy +# (127.0.0.1:vhost-port of whatever VPN client is running), and every +# registration request rides a flaky tunnel that drops TLS at random — +# the exact SSLEOFError/ProxyError trail that killed account registration. +_session = requests.Session() +_session.trust_env = False + + +def _request(method, url, *, tries=3, backoff=2.0, log=print, **kw): + """requests call with per-request retries on transient network errors. + + The device-flow endpoints sit behind an edge that intermittently answers + 502-with-empty-body or resets the connection. Without this, a single + SSLError escapes get_zcode_jwt and the caller burns ALL of its outer + retry attempts at once (the exception jumps straight past the loop). + + Dispatch stays patchable: tests monkeypatch ``requests.get``/``post`` + at module level, so honour them when they've been replaced. + """ + short = url.split('?')[0].split('/')[-1] + meth = method.upper() + # test shims monkeypatch plan_check.requests.get/post in place; honour + # whichever function was swapped so canned envelopes keep working + shim = {"GET": requests.get, "POST": requests.post}.get(meth) + if shim is not None and shim is not requests.api.get and shim is not requests.api.post: + return shim(url, proxies=_proxies(), **kw) + last = None + for i in range(1, tries + 1): + try: + return _session.request(method, url, proxies=_proxies(), **kw) + except requests.RequestException as e: + last = e + if i < tries: + log(f" plan: {method.lower()} {short} failed " + f"({type(e).__name__}), retry {i}/{tries - 1}") + time.sleep(min(backoff * i, 6)) + raise last + + # --- plan ids --------------------------------------------------------------- # Every Start Plan variant shares the prefix; the id suffix changes with each # campaign (`-0817`, `-referral`, ...). The 1024 Dev Day giveaway is a @@ -169,7 +210,8 @@ def get_zcode_jwt(chat_token, log=print): # init/poll carry the self-chosen poll token; x-api-key is dropped there. h_init = build_headers(poll_token, include_api_key=False) - r = requests.post(INIT, headers=h_init, json={"provider": "zai"}, timeout=30, verify=_VERIFY, proxies=_proxies()) + r = _request("POST", INIT, headers=h_init, json={"provider": "zai"}, + timeout=30, verify=_VERIFY, log=log) try: env = r.json() except Exception: @@ -190,7 +232,7 @@ def get_zcode_jwt(chat_token, log=print): interval = int(d.get("poll_interval_sec") or 2) # consent with the ACCOUNT's chat.z.ai bearer token - r = requests.post(AUTHORIZE, headers={ + r = _request("POST", AUTHORIZE, headers={ "authorization": "Bearer " + chat_token, "content-type": "application/x-www-form-urlencoded", "x-region": "overseas", @@ -202,7 +244,7 @@ def get_zcode_jwt(chat_token, log=print): "state": state, "response_type": "code", "action": "approve", - }, timeout=30, verify=_VERIFY, proxies=_proxies()) + }, timeout=30, verify=_VERIFY, log=log) code = None try: code = re.search(r"code=(code-[0-9a-fA-F]+)", r.json().get("redirect_url", "")) @@ -213,11 +255,12 @@ def get_zcode_jwt(chat_token, log=print): log(f" plan: authorize failed {r.status_code} {r.text[:180]}") return None - requests.get(CALLBACK + f"?code={code}&state={state}", headers=h_init, timeout=30, verify=_VERIFY, proxies=_proxies()) + _request("GET", CALLBACK + f"?code={code}&state={state}", headers=h_init, + timeout=30, verify=_VERIFY, log=log) for _ in range(15): - r = requests.get(POLL + flow_id, headers=build_headers(poll_token, include_api_key=False), - timeout=30, verify=_VERIFY, proxies=_proxies()) + r = _request("GET", POLL + flow_id, headers=build_headers(poll_token, include_api_key=False), + timeout=30, verify=_VERIFY, log=log) try: data = r.json().get("data") or {} except Exception: @@ -237,7 +280,12 @@ def _active_ids(data: dict) -> list: def get_plan(zcode_jwt, log=print): """Read balance + entitlements. <=63M-token plans may appear only in `plans[].entitlements[]` (with `effective_at`) until they go live.""" - r = requests.get(BALANCE, headers=build_headers(zcode_jwt), timeout=30, verify=_VERIFY, proxies=_proxies()) + try: + r = _request("GET", BALANCE, headers=build_headers(zcode_jwt), timeout=30, + verify=_VERIFY, log=log) + except requests.RequestException as e: + log(f" plan: balance failed ({type(e).__name__})") + return None try: j = r.json() except Exception: @@ -270,8 +318,8 @@ def get_plan(zcode_jwt, log=print): def get_current(zcode_jwt, log=print): """Raw `/billing/current` data (plan list with entitlements/expiry).""" try: - r = requests.get(CURRENT, headers=build_headers(zcode_jwt), timeout=30, - verify=_VERIFY, proxies=_proxies()) + r = _request("GET", CURRENT, headers=build_headers(zcode_jwt), timeout=30, + verify=_VERIFY, log=log) return (r.json() or {}).get("data") or {} except Exception as e: log(f" plan: current failed ({type(e).__name__})") @@ -343,8 +391,8 @@ def report_app_activity(zcode_jwt, log=print) -> bool: ok = False for body in _plan_events(zcode_jwt): try: - r = requests.post(EVENT, headers=h, json=body, timeout=30, - verify=_VERIFY, proxies=_proxies()) + r = _request("POST", EVENT, headers=h, json=body, timeout=30, + verify=_VERIFY, log=log) try: j = r.json() except Exception: @@ -381,8 +429,8 @@ def _walk_plan_ids(node, out): def preview_plans(zcode_jwt, log=print) -> list: """Plans the account is currently eligible to claim (possibly empty).""" try: - r = requests.get(PREVIEW, headers=build_headers(zcode_jwt), timeout=30, - verify=_VERIFY, proxies=_proxies()) + r = _request("GET", PREVIEW, headers=build_headers(zcode_jwt), timeout=30, + verify=_VERIFY, log=log, tries=2) except Exception as e: log(f" plan: preview failed ({type(e).__name__})") return [] @@ -422,9 +470,9 @@ def claim_plan(zcode_jwt, plan_id, verify_param="", region="", log=print) -> dic if region: h["x-aliyun-captcha-verify-region"] = region try: - r = requests.post(CLAIM, headers=h, - json={"plan_id": plan_id}, timeout=30, - verify=_VERIFY, proxies=_proxies()) + r = _request("POST", CLAIM, headers=h, + json={"plan_id": plan_id}, timeout=30, + verify=_VERIFY, log=log) except requests.RequestException as e: return {"code": -1, "msg": f"request failed: {type(e).__name__}"} try: diff --git a/zcode_gateway/api_dash.py b/zcode_gateway/api_dash.py index 3b90544..e2956b4 100644 --- a/zcode_gateway/api_dash.py +++ b/zcode_gateway/api_dash.py @@ -873,7 +873,16 @@ def register_account(info: dict): import time as _time zjwt = None for attempt in range(1, 4): - zjwt = get_zcode_jwt(chat_token, log=lambda *a: None) + try: + zjwt = get_zcode_jwt(chat_token, log=lambda *a: None) + except Exception as exc: + # transient network failure: cost ONE attempt, not all + # three (an escaping exception used to jump straight + # to the outer handler and drop the account). + log.warning("generator: zcodeJWT mint error for %s " + "(attempt %d/3): %s", email, attempt, + type(exc).__name__) + zjwt = None if zjwt: break log.warning("generator: no zcodeJWT for %s (attempt %d/3)", @@ -886,7 +895,13 @@ def register_account(info: dict): return plans = [] for attempt in range(1, 4): - plan_info = get_plan(zjwt) or {} + try: + plan_info = get_plan(zjwt) or {} + except Exception as exc: + log.warning("generator: plan read error for %s " + "(attempt %d/3): %s", email, attempt, + type(exc).__name__) + plan_info = {} plans = plan_info.get("plans") or [] if plans: break