Try to gracefully quit pulseaudio daemon if it is not responding
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
p "github.com/mafik/pulseaudio"
|
p "github.com/mafik/pulseaudio"
|
||||||
)
|
)
|
||||||
@@ -11,6 +13,8 @@ import (
|
|||||||
var SoundVolume = "🔊:0%"
|
var SoundVolume = "🔊:0%"
|
||||||
var pa *p.Client
|
var pa *p.Client
|
||||||
|
|
||||||
|
// TODO: https://twin.sh/articles/44/add-a-timeout-to-any-function-in-go timeout pulseaudio calls
|
||||||
|
|
||||||
// UpdateVolumeInfo updates info about current Sound Volume.
|
// UpdateVolumeInfo updates info about current Sound Volume.
|
||||||
func UpdateVolumeInfo() {
|
func UpdateVolumeInfo() {
|
||||||
var err error
|
var err error
|
||||||
@@ -107,7 +111,34 @@ func PaReinit() error {
|
|||||||
cmd := exec.Command("pulseaudio", "--check")
|
cmd := exec.Command("pulseaudio", "--check")
|
||||||
|
|
||||||
if err := cmd.Run(); err == nil {
|
if err := cmd.Run(); err == nil {
|
||||||
return fmt.Errorf("pulseaudio is already running, but we have troubles contacting it: %w", err)
|
log.Printf("pulseaudio already running, but seems not responding, try gracefully kill it")
|
||||||
|
|
||||||
|
cmd = exec.Command("pulseaudio", "--kill")
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return fmt.Errorf("unable to kill pulseaudio: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
cmd := exec.Command("pulseaudio", "--check")
|
||||||
|
|
||||||
|
// PA not running. Breakout of loop.
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if cnt >= 5 {
|
||||||
|
return errors.New("timeout waiting pulseaudio to exit") //nolint: goerr113
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Print("Waiting for pulseaudio to exit")
|
||||||
|
|
||||||
|
cnt++
|
||||||
|
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd = exec.Command("pulseaudio", "--start")
|
cmd = exec.Command("pulseaudio", "--start")
|
||||||
|
|||||||
Reference in New Issue
Block a user