From 87eeeb6294d0e79c84658e5c5c5a2e1e919fb985 Mon Sep 17 00:00:00 2001 From: Neko Ayaka Date: Sat, 20 Dec 2025 03:39:46 +0800 Subject: [PATCH] fix(stage-ui): Aliyun NLS will data race on cleanup to let WebSocket close before TranscriptionEnded --- .../providers/aliyun/stream-transcription.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts b/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts index 8792d57cc..e1759570e 100644 --- a/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts +++ b/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts @@ -179,17 +179,24 @@ async function startRealtimeSession(options: InternalRealtimeOptions): Promise await reader.cancel()) - if (websocket) { - if (websocket.readyState === WebSocket.OPEN) { + if (websocket && closeSocket) { + switch (websocket.readyState) { + case WebSocket.OPEN: + if (sendStop) mayThrow(() => session.stop(websocket)) websocket.close(1000, 'client closed') - } - else { - mayThrow(() => websocket?.close()) + break + case WebSocket.CONNECTING: + websocket.close(1000, 'client closed') + break + default: + // If the server has already initiated closure, avoid sending another close frame. + break } } @@ -235,7 +242,7 @@ async function startRealtimeSession(options: InternalRealtimeOptions): Promise