Skip to content

perf(server): bound the audio request queue and add a per-request timeout - #381

Merged
inureyes merged 2 commits into
mainfrom
perf/issue-373-bound-audio-queue-timeout
Jun 21, 2026
Merged

inureyes merged 2 commits into
mainfrom
perf/issue-373-bound-audio-queue-timeout

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Bounds the OpenAI audio worker queue and adds a per-request reply timeout so the network-facing audio path cannot grow memory without bound or hang a caller. Both fixes live in the shared AudioWorker, so they cover the Whisper STT and Kokoro TTS paths together. Surfaced by the PR #371 security review (two MEDIUM availability risks: head-of-line blocking through one worker, and unbounded memory growth from queued 25 MiB payloads).

What changed

  • src/server/audio_worker.rs: command channel switched from an unbounded mpsc::channel to a bounded mpsc::sync_channel(queue_depth); dispatch uses try_send (full queue maps to the new QueueFull error); transcribe/synthesize use recv_timeout(request_timeout) (timeout maps to the new Timeout error). spawn gains queue_depth and request_timeout params and clamps a zero capacity to at least one queued command. Drop keeps a blocking shutdown send, which is correct because the worker keeps draining.
  • src/server/audio_model.rs: two new AudioModelError variants, QueueFull and Timeout.
  • src/server/routes/audio.rs: audio_model_error_response maps QueueFull to the shared 503 server_busy envelope and Timeout to a structured 504 server_timeout.
  • src/server/types/response.rs: new ErrorResponse::gateway_timeout helper (504, server_timeout).
  • src/server/whisper_stt.rs, src/server/kokoro_tts.rs: provider load takes queue_depth and request_timeout and forwards them to AudioWorker::spawn.
  • Config plumbing mirrored on max_queue_depth: new ServerConfig fields audio_queue_depth (default 8) and audio_request_timeout_secs (default 120), wired through ServerStartupInput/ServerStartupConfig/build_server_config and read in the startup audio block (a 0 timeout falls back to the default). New flags --audio-queue-depth / --audio-request-timeout-secs and env vars MLXCEL_AUDIO_QUEUE_DEPTH / MLXCEL_AUDIO_REQUEST_TIMEOUT_SECS on both mlxcel serve and mlxcel-server.
  • Tests: queue_full_returns_queuefull_error and request_timeout_returns_timeout_error (worker level, no real model, deterministic), plus route-level assertions that QueueFull is 503 and Timeout is 504.
  • Docs: new "Queue bound and per-request timeout" section and 504 rows in docs/audio-api.md; new "Server audio admission variables" section in docs/environment-variables.md.

Test plan

  • cargo check --lib --tests (clean)
  • cargo check --bins (CLI arg-struct wiring compiles)
  • cargo test --lib server::audio_worker (6 passed, incl. the two new tests)
  • cargo test --lib server::routes::audio (16 passed)
  • cargo test --lib server::config and cargo test --lib server::cli_input and cargo test --lib server::startup (pass)
  • cargo clippy --lib --tests -- -D warnings and cargo clippy --bins -- -D warnings (clean)
  • cargo fmt --check (clean)

Closes #373

…eout

The OpenAI audio endpoints dispatched work to one dedicated AudioWorker thread over an unbounded mpsc channel with no admission bound and no per-request timeout, so a burst could grow memory without bound (each queued command holds up to a 25 MiB STT payload) and a slow request could hang its caller. The PR #371 security review flagged both as MEDIUM availability risks.

Bound the worker command channel with mpsc::sync_channel and dispatch via try_send: a full queue is rejected with the new AudioModelError::QueueFull, mapped to the shared 503 server_busy envelope. Add a per-request reply timeout (recv_timeout): a stuck request returns the new AudioModelError::Timeout, mapped to a structured 504 (server_timeout). The timeout frees the caller's blocking thread without cancelling the in-flight MLX work, which the worker can only process one at a time. Both changes live in the shared AudioWorker, so they cover the Whisper STT and Kokoro TTS paths together.

Two new ServerConfig knobs, wired end to end like max_queue_depth: audio_queue_depth (default 8, flag --audio-queue-depth, env MLXCEL_AUDIO_QUEUE_DEPTH) and audio_request_timeout_secs (default 120, flag --audio-request-timeout-secs, env MLXCEL_AUDIO_REQUEST_TIMEOUT_SECS). A zero queue depth clamps to at least one queued command at the channel boundary; a zero timeout falls back to the default rather than timing out instantly.

Tests: queue_full_returns_queuefull_error and request_timeout_returns_timeout_error at the worker level (no real model), plus route-level assertions that QueueFull maps to 503 and Timeout to 504. Docs: new admission/timeout section in docs/audio-api.md and env-var rows in docs/environment-variables.md.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review labels Jun 21, 2026
A Duration::ZERO passed to spawn would cause recv_timeout to return Err(Timeout) before the worker thread can reply, rejecting every request. Add a clamp symmetric with the existing queue_depth.max(1) guard: a zero timeout falls back to the documented default (120 s) so the worker behaves correctly regardless of what the caller passes.

Add a focused unit test (zero_request_timeout_falls_back_to_default) that passes Duration::ZERO and verifies a fast request still completes.
@inureyes

Copy link
Copy Markdown
Member Author

PR Finalization Complete

Summary

Tests: 7 audio_worker tests pass (including the new `zero_request_timeout_falls_back_to_default`); 16 audio route tests pass. All existing coverage for the bounded-queue 503 and per-request timeout 504 paths is present and deterministic.

Code change (commit 2cf2555): Added a `Duration::ZERO` self-guard inside `AudioWorker::spawn`, symmetric with the existing `queue_depth.max(1)` clamp. A zero timeout would make `recv_timeout` return `Err(Timeout)` before the worker thread can reply, rejecting every request. The guard falls back to the documented default (120 s) so the worker is correct in isolation regardless of what the caller passes. Added a focused unit test (`zero_request_timeout_falls_back_to_default`) that passes `Duration::ZERO` and verifies a fast request completes normally.

Documentation: `docs/audio-api.md` and `docs/environment-variables.md` accurately reflect both knobs: `--audio-queue-depth` / `MLXCEL_AUDIO_QUEUE_DEPTH` (default 8) and `--audio-request-timeout-secs` / `MLXCEL_AUDIO_REQUEST_TIMEOUT_SECS` (default 120), with the head-of-line and memory rationale. No changes needed.

Lint/Format: `cargo clippy --lib --tests -- -D warnings` clean; `cargo fmt` applied (reformatted the new block).

@inureyes inureyes added status:done Completed and removed status:review Under review labels Jun 21, 2026
@inureyes
inureyes merged commit 80f77d9 into main Jun 21, 2026
4 checks passed
@inureyes
inureyes deleted the perf/issue-373-bound-audio-queue-timeout branch June 21, 2026 11:18
@inureyes inureyes added this to the 0.3 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…eout (lablup#381)

* perf(server): bound the audio request queue and add a per-request timeout

The OpenAI audio endpoints dispatched work to one dedicated AudioWorker thread over an unbounded mpsc channel with no admission bound and no per-request timeout, so a burst could grow memory without bound (each queued command holds up to a 25 MiB STT payload) and a slow request could hang its caller. The PR lablup#371 security review flagged both as MEDIUM availability risks.

Bound the worker command channel with mpsc::sync_channel and dispatch via try_send: a full queue is rejected with the new AudioModelError::QueueFull, mapped to the shared 503 server_busy envelope. Add a per-request reply timeout (recv_timeout): a stuck request returns the new AudioModelError::Timeout, mapped to a structured 504 (server_timeout). The timeout frees the caller's blocking thread without cancelling the in-flight MLX work, which the worker can only process one at a time. Both changes live in the shared AudioWorker, so they cover the Whisper STT and Kokoro TTS paths together.

Two new ServerConfig knobs, wired end to end like max_queue_depth: audio_queue_depth (default 8, flag --audio-queue-depth, env MLXCEL_AUDIO_QUEUE_DEPTH) and audio_request_timeout_secs (default 120, flag --audio-request-timeout-secs, env MLXCEL_AUDIO_REQUEST_TIMEOUT_SECS). A zero queue depth clamps to at least one queued command at the channel boundary; a zero timeout falls back to the default rather than timing out instantly.

Tests: queue_full_returns_queuefull_error and request_timeout_returns_timeout_error at the worker level (no real model), plus route-level assertions that QueueFull maps to 503 and Timeout to 504. Docs: new admission/timeout section in docs/audio-api.md and env-var rows in docs/environment-variables.md.

* fix(audio): guard Duration::ZERO in AudioWorker::spawn

A Duration::ZERO passed to spawn would cause recv_timeout to return Err(Timeout) before the worker thread can reply, rejecting every request. Add a clamp symmetric with the existing queue_depth.max(1) guard: a zero timeout falls back to the documented default (120 s) so the worker behaves correctly regardless of what the caller passes.

Add a focused unit test (zero_request_timeout_falls_back_to_default) that passes Duration::ZERO and verifies a fast request still completes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(server): bound the audio request queue and add a per-request timeout

1 participant