Fix deadlock on exit by making mic level updates async

This commit is contained in:
Jose Luis Montañes Ojados
2026-01-17 17:06:46 +01:00
parent 2860102627
commit b66e0737d0
2 changed files with 6 additions and 1 deletions

View File

@@ -337,7 +337,8 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Update mic level for display (use the calculated level)
if m.program != nil {
m.program.Send(micLevelMsg(level))
// Use goroutine to prevent blocking the capture loop if the UI is busy (e.g. shutting down)
go m.program.Send(micLevelMsg(level))
}
})
m.addLog("Audio capturer initialized")

View File

@@ -22,6 +22,7 @@ type Capturer struct {
running bool
mu sync.Mutex
stopChan chan struct{}
wg sync.WaitGroup
// Callback for captured audio (called with 960-sample frames)
onAudio func(samples []int16)
@@ -136,6 +137,7 @@ func (c *Capturer) Start() error {
return fmt.Errorf("failed to start audio client: %w", err)
}
c.wg.Add(1)
go c.captureLoop()
return nil
}
@@ -151,6 +153,7 @@ func (c *Capturer) Stop() {
c.mu.Unlock()
close(c.stopChan)
c.wg.Wait() // Wait for capture loop to finish before proceeding
c.client.Stop()
}
@@ -180,6 +183,7 @@ func (c *Capturer) IsRunning() bool {
}
func (c *Capturer) captureLoop() {
defer c.wg.Done()
ticker := time.NewTicker(10 * time.Millisecond) // Check more often than 20ms
defer ticker.Stop()