fix(proxy): handle BrokenPipeError on client disconnect gracefully (#11)

main
Izzy Turtle 2026-04-26 15:38:34 +08:00 committed by GitHub
parent 96c889596f
commit 69366d8bd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 15 deletions

View File

@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "deepseek-cursor-proxy"
version = "0.1.0"
version = "0.1.1"
description = "Local OpenAI-compatible proxy for Cursor and DeepSeek thinking models"
readme = "README.md"
requires-python = ">=3.10"

View File

@ -297,12 +297,15 @@ class DeepSeekProxyHandler(BaseHTTPRequestHandler):
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode(
"utf-8"
)
try:
self.send_response(status)
self._send_cors_headers()
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
except (BrokenPipeError, ConnectionError) as exc:
LOG.warning("client disconnected before response could be sent: %s", exc)
def _send_models(self) -> None:
created = int(time.time())
@ -365,6 +368,7 @@ class DeepSeekProxyHandler(BaseHTTPRequestHandler):
body = read_response_body(exc)
if self.config.verbose:
log_bytes("upstream error body", body)
try:
self.send_response(exc.code)
self._send_cors_headers()
self.send_header(
@ -373,6 +377,8 @@ class DeepSeekProxyHandler(BaseHTTPRequestHandler):
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
except (BrokenPipeError, ConnectionError) as write_err:
LOG.warning("client disconnected before upstream error could be sent: %s", write_err)
def _proxy_regular_response(
self,