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 0a5e415
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 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,15 @@ 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", err)
fmt.Fprintf(os.Stderr, "%s\n", 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 +629,14 @@ 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", err)
fmt.Fprintf(os.Stderr, "%s\n", 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 0a5e415

Please sign in to comment.