From 3c59c46551d48c2ca310d3044eedc5b17ef87df8 Mon Sep 17 00:00:00 2001 From: Brad Shannon Date: Wed, 17 Jun 2026 17:57:12 -0400 Subject: [PATCH] fix(core): bound blocking future.result() calls so a hung dependency can't freeze the connection Two cross-thread future.result() calls had no timeout, so a stalled dependency blocked the thread indefinitely with no recovery: - connection.py: a hung memory backend froze the chat thread during query_memory(). - tts/base.py: a stalled WebSocket send froze the audio priority thread in sendAudioMessage(). Add a 30s timeout and explicit TimeoutError handling at both sites. On timeout the failure is logged and the loop proceeds (memory falls back to None; the audio frame is skipped) instead of deadlocking. The tool-call fan-in already uses tool_call_timeout, so it is left unchanged. --- main/xiaozhi-server/core/connection.py | 10 ++++++++-- main/xiaozhi-server/core/providers/tts/base.py | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 8772180c7..a850947b6 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -27,7 +27,7 @@ from core.utils.modules_initialize import ( ) from core.handle.reportHandle import report, enqueue_tool_report from core.providers.tts.default import DefaultTTS -from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeoutError from core.utils.dialogue import Message, Dialogue from core.providers.asr.dto.dto import InterfaceType from core.handle.textHandle import handleTextMessage @@ -976,7 +976,13 @@ class ConnectionHandler: future = asyncio.run_coroutine_threadsafe( self.memory.query_memory(query), self.loop ) - memory_str = future.result() + try: + memory_str = future.result(timeout=30) + except FutureTimeoutError: + self.logger.bind(tag=TAG).error( + "Memory query timed out after 30s" + ) + memory_str = None if self.intent_type == "function_call" and functions is not None: # 使用支持functions的streaming接口 diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index f9b2e02e1..db57d65c9 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -459,7 +459,12 @@ class TTSProviderBase(ABC): sendAudioMessage(self.conn, sentence_type, audio_datas, text, sentence_id), self.conn.loop, ) - future.result() + try: + future.result(timeout=30) + except concurrent.futures.TimeoutError: + logger.bind(tag=TAG).error( + "sendAudioMessage timed out after 30s — skipping frame" + ) # 记录输出和报告 if self.conn.max_output_size > 0 and text: