From 82050d6ac16eab3ea1164037b9e7ba4a69f77645 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 23 Jan 2021 23:11:15 +0900 Subject: [PATCH] audio: Bug fix: Deadlock at acquiring the semaphore Closes #1469 --- audio/audio.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index c3b0bc2f4..ac27dc021 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -389,9 +389,6 @@ func (p *playerImpl) loop() { } func (p *playerImpl) read() ([]byte, bool) { - p.m.Lock() - defer p.m.Unlock() - if p.context.hasError() { return nil, false } @@ -400,18 +397,20 @@ func (p *playerImpl) read() ([]byte, bool) { return nil, false } + p.context.semaphore <- struct{}{} + defer func() { + <-p.context.semaphore + }() + + p.m.Lock() + defer p.m.Unlock() + // playing can be false when pausing. if !p.playing { return nil, false } const bufSize = 2048 - - p.context.semaphore <- struct{}{} - defer func() { - <-p.context.semaphore - }() - newBuf := make([]byte, bufSize-len(p.buf)) n, err := p.src.Read(newBuf) if err != nil {