fix: 停止帧发送后与原先音频时序问题

回退退出工具打断处理(在大模型思考期间进行打断,然后大模型立马调用退出工具会造成死锁,因模型思考时间和是否调用工具不确定性,状态难管理)
This commit is contained in:
Sakura-RanChen
2026-04-22 15:55:28 +08:00
parent 4acd02e2b0
commit 8c18e48b97
7 changed files with 5 additions and 25 deletions
@@ -84,12 +84,6 @@ class ASRProviderBase(ABC):
async def handle_voice_stop(self, conn: "ConnectionHandler", asr_audio_task: List[bytes]):
"""并行处理ASR和声纹识别"""
try:
# 如果处于退出流程中,直接关闭连接,不处理新消息
if conn.close_after_chat or conn.is_exiting:
logger.bind(tag=TAG).info("退出流程中收到新消息,直接关闭连接")
await conn.close()
return
total_start_time = time.monotonic()
# 准备音频数据
@@ -26,6 +26,7 @@ class ASRProvider(ASRProviderBase):
self.asr_ws = None
self.forward_task = None
self.is_processing = False # 添加处理状态标志
self._is_stopping = False # 添加停止标志,防止竞态条件
# 配置参数
self.appid = str(config.get("appid"))
@@ -148,7 +149,7 @@ class ASRProvider(ASRProviderBase):
return
# 发送当前音频数据
if self.asr_ws and self.is_processing:
if self.asr_ws and self.is_processing and not self._is_stopping:
try:
pcm_frame = self.decoder.decode(audio, 960)
payload = gzip.compress(pcm_frame)
@@ -256,6 +257,7 @@ class ASRProvider(ASRProviderBase):
await self.asr_ws.close()
self.asr_ws = None
self.is_processing = False
self._is_stopping = False
# 重置所有音频相关状态
conn.reset_audio_states()
@@ -264,9 +266,11 @@ class ASRProvider(ASRProviderBase):
asyncio.create_task(self.asr_ws.close())
self.asr_ws = None
self.is_processing = False
self._is_stopping = False
async def _send_stop_request(self):
"""发送最后一个音频帧以通知服务器结束"""
self._is_stopping = True # 先标记为停止状态,阻止后续音频发送
if self.asr_ws:
try:
# 发送结束标记的音频帧(gzip压缩的空数据)