Skip to content

Commit

Permalink
feat: catch panics in yamux send/receive loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 14, 2022
1 parent 7b5cadb commit 6ff7c51
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math"
"net"
"os"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -488,7 +489,14 @@ func (s *Session) send() {
}
}

func (s *Session) sendLoop() error {
func (s *Session) sendLoop() (err error) {
defer func() {
if rerr := recover(); rerr != nil {
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
err = fmt.Errorf("panic in yamux send loop: %s", rerr)
}
}()

defer close(s.sendDoneCh)

// Extend the write deadline if we've passed the halfway point. This can
Expand Down Expand Up @@ -620,7 +628,13 @@ var (
)

// recvLoop continues to receive data until a fatal error is encountered
func (s *Session) recvLoop() error {
func (s *Session) recvLoop() (err error) {
defer func() {
if rerr := recover(); rerr != nil {
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
err = fmt.Errorf("panic in yamux receive loop: %s", rerr)
}
}()
defer close(s.recvDoneCh)
var hdr header
for {
Expand Down

0 comments on commit 6ff7c51

Please sign in to comment.