fix(voice): session id deadlock on open (#1408)

* Unlock when checking voice connection sessionID to prevent deadlock

* Move lock to preserve concurrency safety, while allowing the sessionID to be populated

* style: formatting

Fix formatting of the documentation comment for VoiceConnection.Speaking function

* feat: reword explanatory comment

---------

Co-authored-by: Fedor Lapshin <fe.lap.prog@gmail.com>
This commit is contained in:
Denver Quane 2023-08-12 21:16:34 -06:00 committed by Fedor Lapshin
parent 393091b18c
commit 58193fbfaa
No known key found for this signature in database
GPG key ID: 492D745FB49D2DAB

View file

@ -76,7 +76,7 @@ type VoiceSpeakingUpdateHandler func(vc *VoiceConnection, vs *VoiceSpeakingUpdat
// Speaking sends a speaking notification to Discord over the voice websocket. // Speaking sends a speaking notification to Discord over the voice websocket.
// This must be sent as true prior to sending audio and should be set to false // This must be sent as true prior to sending audio and should be set to false
// once finished sending audio. // once finished sending audio.
// b : Send true if speaking, false if not. // b : Send true if speaking, false if not.
func (v *VoiceConnection) Speaking(b bool) (err error) { func (v *VoiceConnection) Speaking(b bool) (err error) {
v.log(LogDebug, "called (%t)", b) v.log(LogDebug, "called (%t)", b)
@ -294,11 +294,15 @@ func (v *VoiceConnection) open() (err error) {
if v.sessionID != "" { if v.sessionID != "" {
break break
} }
if i > 20 { // only loop for up to 1 second total if i > 20 { // only loop for up to 1 second total
return fmt.Errorf("did not receive voice Session ID in time") return fmt.Errorf("did not receive voice Session ID in time")
} }
// Release the lock, so sessionID can be populated upon receiving a VoiceStateUpdate event.
v.Unlock()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
i++ i++
v.Lock()
} }
// Connect to VoiceConnection Websocket // Connect to VoiceConnection Websocket