Skip to content

Register client disconnects while streaming TTS audio.#634

Merged
Blaizzy merged 3 commits into
Blaizzy:mainfrom
orbitalquark:register-server-disconnect
Apr 14, 2026
Merged

Register client disconnects while streaming TTS audio.#634
Blaizzy merged 3 commits into
Blaizzy:mainfrom
orbitalquark:register-server-disconnect

Conversation

@orbitalquark
Copy link
Copy Markdown
Contributor

No need to continue generating for clients that have hung up. This reduces latency processing the next request.

I have a client application that splits long-form text into paragraphs and feeds each paragraph to an mlx-audio server sequentially. The client has a "next" button that stops listening for the current paragraph and POSTs the next one for playback. However, without disconnect detection, the server continues generating the previous request and tries sending to the disconnected client, resulting in unnecessary latency. This PR fixes that.

No need to continue generating for clients that have hung up.
This reduces latency processing the next request.
@orbitalquark orbitalquark changed the title Register server disconnects while streaming TTS audio. Register client disconnects while streaming TTS audio. Apr 5, 2026
@orbitalquark orbitalquark marked this pull request as ready for review April 5, 2026 16:20
Comment thread mlx_audio/server.py
if sample_rate is None:
sample_rate = result.sample_rate

await asyncio.sleep(0) # register any disconnects
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This can cause the server performance to drop significantly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Blaizzy I don't know how else to allow Python to temporarily pass control back to fastapi to register client disconnects. If I remove that line, this PR will do nothing.

For what it's worth, I streamed generated audio for over an hour with a stream interval of 0.5 on a MacBook Air. No hiccups at all. If the server performance had dropped significantly, then normal performance might be overkill.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Ok, let's merge for now and revisit it later.

Could you open a github issue with describing the issue and preferably a video so I we can reproduce and fix it later without using asyncio.sleep(0)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Owner

@Blaizzy Blaizzy left a comment

Choose a reason for hiding this comment

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

Mostly looks good to me, just have one nit before we merge

@orbitalquark
Copy link
Copy Markdown
Contributor Author

See #634 (comment).

@orbitalquark orbitalquark requested a review from Blaizzy April 12, 2026 02:46
@Blaizzy Blaizzy merged commit aa6cf84 into Blaizzy:main Apr 14, 2026
11 of 12 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.

2 participants