From 514904392cd32d17bc7fc99e1d22711fc6d055ef Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 23 Jan 2021 23:07:16 +0900 Subject: [PATCH] audio: Bug fix: Deadlock at acquiring the semaphore Closes #1469 --- audio/writerplayer.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/audio/writerplayer.go b/audio/writerplayer.go index 1c0dc1067..e9726fef1 100644 --- a/audio/writerplayer.go +++ b/audio/writerplayer.go @@ -168,9 +168,6 @@ func (p *writerPlayer) loop() { } func (p *writerPlayer) read() ([]byte, bool) { - p.m.Lock() - defer p.m.Unlock() - if p.context.hasError() { return nil, false } @@ -179,18 +176,20 @@ func (p *writerPlayer) read() ([]byte, bool) { return nil, false } + p.context.acquireSemaphore() + defer func() { + p.context.releaseSemaphore() + }() + + p.m.Lock() + defer p.m.Unlock() + // playing can be false when pausing. if !p.playing { return nil, false } const bufSize = 2048 - - p.context.acquireSemaphore() - defer func() { - p.context.releaseSemaphore() - }() - if p.readbuf == nil { p.readbuf = make([]byte, bufSize) }