Spaces:
Running
Running
Commit ·
b570ea1
1
Parent(s): 7cca751
fix(call_model): retry on transient ConnectionReset / broken-pipe
Browse filesThe Space's first request after an idle period frequently fails with
'All model candidates failed. Last error: ProtocolError(...,
ConnectionResetError(104, Connection reset by peer))' — Cloudflare's
edge silently RSTs idle keepalive sockets pooled by urllib3 inside
huggingface_hub.InferenceClient. urllib3 evicts the bad connection on
failure, so simply retrying the same call lets the pool open a fresh
socket. Add per-model retry budget of 3 with 0.3 / 0.6 / 1.2 s backoff,
gated on a string-match against the underlying exception chain so we
only retry transient network errors (not logical / 4xx / oom).
app.py
CHANGED
|
@@ -2565,6 +2565,31 @@ def _build_client_for_model(model: str) -> Tuple[InferenceClient, str, List[str]
|
|
| 2565 |
return client, model, []
|
| 2566 |
|
| 2567 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2568 |
def call_model(
|
| 2569 |
client: InferenceClient,
|
| 2570 |
messages: List[Dict[str, str]],
|
|
@@ -2579,19 +2604,31 @@ def call_model(
|
|
| 2579 |
model_order.append(m)
|
| 2580 |
|
| 2581 |
last_error = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2582 |
for model_name in model_order:
|
| 2583 |
-
|
| 2584 |
-
|
| 2585 |
-
|
| 2586 |
-
|
| 2587 |
-
|
| 2588 |
-
|
| 2589 |
-
|
| 2590 |
-
|
| 2591 |
-
|
| 2592 |
-
|
| 2593 |
-
|
| 2594 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2595 |
raise RuntimeError(f"All model candidates failed. Last error: {last_error}")
|
| 2596 |
|
| 2597 |
|
|
|
|
| 2565 |
return client, model, []
|
| 2566 |
|
| 2567 |
|
| 2568 |
+
_TRANSIENT_NETWORK_PATTERNS = (
|
| 2569 |
+
"connection reset",
|
| 2570 |
+
"connection aborted",
|
| 2571 |
+
"broken pipe",
|
| 2572 |
+
"remote disconnected",
|
| 2573 |
+
"remote end closed",
|
| 2574 |
+
"server disconnected",
|
| 2575 |
+
"eof occurred",
|
| 2576 |
+
"connectionreseterror",
|
| 2577 |
+
"connectionerror",
|
| 2578 |
+
"104,", # errno 104 = ECONNRESET, often appears like "(104, 'Connection reset...')"
|
| 2579 |
+
)
|
| 2580 |
+
|
| 2581 |
+
|
| 2582 |
+
def _is_transient_network_error(exc: BaseException) -> bool:
|
| 2583 |
+
cause: Optional[BaseException] = exc
|
| 2584 |
+
seen = 0
|
| 2585 |
+
while cause is not None and seen < 4:
|
| 2586 |
+
if any(p in str(cause).lower() for p in _TRANSIENT_NETWORK_PATTERNS):
|
| 2587 |
+
return True
|
| 2588 |
+
cause = cause.__cause__ or cause.__context__
|
| 2589 |
+
seen += 1
|
| 2590 |
+
return False
|
| 2591 |
+
|
| 2592 |
+
|
| 2593 |
def call_model(
|
| 2594 |
client: InferenceClient,
|
| 2595 |
messages: List[Dict[str, str]],
|
|
|
|
| 2604 |
model_order.append(m)
|
| 2605 |
|
| 2606 |
last_error = None
|
| 2607 |
+
# Per-model transient-retry budget. The Space's HTTP client (urllib3 pool
|
| 2608 |
+
# underneath huggingface_hub) caches a TLS connection to the public API;
|
| 2609 |
+
# Cloudflare's edge silently RSTs idle keepalive sockets, so the FIRST
|
| 2610 |
+
# request after an idle period frequently fails with ConnectionResetError
|
| 2611 |
+
# (errno 104). urllib3 evicts the bad connection on failure, so retrying
|
| 2612 |
+
# the same call lets the pool open a fresh socket — usually succeeds on
|
| 2613 |
+
# attempt 2. Three retries with mild backoff cover RST + a TLS hiccup.
|
| 2614 |
+
MAX_TRANSIENT_RETRIES = 3
|
| 2615 |
for model_name in model_order:
|
| 2616 |
+
for attempt in range(MAX_TRANSIENT_RETRIES + 1):
|
| 2617 |
+
try:
|
| 2618 |
+
completion = client.chat_completion(
|
| 2619 |
+
model=model_name,
|
| 2620 |
+
messages=messages,
|
| 2621 |
+
temperature=temperature,
|
| 2622 |
+
max_tokens=max_new_tokens,
|
| 2623 |
+
stop=["\n<tool_response>", "<tool_response>"],
|
| 2624 |
+
)
|
| 2625 |
+
return completion.choices[0].message.content or "", model_name
|
| 2626 |
+
except Exception as exc:
|
| 2627 |
+
last_error = exc
|
| 2628 |
+
if attempt < MAX_TRANSIENT_RETRIES and _is_transient_network_error(exc):
|
| 2629 |
+
time.sleep(0.3 * (2 ** attempt)) # 0.3 / 0.6 / 1.2 s
|
| 2630 |
+
continue
|
| 2631 |
+
break
|
| 2632 |
raise RuntimeError(f"All model candidates failed. Last error: {last_error}")
|
| 2633 |
|
| 2634 |
|