Fix deadlock on exit by making mic level updates async
This commit is contained in:
@@ -337,7 +337,8 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
|
|
||||||
// Update mic level for display (use the calculated level)
|
// Update mic level for display (use the calculated level)
|
||||||
if m.program != nil {
|
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")
|
m.addLog("Audio capturer initialized")
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ type Capturer struct {
|
|||||||
running bool
|
running bool
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
|
wg sync.WaitGroup
|
||||||
|
|
||||||
// Callback for captured audio (called with 960-sample frames)
|
// Callback for captured audio (called with 960-sample frames)
|
||||||
onAudio func(samples []int16)
|
onAudio func(samples []int16)
|
||||||
@@ -136,6 +137,7 @@ func (c *Capturer) Start() error {
|
|||||||
return fmt.Errorf("failed to start audio client: %w", err)
|
return fmt.Errorf("failed to start audio client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.wg.Add(1)
|
||||||
go c.captureLoop()
|
go c.captureLoop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -151,6 +153,7 @@ func (c *Capturer) Stop() {
|
|||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
close(c.stopChan)
|
close(c.stopChan)
|
||||||
|
c.wg.Wait() // Wait for capture loop to finish before proceeding
|
||||||
c.client.Stop()
|
c.client.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,6 +183,7 @@ func (c *Capturer) IsRunning() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Capturer) captureLoop() {
|
func (c *Capturer) captureLoop() {
|
||||||
|
defer c.wg.Done()
|
||||||
ticker := time.NewTicker(10 * time.Millisecond) // Check more often than 20ms
|
ticker := time.NewTicker(10 * time.Millisecond) // Check more often than 20ms
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user