Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: tolerate process probe decoding errors and avoid spurious LLM retries

OrdoAbChao78b03d2ab5613
diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.py
index 4d5568a62..76cb6564c 100644
--- a/deeptutor/runtime/launcher.py
+++ b/deeptutor/runtime/launcher.py
@@ -345,12 +345,13 @@ def _port_listeners(port: int) -> list[tuple[int, str]]:
check=False,
capture_output=True,
text=True,
+ errors="replace",
timeout=3,
)
except Exception:
return []
pids: list[int] = []
- for line in completed.stdout.splitlines():
+ for line in (completed.stdout or "").splitlines():
if not line.startswith("p"):
continue
try:
@@ -372,13 +373,14 @@ def _port_listeners_windows(port: int) -> list[tuple[int, str]]:
check=False,
capture_output=True,
text=True,
+ errors="replace",
timeout=5,
**_no_window_kwargs(),
)
except Exception:
return []
pids: list[int] = []
- for line in completed.stdout.splitlines():
+ for line in (completed.stdout or "").splitlines():
parts = line.split()
if len(parts) < 5 or parts[0].upper() != "TCP" or parts[3].upper() != "LISTENING":
continue
@@ -401,10 +403,11 @@ def _port_listeners_windows(port: int) -> list[tuple[int, str]]:
check=False,
capture_output=True,
text=True,
+ errors="replace",
timeout=3,
**_no_window_kwargs(),
)
- first = result.stdout.strip().splitlines()[:1]
+ first = (result.stdout or "").strip().splitlines()[:1]
if first and first[0].startswith('"'):
name = first[0].split('","')[0].strip('"')
except Exception:
@@ -971,11 +974,12 @@ def _process_command(pid: int | None) -> str:
check=False,
capture_output=True,
text=True,
+ errors="replace",
timeout=2,
)
except Exception:
return ""
- return completed.stdout.strip()
+ return (completed.stdout or "").strip()
def _looks_like_next_process(pid: int | None) -> bool:
diff --git a/deeptutor/services/llm/provider_core/base.py b/deeptutor/services/llm/provider_core/base.py
index 2a53423df..964e90e76 100644
--- a/deeptutor/services/llm/provider_core/base.py
+++ b/deeptutor/services/llm/provider_core/base.py
@@ -8,6 +8,7 @@ from collections.abc import Awaitable, Callable, Sequence
from dataclasses import dataclass, field
import inspect
import json
+import re
from typing import Any
from loguru import logger
@@ -74,12 +75,7 @@ class LLMProvider(ABC):
_CHAT_RETRY_DELAYS = (1, 2, 4)
_TRANSIENT_ERROR_MARKERS = (
- "429",
"rate limit",
- "500",
- "502",
- "503",
- "504",
"overloaded",
"timeout",
"timed out",
@@ -273,7 +269,11 @@ class LLMProvider(ABC):
@classmethod
def _is_transient_error(cls, content: str | None) -> bool:
err = (content or "").lower()
- return any(marker in err for marker in cls._TRANSIENT_ERROR_MARKERS)
+ # Gateway errors often embed opaque request IDs. Matching bare status
+ # substrings misclassifies permanent errors when an ID contains "500".
+ return bool(re.search(r"\b(?:429|500|502|503|504)\b", err)) or any(
+ marker in err for marker in cls._TRANSIENT_ERROR_MARKERS
+ )
async def _call_with_retry(
self,
diff --git a/tests/runtime/test_launcher.py b/tests/runtime/test_launcher.py
index c4a8dea15..23729cabc 100644
--- a/tests/runtime/test_launcher.py
+++ b/tests/runtime/test_launcher.py
@@ -878,3 +878,52 @@ def test_windows_children_and_console_tools_allocate_no_console(
class _NoopThread:
def start(self) -> None:
return None
+
+
[email protected]("probe", ["netstat", "tasklist", "lsof", "ps"])
+def test_process_probes_tolerate_non_utf8_output(monkeypatch, probe: str) -> None:
+ """A localized utility must not crash or hide a listener in UTF-8 mode."""
+ import subprocess
+ import sys
+
+ run = subprocess.run
+ outputs = {
+ "netstat": b"\xbb localized heading\n TCP 127.0.0.1:3782 0.0.0.0:0 LISTENING 123\n",
+ "tasklist": b'"python.exe","123","\xbb session","1","0 K"\n',
+ "lsof": b"\xbb diagnostic\np123\n",
+ "ps": b"node next-server \xbb\n",
+ }
+
+ for name in outputs:
+ if name != probe:
+ outputs[name] = outputs[name].replace(b"\xbb", b"localized")
+
+ def run_probe(args, **kwargs):
+ # Use a real child pipe, with UTF-8 decoding as in the reported crash.
+ kwargs["encoding"] = "utf-8"
+ return run(
+ [sys.executable, "-c", f"import sys; sys.stdout.buffer.write({outputs[args[0]]!r})"],
+ **kwargs,
+ )
+
+ monkeypatch.setattr(launcher.shutil, "which", lambda name: name)
+ monkeypatch.setattr(launcher.subprocess, "run", run_probe)
+ if probe in {"netstat", "tasklist"}:
+ assert launcher._port_listeners_windows(3782) == [(123, "python.exe")]
+ else:
+ monkeypatch.setattr(launcher, "os", SimpleNamespace(name="posix"))
+ if probe == "lsof":
+ assert launcher._port_listeners(3782) == [(123, "node next-server localized")]
+ else:
+ assert launcher._process_command(123) == "node next-server \ufffd"
+
+
[email protected]("platform", ["nt", "posix"])
+def test_port_listeners_tolerate_missing_stdout(monkeypatch, platform: str) -> None:
+ """A failed subprocess reader may leave stdout unset on Windows."""
+ monkeypatch.setattr(launcher, "os", SimpleNamespace(name=platform))
+ monkeypatch.setattr(launcher.shutil, "which", lambda name: name)
+ monkeypatch.setattr(
+ launcher.subprocess, "run", lambda *args, **kwargs: SimpleNamespace(stdout=None)
+ )
+ assert launcher._port_listeners(3782) == []
diff --git a/tests/services/llm/test_provider_retry_classification.py b/tests/services/llm/test_provider_retry_classification.py
new file mode 100644
index 000000000..39fa38f55
--- /dev/null
+++ b/tests/services/llm/test_provider_retry_classification.py
@@ -0,0 +1,66 @@
+"""Retry decisions must not interpret digits embedded in request IDs as HTTP statuses."""
+
+from __future__ import annotations
+
+from typing import Any
+
+import pytest
+
+from deeptutor.services.llm.provider_core.base import LLMProvider, LLMResponse
+
+
+class _FailingProvider(LLMProvider):
+ def __init__(self, error: str) -> None:
+ super().__init__()
+ self.error = error
+ self.calls = 0
+
+ async def chat(self, messages: list[dict[str, Any]], **kwargs: Any) -> LLMResponse:
+ self.calls += 1
+ return LLMResponse(content=self.error, finish_reason="error")
+
+ def get_default_model(self) -> str:
+ return "test-model"
+
+
+ "error",
+ [
+ "credit insufficient balance: balance=0 required=118 (request id: req202601010005000123)",
+ "invalid API key (request id: abc429def)",
+ "invalid model (request id: 1502)",
+ "invalid parameter (request id: trace503end)",
+ "invalid request (request id: 1504)",
+ ],
+)
+async def test_permanent_error_with_status_digits_is_not_retried(error: str) -> None:
+ provider = _FailingProvider(error)
+ response = await provider.chat_with_retry(
+ messages=[{"role": "user", "content": "hello"}], retry_delays=(0.001,)
+ )
+ assert response.content == error
+ assert provider.calls == 1
+
+
+ "error",
+ [
+ "Error code: 429 - rate limit exceeded",
+ "Error code: 500 - internal error",
+ 'Error: {"code":502,"message":"upstream failure"}',
+ "HTTP 503: unavailable",
+ "504 Gateway Timeout",
+ "request timed out",
+ "connection reset by peer",
+ "stream stalled",
+ ],
+)
+async def test_transient_errors_still_retry(error: str) -> None:
+ provider = _FailingProvider(error)
+ response = await provider.chat_with_retry(
+ messages=[{"role": "user", "content": "hello"}], retry_delays=(0.001,)
+ )
+ assert response.content == error
+ assert provider.calls == 2