Skip to content

Commit

Permalink
Removed 'recover-from-panic' band-aids.
Browse files Browse the repository at this point in the history
All the panics must be handled by the user code.
  • Loading branch information
valyala committed Jul 12, 2016
1 parent d1dd56c commit 886e541
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 119 deletions.
5 changes: 0 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"mime/multipart"
"net"
"os"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -1673,10 +1672,6 @@ func hijackConnHandler(r io.Reader, c net.Conn, s *Server, h HijackHandler) {
hjc := s.acquireHijackConn(r, c)

defer func() {
if r := recover(); r != nil {
s.logger().Printf("panic on hijacked conn: %s\nStack trace:\n%s", r, debug.Stack())
}

if br, ok := r.(*bufio.Reader); ok {
releaseReader(s, br)
}
Expand Down
7 changes: 0 additions & 7 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fasthttp
import (
"bufio"
"io"
"runtime/debug"
"sync"

"github.com/valyala/fasthttp/fasthttputil"
Expand Down Expand Up @@ -42,12 +41,6 @@ func NewStreamReader(sw StreamWriter) io.ReadCloser {
}

go func() {
defer func() {
if r := recover(); r != nil {
defaultLogger.Printf("panic in StreamWriter: %s\nStack trace:\n%s", r, debug.Stack())
}
}()

sw(bw)
bw.Flush()
pw.Close()
Expand Down
8 changes: 0 additions & 8 deletions workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fasthttp
import (
"net"
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -203,13 +202,6 @@ func (wp *workerPool) workerFunc(ch *workerChan) {
var c net.Conn

defer func() {
if r := recover(); r != nil {
wp.Logger.Printf("panic: %s\nStack trace:\n%s", r, debug.Stack())
if c != nil {
c.Close()
}
}

wp.lock.Lock()
wp.workersCount--
wp.lock.Unlock()
Expand Down
99 changes: 0 additions & 99 deletions workerpool_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package fasthttp

import (
"fmt"
"io/ioutil"
"net"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -168,100 +166,3 @@ func testWorkerPoolMaxWorkersCount(t *testing.T) {
}
wp.Stop()
}

func TestWorkerPoolPanicErrorSerial(t *testing.T) {
testWorkerPoolPanicErrorMulti(t)
}

func TestWorkerPoolPanicErrorConcurrent(t *testing.T) {
concurrency := 10
ch := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
testWorkerPoolPanicErrorMulti(t)
ch <- struct{}{}
}()
}
for i := 0; i < concurrency; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}
}

func testWorkerPoolPanicErrorMulti(t *testing.T) {
var globalCount uint64
wp := &workerPool{
WorkerFunc: func(conn net.Conn) error {
count := atomic.AddUint64(&globalCount, 1)
switch count % 3 {
case 0:
panic("foobar")
case 1:
return fmt.Errorf("fake error")
}
return nil
},
MaxWorkersCount: 1000,
MaxIdleWorkerDuration: time.Millisecond,
Logger: &customLogger{},
}

for i := 0; i < 10; i++ {
testWorkerPoolPanicError(t, wp)
}
}

func testWorkerPoolPanicError(t *testing.T, wp *workerPool) {
wp.Start()

ln := fasthttputil.NewInmemoryListener()

clientsCount := 10
clientCh := make(chan struct{}, clientsCount)
for i := 0; i < clientsCount; i++ {
go func() {
conn, err := ln.Dial()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
data, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if len(data) > 0 {
t.Fatalf("unexpected data read: %q. Expecting empty data", data)
}
if err = conn.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
clientCh <- struct{}{}
}()
}

for i := 0; i < clientsCount; i++ {
conn, err := ln.Accept()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !wp.Serve(conn) {
t.Fatalf("worker pool mustn't be full")
}
}

for i := 0; i < clientsCount; i++ {
select {
case <-clientCh:
case <-time.After(time.Second):
t.Fatalf("timeout")
}
}

if err := ln.Close(); err != nil {
t.Fatalf("unexpected error: %s", err)
}

wp.Stop()
}

0 comments on commit 886e541

Please sign in to comment.