Repository navigation
perf(server): bound the audio request queue and add a per-request timeout - #381
Conversation
…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.
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.
PR Finalization CompleteSummaryTests: 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). |
…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.
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 unboundedmpsc::channelto a boundedmpsc::sync_channel(queue_depth);dispatchusestry_send(full queue maps to the newQueueFullerror);transcribe/synthesizeuserecv_timeout(request_timeout)(timeout maps to the newTimeouterror).spawngainsqueue_depthandrequest_timeoutparams and clamps a zero capacity to at least one queued command.Dropkeeps a blocking shutdown send, which is correct because the worker keeps draining.src/server/audio_model.rs: two newAudioModelErrorvariants,QueueFullandTimeout.src/server/routes/audio.rs:audio_model_error_responsemapsQueueFullto the shared503server_busyenvelope andTimeoutto a structured504server_timeout.src/server/types/response.rs: newErrorResponse::gateway_timeouthelper (504,server_timeout).src/server/whisper_stt.rs,src/server/kokoro_tts.rs: providerloadtakesqueue_depthandrequest_timeoutand forwards them toAudioWorker::spawn.max_queue_depth: newServerConfigfieldsaudio_queue_depth(default 8) andaudio_request_timeout_secs(default 120), wired throughServerStartupInput/ServerStartupConfig/build_server_configand read in the startup audio block (a0timeout falls back to the default). New flags--audio-queue-depth/--audio-request-timeout-secsand env varsMLXCEL_AUDIO_QUEUE_DEPTH/MLXCEL_AUDIO_REQUEST_TIMEOUT_SECSon bothmlxcel serveandmlxcel-server.queue_full_returns_queuefull_errorandrequest_timeout_returns_timeout_error(worker level, no real model, deterministic), plus route-level assertions thatQueueFullis503andTimeoutis504.504rows indocs/audio-api.md; new "Server audio admission variables" section indocs/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::configandcargo test --lib server::cli_inputandcargo test --lib server::startup(pass)cargo clippy --lib --tests -- -D warningsandcargo clippy --bins -- -D warnings(clean)cargo fmt --check(clean)Closes #373