mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
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:
parent
d3c669d1f9
commit
7f477e3b29
1 changed files with 52 additions and 46 deletions
|
|
@ -171,59 +171,65 @@ 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())
|
||||||
await ws.send(
|
try:
|
||||||
json.dumps(
|
await ws.send(
|
||||||
{
|
json.dumps(
|
||||||
"type": "hello",
|
{
|
||||||
"version": 1,
|
"type": "hello",
|
||||||
"features": {"mcp": True},
|
"version": 1,
|
||||||
"transport": "websocket",
|
"features": {"mcp": True},
|
||||||
"audio_params": {
|
"transport": "websocket",
|
||||||
"format": "opus",
|
"audio_params": {
|
||||||
"sample_rate": SAMPLE_RATE,
|
"format": "opus",
|
||||||
"channels": 1,
|
"sample_rate": SAMPLE_RATE,
|
||||||
"frame_duration": 60,
|
"channels": 1,
|
||||||
|
"frame_duration": 60,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
ensure_ascii=False,
|
||||||
ensure_ascii=False,
|
)
|
||||||
)
|
)
|
||||||
)
|
log("hello_sent")
|
||||||
log("hello_sent")
|
await asyncio.sleep(0.6)
|
||||||
await asyncio.sleep(0.6)
|
await ws.send(
|
||||||
await ws.send(
|
json.dumps(
|
||||||
json.dumps(
|
{"session_id": state["session"], "type": "listen", "state": "start", "mode": "auto"},
|
||||||
{"session_id": state["session"], "type": "listen", "state": "start", "mode": "auto"},
|
ensure_ascii=False,
|
||||||
ensure_ascii=False,
|
)
|
||||||
)
|
)
|
||||||
)
|
log("listen_start")
|
||||||
log("listen_start")
|
|
||||||
|
|
||||||
next_at = time.monotonic()
|
next_at = time.monotonic()
|
||||||
sent = 0
|
sent = 0
|
||||||
for i in range(0, len(pcm) - FRAME_SAMPLES + 1, FRAME_SAMPLES):
|
for i in range(0, len(pcm) - FRAME_SAMPLES + 1, FRAME_SAMPLES):
|
||||||
await ws.send(enc.encode(pcm[i:i + FRAME_SAMPLES].tobytes(), FRAME_SAMPLES))
|
await ws.send(enc.encode(pcm[i:i + FRAME_SAMPLES].tobytes(), FRAME_SAMPLES))
|
||||||
sent += 1
|
sent += 1
|
||||||
next_at += 0.06
|
next_at += 0.06
|
||||||
delay = next_at - time.monotonic()
|
delay = next_at - time.monotonic()
|
||||||
if delay > 0:
|
if delay > 0:
|
||||||
await asyncio.sleep(delay)
|
await asyncio.sleep(delay)
|
||||||
log("frames_sent", n=sent)
|
log("frames_sent", n=sent)
|
||||||
|
|
||||||
await asyncio.sleep(args.tail_silence)
|
await asyncio.sleep(args.tail_silence)
|
||||||
await ws.send(
|
await ws.send(
|
||||||
json.dumps(
|
json.dumps(
|
||||||
{"session_id": state["session"], "type": "listen", "state": "stop", "mode": "auto"},
|
{"session_id": state["session"], "type": "listen", "state": "stop", "mode": "auto"},
|
||||||
ensure_ascii=False,
|
ensure_ascii=False,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
log("listen_stop")
|
||||||
log("listen_stop")
|
|
||||||
|
|
||||||
deadline = time.monotonic() + args.timeout
|
deadline = time.monotonic() + args.timeout
|
||||||
while time.monotonic() < deadline:
|
while time.monotonic() < deadline:
|
||||||
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)
|
||||||
rx_task.cancel()
|
finally:
|
||||||
|
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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue