Skip to content

Commit

Permalink
audio: Bug fix: Deadlock at acquiring the semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Jan 23, 2021
1 parent f488988 commit 53c300a
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions audio/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,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
}
Expand All @@ -450,18 +447,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 {
Expand Down

0 comments on commit 53c300a

Please sign in to comment.