mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-31 10:28:43 -04:00
AudioTranscriptionLive holds the model's inference lane for the whole stream,
which is correct (the streaming session is stateful and a concurrent run would
interleave two callers' audio) and newly dangerous. Every other RPC holds the
lane across compute, or across a write to a slow reader, and both of those
terminate on their own. A live stream instead blocks in a client-driven read,
and a peer that goes silent WITHOUT closing the stream never terminates
anything: the lane stays taken and every other request against that model queues
behind a client that stopped speaking.
live_watchdog is a one-shot idle timer that ends the stream when no frame has
arrived inside a window. It is standard library only, so it is unit tested
without an engine. gRPC's synchronous Read has no timeout and cannot be given
one, so the only way to unblock it is ServerContext::TryCancel, which decides
the wire status itself: the client sees CANCELLED rather than the
DEADLINE_EXCEEDED the handler returns, the reason is logged, and the lane coming
back is the point. When it fires the read loop throws rather than reporting
end-of-input, so the driver does not go on to finalize a decode nobody is
waiting for.
It is armed only after the lane is taken and disarmed as soon as the read side
closes, and both ends matter. Arming earlier would cover acquire(), which
legitimately blocks while another live stream runs, so a queued caller would be
cancelled for waiting its turn. Disarming later would cover our own decode,
where a window overrun is not a peer going quiet and cancelling would throw away
the transcript the client is waiting for.
The window is the new live_idle_timeout_ms option, 30 s by default, 0 meaning no
limit. core/http/endpoints/openai/realtime.go drives a 300 ms ticker and feeds
every tick that produced new audio while a turn is open, so 30 s of silence is a
hundred ticks that delivered nothing. It is also longer than any pause a speaker
takes mid-utterance, which is the case that must never be cut off, and
backend.proto lets one stream span many utterances, so a client that pauses
longer between them raises the option rather than discovering it.
Two smaller corrections in the same handler:
- check_can_serve now runs BEFORE the sample rate check.
pkg/grpc/grpcerrors/errors.go degrades to the file path on UNIMPLEMENTED and
on nothing else, so a live-incapable model asked at a wrong rate was
answering INVALID_ARGUMENT and costing the caller its fallback.
- a negative sample rate is refused instead of silently becoming 16000. Zero
still means 16000, which is what the proto documents; -1 is malformed rather
than absent and gets the same refusal every other bad rate gets.
And one thing recorded rather than changed, at the handler: "live" here means
incremental INPUT, not low latency, and with the pinned families it does not yet
mean incremental OUTPUT either. nemotron_asr's process_audio_chunk only appends
to its buffer, so its whole decode and every delta happen inside finalize(),
after the client closes its send side. The policy-window buffering is inert for
that family and matters only for vibevoice_asr and higgs_audio_stt.
Verified on the wire with live_idle_timeout_ms:3000. A silent client acked at
371 ms and was cancelled at 3.371 s; a second live stream opened one second
later received its ack 2.37 s in, i.e. at the instant the first was cancelled,
and then transcribed successfully on the same cached session. Without the
watchdog it would still be waiting. Re-ran the live transcription (ready first,
59 incremental deltas, concat equal to the final text, word timestamps in
nanoseconds, eou and eob false), the citrinet refusal at both a right and a
wrong rate (UNIMPLEMENTED either way now), and Task 12's AudioTranscriptionStream
on nemotron_asr, which is unchanged.
Mutation testing the watchdog found a weakness in its own test: the destructor
test slept past the window inside the watched scope, so a destructor that
DETACHED the thread instead of joining it passed unnoticed. The test now uses a
window longer than the scope, which kills that mutant, and says why.
Assisted-by: Claude:claude-opus-5 [Claude Code]
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
85 lines
3.6 KiB
C++
85 lines
3.6 KiB
C++
#pragma once
|
|
|
|
// A one-shot idle timer for a bidirectional stream. Standard library only, so
|
|
// it is tested without an audio.cpp checkout or a gRPC server.
|
|
//
|
|
// WHY IT EXISTS. AudioTranscriptionLive holds the model's inference lane for the
|
|
// whole stream, because the streaming session is stateful and a concurrent run
|
|
// would interleave two callers' audio. Every other RPC in this backend holds the
|
|
// lane across COMPUTE, or across a write to a slow reader, and both of those
|
|
// terminate on their own. A live stream instead blocks in a client-driven read,
|
|
// and a peer that goes silent WITHOUT closing the stream never terminates
|
|
// anything: the lane stays taken and every other RPC against that model queues
|
|
// behind a client that has stopped speaking. A websocket death does cancel the
|
|
// RPC and free it, but "the peer's TCP connection eventually dies" is not a
|
|
// bound anyone can state, so this supplies one.
|
|
//
|
|
// HOW IT ENDS THE STREAM, and the part that is not obvious: gRPC's synchronous
|
|
// ServerReaderWriter::Read has no timeout and cannot be given one. The only way
|
|
// to unblock it from another thread is ServerContext::TryCancel, which is what
|
|
// the callback is for. That means the client sees CANCELLED rather than whatever
|
|
// status the handler goes on to return: the returned status is for the server's
|
|
// own record. Releasing the lane is the point.
|
|
//
|
|
// ONE SHOT on purpose. Once the callback has run the stream is being torn down,
|
|
// so there is nothing left to watch, and a repeating timer would call TryCancel
|
|
// on a context the handler may already have returned from.
|
|
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
namespace audiocpp_backend {
|
|
|
|
class IdleWatchdog {
|
|
public:
|
|
// A window that is not positive DISABLES the watchdog entirely: no thread is
|
|
// started and fired() never becomes true. That is the operator's escape
|
|
// hatch for a client that legitimately holds a stream open through long
|
|
// pauses, and it is why the option carrying it documents 0 as "no limit"
|
|
// rather than as "expire immediately".
|
|
//
|
|
// `on_idle` runs on the watchdog's own thread with no lock held. It must be
|
|
// safe to call while the watched thread is blocked in a read, which is the
|
|
// only reason this class exists; ServerContext::TryCancel is documented as
|
|
// exactly that.
|
|
IdleWatchdog(std::chrono::milliseconds window, std::function<void()> on_idle);
|
|
|
|
// Joins the thread, so the callback can safely capture anything that
|
|
// outlives this object's scope and nothing else has to be reasoned about.
|
|
~IdleWatchdog();
|
|
|
|
IdleWatchdog(const IdleWatchdog &) = delete;
|
|
IdleWatchdog &operator=(const IdleWatchdog &) = delete;
|
|
|
|
// Restarts the window. Call it whenever the peer proves it is still there.
|
|
void touch();
|
|
|
|
// Stops watching and joins. Idempotent, and REQUIRED before any long
|
|
// non-read work the window must not cover: the caller's own decode is not
|
|
// the peer going quiet, and cancelling in the middle of it would throw away
|
|
// a transcript the client is waiting for.
|
|
void disarm();
|
|
|
|
// True once the window elapsed and the callback ran. Stays true after
|
|
// disarm, so the caller can tell "the peer closed" from "we cancelled it".
|
|
bool fired() const;
|
|
|
|
private:
|
|
void run();
|
|
|
|
const std::chrono::milliseconds window_;
|
|
std::function<void()> on_idle_;
|
|
|
|
mutable std::mutex mu_;
|
|
std::condition_variable cv_;
|
|
std::chrono::steady_clock::time_point last_;
|
|
bool stop_ = false;
|
|
bool fired_ = false;
|
|
std::thread thread_;
|
|
};
|
|
|
|
} // namespace audiocpp_backend
|