mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-31 02:18:50 -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>
160 lines
6.1 KiB
C++
160 lines
6.1 KiB
C++
// Unit tests for the live stream idle watchdog. Standard library only; the
|
|
// harness compiles this as a single translation unit, so the implementation is
|
|
// included directly.
|
|
//
|
|
// These are TIMING tests, which is unavoidable: what is under test is a
|
|
// deadline. Every window here is short and every assertion waits several
|
|
// multiples of it, so a loaded machine slows the test down rather than
|
|
// flipping its answer. The one thing never asserted is how SOON something
|
|
// happens, only that it eventually does or never does.
|
|
|
|
#include "live_watchdog.cpp"
|
|
|
|
#include <atomic>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
static int failures = 0;
|
|
|
|
static void check(bool ok, const std::string &name) {
|
|
if (!ok) {
|
|
failures++;
|
|
fprintf(stderr, "FAIL: %s\n", name.c_str());
|
|
} else {
|
|
fprintf(stderr, "ok: %s\n", name.c_str());
|
|
}
|
|
}
|
|
|
|
// A peer that goes quiet without closing. This is the whole point: the lane it
|
|
// holds has to come back.
|
|
static void test_it_fires_when_nothing_touches_it() {
|
|
std::atomic<int> calls{0};
|
|
audiocpp_backend::IdleWatchdog watchdog(100ms, [&calls] { ++calls; });
|
|
std::this_thread::sleep_for(600ms);
|
|
check(watchdog.fired(), "a window that elapses untouched fires");
|
|
check(calls.load() == 1, "the callback runs exactly once, not once per window");
|
|
}
|
|
|
|
// A peer that is still speaking must never be cut off. Touches land at a third
|
|
// of the window, for six windows' worth of wall clock.
|
|
static void test_touching_defers_it_indefinitely() {
|
|
std::atomic<int> calls{0};
|
|
audiocpp_backend::IdleWatchdog watchdog(300ms, [&calls] { ++calls; });
|
|
for (int i = 0; i < 20; ++i) {
|
|
std::this_thread::sleep_for(100ms);
|
|
watchdog.touch();
|
|
}
|
|
check(!watchdog.fired(),
|
|
"a stream touched inside every window is never cancelled");
|
|
check(calls.load() == 0, "no callback runs while the peer is still there");
|
|
}
|
|
|
|
// Disarm is what the handler calls when the read side closes, before a decode
|
|
// that can take longer than the window. Firing after that would throw away the
|
|
// transcript the client is waiting for.
|
|
static void test_disarm_stops_it_before_the_window() {
|
|
std::atomic<int> calls{0};
|
|
audiocpp_backend::IdleWatchdog watchdog(200ms, [&calls] { ++calls; });
|
|
std::this_thread::sleep_for(20ms);
|
|
watchdog.disarm();
|
|
std::this_thread::sleep_for(600ms);
|
|
check(!watchdog.fired(), "a disarmed watchdog does not fire");
|
|
check(calls.load() == 0, "a disarmed watchdog runs no callback");
|
|
}
|
|
|
|
static void test_disarm_is_idempotent() {
|
|
audiocpp_backend::IdleWatchdog watchdog(50ms, [] {});
|
|
watchdog.disarm();
|
|
watchdog.disarm();
|
|
watchdog.disarm();
|
|
check(true, "disarming three times joins once and does not abort");
|
|
}
|
|
|
|
// The operator's escape hatch, for a client that legitimately holds a stream
|
|
// open through long pauses. Not "expire immediately", which is what a naive
|
|
// reading of a zero timeout would give.
|
|
static void test_a_non_positive_window_disables_it() {
|
|
std::atomic<int> calls{0};
|
|
{
|
|
audiocpp_backend::IdleWatchdog watchdog(0ms, [&calls] { ++calls; });
|
|
std::this_thread::sleep_for(300ms);
|
|
check(!watchdog.fired(), "a zero window never fires");
|
|
}
|
|
{
|
|
audiocpp_backend::IdleWatchdog watchdog(-5ms, [&calls] { ++calls; });
|
|
std::this_thread::sleep_for(300ms);
|
|
check(!watchdog.fired(), "a negative window never fires");
|
|
}
|
|
check(calls.load() == 0, "a disabled watchdog runs no callback");
|
|
}
|
|
|
|
// fired() has to survive the disarm, because the handler reads it AFTER the
|
|
// read loop ends to tell "the peer closed" from "we cancelled the peer", and
|
|
// those two get different statuses.
|
|
static void test_fired_survives_a_later_disarm() {
|
|
audiocpp_backend::IdleWatchdog watchdog(80ms, [] {});
|
|
std::this_thread::sleep_for(500ms);
|
|
watchdog.disarm();
|
|
check(watchdog.fired(), "a watchdog that fired still says so after disarm");
|
|
}
|
|
|
|
// The destructor joins, so a callback capturing the handler's frame cannot run
|
|
// after that frame is gone. Without the join this is a use after free that only
|
|
// shows up under load.
|
|
//
|
|
// The window is LONGER than the scope on purpose. An earlier version of this
|
|
// test slept past the window inside the scope, so the callback had already run
|
|
// by the time the object was destroyed and a destructor that DETACHED the thread
|
|
// instead of joining it passed unnoticed. Mutation testing is what found that;
|
|
// the shape below kills it, because a detached thread wakes after the object is
|
|
// gone and calls a callback that must never run.
|
|
static void test_the_destructor_joins() {
|
|
std::atomic<int> calls{0};
|
|
std::atomic<bool> alive{true};
|
|
{
|
|
audiocpp_backend::IdleWatchdog watchdog(200ms, [&calls, &alive] {
|
|
check(alive.load(),
|
|
"the callback never runs after the watched scope ended");
|
|
++calls;
|
|
});
|
|
std::this_thread::sleep_for(20ms);
|
|
}
|
|
alive.store(false);
|
|
std::this_thread::sleep_for(600ms);
|
|
check(calls.load() == 0,
|
|
"destruction stops the timer rather than leaving it running against a "
|
|
"dead frame");
|
|
}
|
|
|
|
// The other half of that pair: a callback that DOES fire inside the scope runs
|
|
// exactly once, so the test above is not passing merely because nothing ever
|
|
// fires.
|
|
static void test_a_firing_watchdog_still_joins_cleanly() {
|
|
std::atomic<int> calls{0};
|
|
{
|
|
audiocpp_backend::IdleWatchdog watchdog(50ms, [&calls] { ++calls; });
|
|
std::this_thread::sleep_for(400ms);
|
|
}
|
|
check(calls.load() == 1, "the callback ran once, inside the scope");
|
|
}
|
|
|
|
int main() {
|
|
test_it_fires_when_nothing_touches_it();
|
|
test_touching_defers_it_indefinitely();
|
|
test_disarm_stops_it_before_the_window();
|
|
test_disarm_is_idempotent();
|
|
test_a_non_positive_window_disables_it();
|
|
test_fired_survives_a_later_disarm();
|
|
test_the_destructor_joins();
|
|
test_a_firing_watchdog_still_joins_cleanly();
|
|
if (failures) {
|
|
fprintf(stderr, "%d check(s) failed\n", failures);
|
|
return 1;
|
|
}
|
|
fprintf(stderr, "all live_watchdog checks passed\n");
|
|
return 0;
|
|
}
|