fix(performance-tester): cancel receiver task on all exit paths

Ensure the message receiver is cancelled and awaited in a finally block,
so a server-side close does not leave an orphan task or an unretrieved
exception.
This commit is contained in:
SecInsight 2026-10-29 07:47:59 +08:00
parent d3c669d1f9
commit 7f477e3b29

View file

@ -171,6 +171,7 @@ async def run(args) -> int:
log("msg", type=t, raw=json.dumps(msg, ensure_ascii=False)[:160]) log("msg", type=t, raw=json.dumps(msg, ensure_ascii=False)[:160])
rx_task = asyncio.create_task(rx()) rx_task = asyncio.create_task(rx())
try:
await ws.send( await ws.send(
json.dumps( json.dumps(
{ {
@ -223,7 +224,12 @@ async def run(args) -> int:
if state["tts"] and state["tts"][-1] == "stop": if state["tts"] and state["tts"][-1] == "stop":
break break
await asyncio.sleep(0.25) await asyncio.sleep(0.25)
finally:
rx_task.cancel() rx_task.cancel()
try:
await rx_task
except BaseException:
pass
except websockets.exceptions.InvalidStatus as e: except websockets.exceptions.InvalidStatus as e:
print(f"[device-sim] 握手失败:{e}(检查 url / 反向代理)") print(f"[device-sim] 握手失败:{e}(检查 url / 反向代理)")
return 2 return 2