Avoid deadlock during room close (#451)
* avoid deadlock during room closse * better fix
This commit is contained in:
+19
-18
@@ -44,9 +44,8 @@ type Room struct {
|
|||||||
joinedAt atomic.Int64
|
joinedAt atomic.Int64
|
||||||
holds atomic.Int32
|
holds atomic.Int32
|
||||||
// time that the last participant left the room
|
// time that the last participant left the room
|
||||||
leftAt atomic.Int64
|
leftAt atomic.Int64
|
||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
closeOnce sync.Once
|
|
||||||
|
|
||||||
onParticipantChanged func(p types.LocalParticipant)
|
onParticipantChanged func(p types.LocalParticipant)
|
||||||
onMetadataUpdate func(metadata string)
|
onMetadataUpdate func(metadata string)
|
||||||
@@ -441,14 +440,15 @@ func (r *Room) IsClosed() bool {
|
|||||||
// CloseIfEmpty closes the room if all participants had left, or it's still empty past timeout
|
// CloseIfEmpty closes the room if all participants had left, or it's still empty past timeout
|
||||||
func (r *Room) CloseIfEmpty() {
|
func (r *Room) CloseIfEmpty() {
|
||||||
r.lock.Lock()
|
r.lock.Lock()
|
||||||
defer r.lock.Unlock()
|
|
||||||
|
|
||||||
if r.IsClosed() || r.holds.Load() > 0 {
|
if r.IsClosed() || r.holds.Load() > 0 {
|
||||||
|
r.lock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range r.participants {
|
for _, p := range r.participants {
|
||||||
if !p.Hidden() {
|
if !p.Hidden() {
|
||||||
|
r.lock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -464,27 +464,28 @@ func (r *Room) CloseIfEmpty() {
|
|||||||
} else {
|
} else {
|
||||||
elapsed = time.Now().Unix() - r.Room.CreationTime
|
elapsed = time.Now().Unix() - r.Room.CreationTime
|
||||||
}
|
}
|
||||||
|
r.lock.Unlock()
|
||||||
|
|
||||||
if elapsed >= int64(timeout) {
|
if elapsed >= int64(timeout) {
|
||||||
r.closeLocked()
|
r.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Room) Close() {
|
func (r *Room) Close() {
|
||||||
r.lock.Lock()
|
r.lock.Lock()
|
||||||
defer r.lock.Unlock()
|
select {
|
||||||
|
case <-r.closed:
|
||||||
r.closeLocked()
|
r.lock.Unlock()
|
||||||
}
|
return
|
||||||
|
default:
|
||||||
func (r *Room) closeLocked() {
|
// fall through
|
||||||
r.closeOnce.Do(func() {
|
}
|
||||||
r.Logger.Infow("closing room")
|
close(r.closed)
|
||||||
if r.onClose != nil {
|
r.lock.Unlock()
|
||||||
r.onClose()
|
r.Logger.Infow("closing room")
|
||||||
}
|
if r.onClose != nil {
|
||||||
close(r.closed)
|
r.onClose()
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Room) OnClose(f func()) {
|
func (r *Room) OnClose(f func()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user