Attempt to make audio and speech discovery more robust.

This commit is contained in:
Storm Dragon committed 2026-11-03 13:21:50 -05:00
1 parent a27f274099
commit dc945ff392
21 files changed
+1314 -60

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
// Package audio contains the shared, init-independent audio retry policy.
package audio
import (
"context"
"errors"
"io"
"net"
"os"
"os/exec"
"syscall"
"time"
)
const RecoveryTimeout = 20 * time.Second
// Temporary excludes permission, executable and protocol/configuration errors.
func Temporary(err error) bool {
if errors.Is(err, context.Canceled) {
return false
}
var executableError *exec.Error
var pathError *os.PathError
if errors.As(err, &executableError) || errors.As(err, &pathError) && pathError.Op == "fork/exec" {
return false
}
var networkError net.Error
return errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) ||
errors.Is(err, syscall.ENOENT) || errors.Is(err, syscall.ECONNREFUSED) ||
errors.Is(err, syscall.ECONNRESET) || errors.Is(err, syscall.EPIPE) ||
errors.Is(err, context.DeadlineExceeded) || errors.As(err, &networkError) && networkError.Timeout()
}
// Retry never sleeps or performs I/O on the input loop. Callers supply a
// deadline and must make each attempt honor its context.
func Retry(ctx context.Context, attempt func(context.Context) error, retryable func(error) bool) error {
delay := 100 * time.Millisecond
for {
if err := ctx.Err(); err != nil {
return err
}
err := attempt(ctx)
if err == nil || !retryable(err) {
return err
}
timer := time.NewTimer(delay)
select {
case <-ctx.Done():
timer.Stop()
return errors.Join(ctx.Err(), err)
case <-timer.C:
}
delay = min(2*delay, time.Second)
}
}
+51
View File
@@ -0,0 +1,51 @@
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
package audio
import (
"context"
"errors"
"io"
"net"
"os/exec"
"syscall"
"testing"
"time"
)
func TestRetryRecoversAndPreservesPermanentErrors(t *testing.T) {
attempts := 0
err := Retry(context.Background(), func(context.Context) error {
attempts++
if attempts < 2 {
return &net.OpError{Op: "dial", Net: "unix", Err: syscall.ENOENT}
}
return nil
}, Temporary)
if err != nil || attempts != 2 {
t.Fatal(err, attempts)
}
for _, problem := range []error{syscall.EACCES, errors.New("invalid configuration"), &exec.Error{Name: "missing-speech-daemon", Err: exec.ErrNotFound}} {
attempts = 0
err := Retry(context.Background(), func(context.Context) error { attempts++; return problem }, Temporary)
if !errors.Is(err, problem) || attempts != 1 {
t.Fatal(err, attempts)
}
}
}
func TestRetryCancellationStopsBackoffAndDeadlinePreservesCause(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
attempts := 0
err := Retry(ctx, func(context.Context) error { attempts++; cancel(); return io.EOF }, Temporary)
if !errors.Is(err, context.Canceled) || attempts != 1 {
t.Fatal(err, attempts)
}
ctx, cancel = context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
err = Retry(ctx, func(context.Context) error { return io.EOF }, Temporary)
if !errors.Is(err, context.DeadlineExceeded) || !errors.Is(err, io.EOF) {
t.Fatal(err)
}
}