Skip to content

Commit 2add9c7

Browse files
authored
[serving] fix same behavior on win/linux (#435)
* [serving] refactor the api_llm_serving and add auto test for it * [pytest] ignore the dataflow main dir to avoid scaffold test * [serving] same behavior on win/linux
1 parent 38732df commit 2add9c7

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

dataflow/serving/api_llm_serving_request.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,40 @@ def _api_chat_with_id(
159159
# self.logger.exception(f"API request failed (id = {id}) with status {response.status_code}: {response.text}")
160160
self.logger.error(f"API request failed id={id} status={response.status_code} cost={cost:.2f}s body={response.text[:500]}")
161161
return id, None
162+
# ✅ 1) 连接阶段超时:认为“根本连不上” => 统一抛 RuntimeError(Win/Linux 一致)
163+
except requests.exceptions.ConnectTimeout as e:
164+
cost = time.time() - start
165+
self.logger.error(f"API connect timeout (id={id}) cost={cost:.2f}s: {e}")
166+
raise RuntimeError(f"Cannot connect to LLM server (connect timeout): {e}") from e
167+
168+
# ✅ 2) 读超时:服务可达但没有数据(排队/推理太久)=> warn + None
169+
except requests.exceptions.ReadTimeout as e:
170+
cost = time.time() - start
171+
warnings.warn(f"API read timeout (id={id}) cost={cost:.2f}s: {e}", RuntimeWarning)
172+
return id, None
173+
174+
# ✅ 3) 其他 Timeout(极少)按 warn 处理
162175
except requests.exceptions.Timeout as e:
163176
cost = time.time() - start
164177
warnings.warn(f"API timeout (id={id}) cost={cost:.2f}s: {e}", RuntimeWarning)
165178
return id, None
166179

180+
# ✅ 4) ConnectionError:这里面 Win/Linux/urllib3 可能包装了各种“超时/断开”
167181
except requests.exceptions.ConnectionError as e:
168182
cost = time.time() - start
169183
msg = str(e).lower()
170184

171185
# requests/urllib3 有时会把 ReadTimeout 包装成 ConnectionError
172-
if "timed out" in msg or "read timed out" in msg:
173-
warnings.warn(f"API timeout (id={id}) cost={cost:.2f}s: {e}", RuntimeWarning)
186+
if "read timed out" in msg:
187+
warnings.warn(f"API read timeout (id={id}) cost={cost:.2f}s: {e}", RuntimeWarning)
174188
return id, None
175189

190+
# 连接阶段超时在某些平台也可能表现为 ConnectionError 文本
191+
if "connect timeout" in msg or ("timed out" in msg and "connect" in msg):
192+
self.logger.error(f"API connect timeout (id={id}) cost={cost:.2f}s: {e}")
193+
raise RuntimeError(f"Cannot connect to LLM server (connect timeout): {e}") from e
194+
195+
# 其它连接错误:refused/reset/remote disconnected 等 => 统一抛 RuntimeError
176196
self.logger.error(f"API connection error (id={id}) cost={cost:.2f}s: {e}")
177197
raise RuntimeError(f"Cannot connect to LLM server: {e}") from e
178198

0 commit comments

Comments
 (0)