Beep cleanup: start before Task.Run + Wait timeout to avoid orphaned task

This commit is contained in:
2026-06-12 12:06:48 +00:00
parent 14e862f50a
commit 9b63e298d3
+14 -3
View File
@@ -165,7 +165,6 @@ static void FireAndForgetBeep(int device, short[] beepBuffer)
{
int remaining = beepBuffer.Length - offset;
int take = (int)Math.Min(frameCount, (uint)remaining);
int silence = (int)frameCount - take;
unsafe
{
short* dst = (short*)output.ToPointer();
@@ -187,15 +186,27 @@ static void FireAndForgetBeep(int device, short[] beepBuffer)
var stream = new Stream(
null, outParams, WakewordModel.SampleRate, 1024, StreamFlags.NoFlag, playCb, IntPtr.Zero);
try
{
stream.Start();
}
catch
{
stream.Dispose();
done.Dispose();
throw;
}
Task.Run(() =>
{
done.Wait();
// 2 s timeout so a failed callback (which PortAudio silently swallows
// and would leave 'done' unset) eventually releases the stream + handle
// instead of leaking them for the process lifetime.
done.Wait(TimeSpan.FromSeconds(2));
Thread.Sleep(200);
stream.Stop();
stream.Dispose();
done.Dispose();
});
stream.Start();
}
static class Libc