diff --git a/src/app/spreed-speakfreely-server/hub.go b/src/app/spreed-speakfreely-server/hub.go index a1207618..f8ae496d 100644 --- a/src/app/spreed-speakfreely-server/hub.go +++ b/src/app/spreed-speakfreely-server/hub.go @@ -164,18 +164,25 @@ func (h *Hub) GetRoom(id string) *RoomWorker { if !ok { h.mutex.RUnlock() h.mutex.Lock() - room = NewRoomWorker(h, id) - h.roomTable[id] = room - h.mutex.Unlock() - go func() { - // Start room, this blocks until room expired. - room.Start() - // Cleanup room when we are done. - h.mutex.Lock() - defer h.mutex.Unlock() - delete(h.roomTable, id) - log.Printf("Cleaned up room '%s'\n", id) - }() + // need to re-check, another thread might have created the room + // while we waited for the lock + room, ok = h.roomTable[id] + if !ok { + room = NewRoomWorker(h, id) + h.roomTable[id] = room + h.mutex.Unlock() + go func() { + // Start room, this blocks until room expired. + room.Start() + // Cleanup room when we are done. + h.mutex.Lock() + defer h.mutex.Unlock() + delete(h.roomTable, id) + log.Printf("Cleaned up room '%s'\n", id) + }() + } else { + h.mutex.Unlock() + } } else { h.mutex.RUnlock() }