Skip to content

Commit

Permalink
Fix SIGSEGV (issue andersfylling#298) & voice hearthbeating
Browse files Browse the repository at this point in the history
* Fix SIGSEGV panic in issue andersfylling#298, function returns non-nil error:
  "context cancelled"

* Fix opCode 3 sending before opCode 0 in voice
	* this caused "authentication error", which resulted in SIGSEGV

* Add & improved errors and logs in (re)connecting functions
  • Loading branch information
oidq committed May 30, 2020
1 parent e423673 commit 454272d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions internal/gateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func (c *client) reconnect() (err error) {

c.log.Debug(c.getLogPrefix(), "is reconnecting")
if err := c.disconnect(); err != nil {
c.log.Debug(c.getLogPrefix(), "reconnecting failed: ", err.Error())
c.RLock()
if c.requestedDisconnect.Load() {
c.RUnlock()
Expand All @@ -330,15 +331,14 @@ func (c *client) reconnectLoop() (err error) {
if try == 0 {
c.log.Debug(c.getLogPrefix(), "trying to connect")
} else {
c.log.Debug(c.getLogPrefix(), "reconnect attempt", try)
c.log.Debug(c.getLogPrefix(), "reconnect attempt ", try)
}
if _, err = c.connect(); err == nil {
c.log.Debug(c.getLogPrefix(), "establishing connection succeeded")
break
}

c.log.Info(c.getLogPrefix(), "establishing connection failed, trying again in ", delay)
c.log.Info(c.getLogPrefix(), err)
c.log.Error(c.getLogPrefix(), "establishing connection failed: ", err)
c.log.Info(c.getLogPrefix(), "next connection attempt in ", delay)

// wait N seconds
select {
Expand Down Expand Up @@ -497,15 +497,20 @@ func (c *client) receiver(ctx context.Context) {
once.Do(cancel) // free
return
case <-internal.Done():
go c.reconnect()
c.log.Debug(c.getLogPrefix(), "closing receiver after read error")
go func() {
if err := c.reconnect(); err != nil {
c.log.Error(c.getLogPrefix(), "reconnecting attempt failed: ", err.Error())
}
}()
return
default:
}

var packet []byte
var err error
if packet, err = c.conn.Read(ctx); err != nil {
c.log.Debug(c.getLogPrefix(), "read error: ", err.Error())
reconnect := true
var closeErr *CloseErr
isCloseErr := errors.As(err, &closeErr)
Expand All @@ -523,8 +528,6 @@ func (c *client) receiver(ctx context.Context) {
reconnect = false
default:
}
} else {
c.log.Debug(c.getLogPrefix(), err)
}

select {
Expand Down
4 changes: 2 additions & 2 deletions internal/gateway/voiceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ func (c *VoiceClient) onHello(v interface{}) (err error) {
c.heartbeatInterval = interval
c.Unlock()

c.activateHeartbeats <- true

c.sendVoiceHelloPacket()
c.activateHeartbeats <- true
return nil
}

Expand Down Expand Up @@ -298,6 +297,7 @@ func (c *VoiceClient) internalConnect() (evt interface{}, err error) {
c.log.Info(c.getLogPrefix(), "connected")
case <-ctx.Done():
c.isConnected.Store(false)
err = errors.New("context cancelled")
case <-time.After(5 * time.Second):
c.isConnected.Store(false)
err = errors.New("did not receive desired event in time. opcode " + strconv.Itoa(int(opcode.VoiceReady)))
Expand Down

0 comments on commit 454272d

Please sign in to comment.