From a6cdbf941017cb40fba54f106880fda3c20b8728 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sat, 17 Oct 2026 07:22:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BB=BB=E5=8A=A1=E7=94=A8?= =?UTF-8?q?=20snapshot=20=E9=9A=94=E7=A6=BB=E8=B7=A8=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 旧 monitor 循环直接 self.ws.recv(),连接重建后旧任务读新下行;finally 无条件 关 self.ws 误关新连接。改成入口绑 monitor_ws / monitor_task,recv 用 snapshot; 旧 session 事件 continue 不再 reset 当前;finally 判 id 相同才关 ws / 清引用。 --- .../providers/tts/huoshan_double_stream.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py index 8d8223c87..501019668 100644 --- a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py +++ b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py @@ -509,11 +509,16 @@ class TTSProvider(TTSProviderBase): async def _start_monitor_tts_response(self): """监听TTS响应 - 长期运行""" + # 绑定本次任务的 ws / task 引用;清理前判 id,避免误关新连接 + monitor_ws = self.ws + monitor_task = asyncio.current_task() try: while not self.conn.stop_event.is_set(): try: + if monitor_ws is None: + break # 确保 `recv()` 运行在同一个 event loop - msg = await self.ws.recv() + msg = await monitor_ws.recv() res = self.parser_response(msg) self.print_response(res, "send_text res:") @@ -532,12 +537,8 @@ class TTSProvider(TTSProviderBase): res.optional.event == EVENT_SessionStarted ) - # 只处理当前活跃会话的响应 + # 只处理当前活跃会话的响应;旧会话事件不影响当前 activate_session if res.optional.sessionId and self.conn.sentence_id != res.optional.sessionId: - # 如果是会话结束相关事件,即使会话ID不匹配也要重置状态 - if res.optional.event in [EVENT_SessionCanceled, EVENT_SessionFailed, EVENT_SessionFinished]: - logger.bind(tag=TAG).debug(f"收到残余下行结束响应重置会话状态~~") - self.activate_session = False continue if res.optional.event == EVENT_SessionCanceled: @@ -589,17 +590,16 @@ class TTSProvider(TTSProviderBase): ) traceback.print_exc() break - # 连接异常时关闭WebSocket - if self.ws: + finally: + # 只关自己绑定的 ws;只在 self._monitor_task 仍是本任务时清空引用 + if self.ws is monitor_ws and self.ws is not None: try: await self.ws.close() except: pass self.ws = None - # 监听任务退出时清理引用 - finally: - self.activate_session = False - self._monitor_task = None + if self._monitor_task is monitor_task: + self._monitor_task = None async def send_event( self,