v2.2.1: fix NameError _ts crash + catch stream disconnect errors

- Fix NameError: _ts undefined in crof debug logging (caused ALL requests to crash)
- Catch ConnectionResetError/BrokenPipeError during streaming (graceful client disconnect)
- Tested: kimi-k2.6 + mimo-v2.5-pro streaming through proxy, both status=completed
This commit is contained in:
Roman
2026-05-20 13:54:47 +04:00
Unverified
parent 0a3cd3fd7a
commit 881f4f35d2
2 changed files with 18 additions and 16 deletions

Binary file not shown.

View File

@@ -918,12 +918,11 @@ class Handler(http.server.BaseHTTPRequestHandler):
_crof_debug_path = os.path.join(_LOG_DIR, "crof-upstream.jsonl") _crof_debug_path = os.path.join(_LOG_DIR, "crof-upstream.jsonl")
with open(_crof_debug_path, "a") as _cdf: with open(_crof_debug_path, "a") as _cdf:
_cdf.write(json.dumps({ _cdf.write(json.dumps({
"ts": _ts, "model": model, "max_tokens": chat_body.get("max_tokens"), "model": model, "max_tokens": chat_body.get("max_tokens"),
"reasoning_enabled": REASONING_ENABLED, "reasoning_effort": chat_body.get("reasoning_effort"), "reasoning_effort": chat_body.get("reasoning_effort"),
"enable_thinking": chat_body.get("enable_thinking", "NOT_SENT"), "enable_thinking": chat_body.get("enable_thinking", "NOT_SENT"),
"n_messages": len(chat_body.get("messages", [])), "n_messages": len(chat_body.get("messages", [])),
"has_tools": bool(chat_body.get("tools")), "has_tools": bool(chat_body.get("tools")),
"messages_summary": [{"role": m.get("role"), "tc": len(m.get("tool_calls", [])), "content_len": len(str(m.get("content", "")))[:6], "tool_call_id": m.get("tool_call_id")} for m in chat_body.get("messages", [])],
}) + "\n") }) + "\n")
req = urllib.request.Request( req = urllib.request.Request(
target, target,
@@ -1098,19 +1097,22 @@ class Handler(http.server.BaseHTTPRequestHandler):
last_resp_id = None last_resp_id = None
last_output = None last_output = None
last_status = None last_status = None
for event in stream_fn(upstream): try:
self.wfile.write(event.encode("utf-8")) for event in stream_fn(upstream):
self.wfile.flush() self.wfile.write(event.encode("utf-8"))
for line in event.strip().split("\n"): self.wfile.flush()
if line.startswith("data: "): for line in event.strip().split("\n"):
try: if line.startswith("data: "):
d = json.loads(line[6:]) try:
if d.get("type") == "response.completed": d = json.loads(line[6:])
last_resp_id = d.get("response", {}).get("id") if d.get("type") == "response.completed":
last_output = d.get("response", {}).get("output", []) last_resp_id = d.get("response", {}).get("id")
last_status = d.get("response", {}).get("status") last_output = d.get("response", {}).get("output", [])
except: pass last_status = d.get("response", {}).get("status")
_log_resp(last_resp_id, last_status, last_output) except: pass
except (ConnectionResetError, BrokenPipeError, ConnectionAbortedError):
print("[translate-proxy] client disconnected during stream", file=sys.stderr)
_log_resp(last_resp_id, last_status or "client_disconnect", last_output)
if last_resp_id and input_data is not None: if last_resp_id and input_data is not None:
store_response(last_resp_id, input_data, last_output) store_response(last_resp_id, input_data, last_output)
else: else: