Browse Source

Add lock to prevent race condition (#816)

There's a race between `VoiceConnection.Disconnect()` and `VoiceConnection.onVoiceStateUpdate()` (accessing VoiceConnection.sessionID)
Jonathan Roth 4 years ago
parent
commit
9bcbfecf1d
1 changed files with 2 additions and 0 deletions
  1. 2 0
      voice.go

+ 2 - 0
voice.go

@@ -139,6 +139,7 @@ func (v *VoiceConnection) ChangeChannel(channelID string, mute, deaf bool) (err
 func (v *VoiceConnection) Disconnect() (err error) {
 
 	// Send a OP4 with a nil channel to disconnect
+	v.Lock()
 	if v.sessionID != "" {
 		data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, nil, true, true}}
 		v.session.wsMutex.Lock()
@@ -146,6 +147,7 @@ func (v *VoiceConnection) Disconnect() (err error) {
 		v.session.wsMutex.Unlock()
 		v.sessionID = ""
 	}
+	v.Unlock()
 
 	// Close websocket and udp connections
 	v.Close()