Add server tool timeout protection

This commit is contained in:
Xin Wang
2026-02-10 19:17:45 +08:00
parent 6cac24918d
commit 2d7fc2b700
2 changed files with 60 additions and 1 deletions

View File

@@ -58,6 +58,7 @@ class DuplexPipeline:
_SENTENCE_CLOSERS = frozenset({'"', "'", "", "", ")", "]", "}", "", "", "", "", ""})
_MIN_SPLIT_SPOKEN_CHARS = 6
_TOOL_WAIT_TIMEOUT_SECONDS = 15.0
_SERVER_TOOL_TIMEOUT_SECONDS = 15.0
_DEFAULT_TOOL_SCHEMAS: Dict[str, Dict[str, Any]] = {
"search": {
"name": "search",
@@ -984,7 +985,18 @@ class DuplexPipeline:
tool_results.append(result)
continue
result = await execute_server_tool(call)
try:
result = await asyncio.wait_for(
execute_server_tool(call),
timeout=self._SERVER_TOOL_TIMEOUT_SECONDS,
)
except asyncio.TimeoutError:
result = {
"tool_call_id": call_id,
"name": self._tool_name(call) or "unknown_tool",
"output": {"message": "server tool timeout"},
"status": {"code": 504, "message": "server_tool_timeout"},
}
await self._emit_tool_result(result, source="server")
tool_results.append(result)