Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: add recover logic in session listen go routines to prevent program crash #1588

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ func iconURL(iconHash, staticIconURL, animatedIconURL, size string) string {
}
return URL
}

func runGoroutineWithRecover(f func(), recoverHandler func(interface{}), rerun bool) {
go func() {
defer func() {
if err := recover(); err != nil {
if recoverHandler != nil {
recoverHandler(err)
}
if rerun {
runGoroutineWithRecover(f, recoverHandler, rerun)
}
}
}()
f()
}()
}
34 changes: 24 additions & 10 deletions wsapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,22 @@ func (s *Session) Open() error {
s.listening = make(chan interface{})

// Start sending heartbeats and reading messages from Discord.
go s.heartbeat(s.wsConn, s.listening, h.HeartbeatInterval)
go s.listen(s.wsConn, s.listening)
runGoroutineWithRecover(
func() {
s.heartbeat(s.wsConn, s.listening, h.HeartbeatInterval)
},
func(i interface{}) {
s.log(LogError, "recover panic in heartbeat: %v", i)
},
true)
runGoroutineWithRecover(
func() {
s.listen(s.wsConn, s.listening)
},
func(i interface{}) {
s.log(LogError, "recover panic in listen: %v", i)
},
true)

s.log(LogInformational, "exiting")
return nil
Expand Down Expand Up @@ -697,10 +711,10 @@ type voiceChannelJoinOp struct {

// ChannelVoiceJoin joins the session user to a voice channel.
//
// gID : Guild ID of the channel to join.
// cID : Channel ID of the channel to join.
// mute : If true, you will be set to muted upon joining.
// deaf : If true, you will be set to deafened upon joining.
// gID : Guild ID of the channel to join.
// cID : Channel ID of the channel to join.
// mute : If true, you will be set to muted upon joining.
// deaf : If true, you will be set to deafened upon joining.
func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *VoiceConnection, err error) {

s.log(LogInformational, "called")
Expand Down Expand Up @@ -744,10 +758,10 @@ func (s *Session) ChannelVoiceJoin(gID, cID string, mute, deaf bool) (voice *Voi
//
// This should only be used when the VoiceServerUpdate will be intercepted and used elsewhere.
//
// gID : Guild ID of the channel to join.
// cID : Channel ID of the channel to join, leave empty to disconnect.
// mute : If true, you will be set to muted upon joining.
// deaf : If true, you will be set to deafened upon joining.
// gID : Guild ID of the channel to join.
// cID : Channel ID of the channel to join, leave empty to disconnect.
// mute : If true, you will be set to muted upon joining.
// deaf : If true, you will be set to deafened upon joining.
func (s *Session) ChannelVoiceJoinManual(gID, cID string, mute, deaf bool) (err error) {

s.log(LogInformational, "called")
Expand Down