From d1242fc39aabf9d49bc0b91f50b27356179f8493 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Tue, 17 Nov 2020 11:00:50 -0800 Subject: [PATCH] Voice: Remove Session from repository before disconnecting This commit introduces this change so that Voice can be called from other threads without waiting for one Session to disconnect itself. --- voice/voice.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/voice/voice.go b/voice/voice.go index 7c91ec1..f108f8e 100644 --- a/voice/voice.go +++ b/voice/voice.go @@ -131,14 +131,17 @@ func (v *Voice) GetSession(guildID discord.GuildID) (*Session, bool) { // RemoveSession removes a session. func (v *Voice) RemoveSession(guildID discord.GuildID) { v.mapmutex.Lock() - defer v.mapmutex.Unlock() - - // Ensure that the session is disconnected. - if ses, ok := v.sessions[guildID]; ok { - ses.Disconnect() + ses, ok := v.sessions[guildID] + if !ok { + v.mapmutex.Unlock() + return } delete(v.sessions, guildID) + v.mapmutex.Unlock() + + // Ensure that the session is disconnected. + ses.Disconnect() } // JoinChannel joins the specified channel in the specified guild.