fix: cancel pipeline on peer disconnect

This commit is contained in:
Xin Wang
2026-08-05 14:34:20 +08:00
parent 0456d2b4ae
commit 0e9a1bb857
2 changed files with 56 additions and 6 deletions
+8 -5
View File
@@ -7,7 +7,7 @@ from loguru import logger
from pipecat.frames.frames import (
BotStartedSpeakingFrame,
BotStoppedSpeakingFrame,
EndFrame,
CancelFrame,
OutputTransportMessageUrgentFrame,
TTSSpeakFrame,
)
@@ -218,8 +218,11 @@ def bind_cascade_pipeline_events(
@transport.event_handler("on_client_disconnected")
async def on_client_disconnected(_transport, _client):
logger.info("对端断开,结束管线")
await worker.queue_frame(EndFrame())
# The peer can no longer consume queued audio. EndFrame would wait for
# graceful playback and can deadlock on SmallWebRTC's pending audio
# future, so a transport disconnect must stop output immediately.
logger.info("对端断开,立即取消管线")
await worker.queue_frame(CancelFrame(reason="client_disconnected"))
def bind_realtime_pipeline_events(
@@ -296,5 +299,5 @@ def bind_realtime_pipeline_events(
@transport.event_handler("on_client_disconnected")
async def on_client_disconnected(_transport, _client):
logger.info("Realtime 对端断开,结束管线")
await worker.queue_frame(EndFrame())
logger.info("Realtime 对端断开,立即取消管线")
await worker.queue_frame(CancelFrame(reason="client_disconnected"))