Skip to content

fix(xai): detect a silently dropped STT socket instead of hanging - #7364

Merged
tinalenguyen merged 1 commit into
livekit:mainfrom
sidxh:fix/xai-half-open
Sep 21, 2026
Merged

tinalenguyen merged 1 commit into
livekit:mainfrom
sidxh:fix/xai-half-open

Conversation

@sidxh

@sidxh sidxh commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

On a half-open connection (no FIN, no RST) the xAI STT SpeechStream sits on a dead socket forever: the session stays open, no transcripts arrive, and nothing is ever logged.

Two things cause it, same class as Deepgram v1 before #7206, Soniox in #7357 and Telnyx in #7359. _connect_ws opens the socket without heartbeat, so recv_task parks on ws.receive() and the retry in _main_task, which only runs when something raises, never gets a turn. This plugin has no keepalive task, so when audio stops nothing else touches the socket either. And a WSMsgType.ERROR (how a heartbeat timeout arrives) hit the "unexpected xAI message type" branch and was stepped over; the loop only ended because a CLOSED followed, raised as a generic "closed unexpectedly" with the reason on ws.exception() dropped.

This adds heartbeat=30.0 (parity with Deepgram and Muse) and raises a retryable APIConnectionError from the ERROR frame with ws.exception() as the cause, so _main_task reconnects deterministically and the log says why.

Verified against a local fake xAI server that goes dead 10s into each connection while keeping the TCP socket open, with a real SpeechStream pushing silence for 120s. On main the last transcript is at 9.2s, nothing is logged for the remaining 110s, and one socket is opened. With this change the plugin logs xAI connection lost (caused by ServerTimeoutError: No PONG received after 15.0 seconds) at 55.6s, reconnects, and transcripts resume at 56.9s; three sockets over the run. Two regression tests added in a new tests/test_plugin_xai_stt.py; both fail on main.

#7279 handles xAI's application-level {"type": "error"} message in the same recv_task; that is a different failure (the server tells you it is done) and the two changes are independent.

30s heartbeat means ~46s to detect, which is long for a live call. Kept it for parity; happy to lower it.

Closes #7363.

Add heartbeat=30.0 to the STT ws_connect and raise a retryable
APIConnectionError from recv_task on WSMsgType.ERROR, with ws.exception()
attached, so _main_task reconnects and the reason is logged. Same fix as
livekit#7206 (deepgram), livekit#7357 (soniox) and livekit#7359 (telnyx).
@sidxh
sidxh requested a review from a team as a code owner September 20, 2026 15:39

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@tinalenguyen tinalenguyen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for the PR!

@tinalenguyen
tinalenguyen merged commit d896c07 into livekit:main Sep 21, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xai STT: a silently dropped WebSocket is never detected

2 participants