fix deadlock.

This commit is contained in:
Miroslav Šedivý 2021-09-18 00:56:03 +02:00
parent 6bd46b438e
commit 137614c087
3 changed files with 15 additions and 29 deletions

View File

@ -198,16 +198,13 @@ func (manager *SessionManagerCtx) ClearHost() {
// --- // ---
func (manager *SessionManagerCtx) Broadcast(event string, payload interface{}, exclude interface{}) { func (manager *SessionManagerCtx) Broadcast(event string, payload interface{}, exclude interface{}) {
manager.sessionsMu.Lock() for _, session := range manager.List() {
defer manager.sessionsMu.Unlock()
for id, session := range manager.sessions {
if !session.State().IsConnected { if !session.State().IsConnected {
continue continue
} }
if exclude != nil { if exclude != nil {
if in, _ := utils.ArrayIn(id, exclude); in { if in, _ := utils.ArrayIn(session.ID(), exclude); in {
continue continue
} }
} }
@ -217,16 +214,13 @@ func (manager *SessionManagerCtx) Broadcast(event string, payload interface{}, e
} }
func (manager *SessionManagerCtx) AdminBroadcast(event string, payload interface{}, exclude interface{}) { func (manager *SessionManagerCtx) AdminBroadcast(event string, payload interface{}, exclude interface{}) {
manager.sessionsMu.Lock() for _, session := range manager.List() {
defer manager.sessionsMu.Unlock()
for id, session := range manager.sessions {
if !session.State().IsConnected || !session.Profile().IsAdmin { if !session.State().IsConnected || !session.Profile().IsAdmin {
continue continue
} }
if exclude != nil { if exclude != nil {
if in, _ := utils.ArrayIn(id, exclude); in { if in, _ := utils.ArrayIn(session.ID(), exclude); in {
continue continue
} }
} }

View File

@ -68,14 +68,9 @@ func (peer *WebRTCPeerCtx) Destroy() {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected { if peer.connection != nil {
return err := peer.connection.Close()
peer.logger.Err(err).Msg("peer connection destroyed")
peer.connection = nil
} }
// TODO: Send webrtc disconnect event via websocket.
err := peer.connection.Close()
peer.logger.Err(err).Msg("peer connection destroyed")
peer.connection = nil
} }

View File

@ -67,21 +67,18 @@ func (peer *WebSocketPeerCtx) Send(event string, payload interface{}) {
} }
func (peer *WebSocketPeerCtx) Destroy(reason string) { func (peer *WebSocketPeerCtx) Destroy(reason string) {
peer.mu.Lock()
defer peer.mu.Unlock()
if peer.connection == nil {
return
}
peer.Send( peer.Send(
event.SYSTEM_DISCONNECT, event.SYSTEM_DISCONNECT,
message.SystemDisconnect{ message.SystemDisconnect{
Message: reason, Message: reason,
}) })
err := peer.connection.Close() peer.mu.Lock()
peer.logger.Err(err).Msg("peer connection destroyed") defer peer.mu.Unlock()
peer.connection = nil if peer.connection != nil {
err := peer.connection.Close()
peer.logger.Err(err).Msg("peer connection destroyed")
peer.connection = nil
}
} }