Skip to content

Commit eadf165

Browse files
author
zhansheng.lzs
committed
Fix end_session() to handle server errors immediately
The server currently does not support session.finish event for qwen3.5-omni-flash-realtime, returning an error instead of session.finished. Previously end_session() would wait the full timeout duration (20s) before falling back to ws.close(). Now the method detects server error events immediately and closes the connection without waiting, reducing wait time from 20s to <0.1s when the server rejects session.finish. Related to issue #140
1 parent 6475159 commit eadf165

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

dashscope/audio/qwen_omni/omni_realtime.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def __init__(
153153
self.metrics = []
154154
# Add event for synchronously waiting on connection close
155155
self.disconnect_event = None
156+
self._disconnect_error = None
156157

157158
def _generate_event_id(self):
158159
"""
@@ -350,6 +351,7 @@ def end_session(self, timeout: int = 20) -> None:
350351

351352
# create the event
352353
self.disconnect_event = threading.Event()
354+
self._disconnect_error = None
353355

354356
self.__send_str(
355357
json.dumps(
@@ -362,10 +364,14 @@ def end_session(self, timeout: int = 20) -> None:
362364

363365
# wait for the event to be set
364366
finish_success = self.disconnect_event.wait(timeout)
365-
# clear the event
367+
error = self._disconnect_error
366368
self.disconnect_event = None
369+
self._disconnect_error = None
367370

368-
# if the event is not set, close the connection
371+
# if the server returned an error or timed out, close the connection
372+
if error is not None:
373+
self.close()
374+
return
369375
if not finish_success:
370376
self.close()
371377
raise TimeoutError(
@@ -536,6 +542,14 @@ def _on_message( # pylint: disable=unused-argument,too-many-branches
536542
logger.info("[omni realtime] session finished")
537543
if self.disconnect_event is not None:
538544
self.disconnect_event.set()
545+
elif "error" == json_data.get("type"):
546+
if self.disconnect_event is not None:
547+
self._disconnect_error = json_data.get("error")
548+
logger.warning(
549+
"[omni realtime] error during end_session: %s",
550+
self._disconnect_error,
551+
)
552+
self.disconnect_event.set()
539553
if "response.created" == json_data["type"]:
540554
self.last_response_id = json_data["response"]["id"]
541555
self.last_response_create_time = time.time() * 1000

0 commit comments

Comments
 (0)