Skip to content
Myr

Fix panic stack trace being printed during recovery of broken pipe (#1089) (#1259)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Justin Israel · committed by thinkerou · parent 66b47a8 · Browse files · 2 files+69 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+69 added−5 removed

  • other:+69 added−5 removed

2 files+69 added−5 removed

modified:recovery_test.go+40 added
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45// +build go1.767package gin89import (10 "bytes"11 "net"12 "net/http"13 "os"14 "syscall"15 "testing"1617 "github.com/stretchr/testify/assert"18)
57 unmodified lines
76func TestFunction(t *testing.T) {77 bs := function(1)78 assert.Equal(t, []byte("???"), bs)79}8081// TestPanicWithBrokenPipe asserts that recovery specifically handles82// writing responses to broken pipes83func TestPanicWithBrokenPipe(t *testing.T) {84 const expectCode = 2048586 expectMsgs := map[syscall.Errno]string{87 syscall.EPIPE: "broken pipe",88 syscall.ECONNRESET: "connection reset",89 }9091 for errno, expectMsg := range expectMsgs {92 t.Run(expectMsg, func(t *testing.T) {9394 var buf bytes.Buffer9596 router := New()97 router.Use(RecoveryWithWriter(&buf))98 router.GET("/recovery", func(c *Context) {99 // Start writing response100 c.Header("X-Test", "Value")101 c.Status(expectCode)102103 // Oops. Client connection closed104 e := &net.OpError{Err: &os.SyscallError{Err: errno}}105 panic(e)106 })107 // RUN108 w := performRequest(router, "GET", "/recovery")109 // TEST110 assert.Equal(t, expectCode, w.Code)111 assert.Contains(t, buf.String(), expectMsg)112 })113 }114}
modified:recovery.go+29 added-5 removed
8 unmodified lines
9 "fmt"10 "io"11 "io/ioutil"12 "log"13 "net"14 "net/http"15 "net/http/httputil"16 "os"17 "runtime"18 "syscall"19 "time"20)2122var (
16 unmodified lines
39 }40 return func(c *Context) {41 defer func() {42 if err := recover(); err != nil {43 // Check for a broken connection, as it is not really a44 // condition that warrants a panic stack trace.45 var brokenPipe bool46 if ne, ok := err.(*net.OpError); ok {47 if se, ok := ne.Err.(*os.SyscallError); ok {48 if se.Err == syscall.EPIPE || se.Err == syscall.ECONNRESET {49 brokenPipe = true50 }51 }52 }53 if logger != nil {54 stack := stack(3)42 if IsDebugging() {43 httprequest, _ := httputil.DumpRequest(c.Request, false)44 logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s", timeFormat(time.Now()), string(httprequest), err, stack, reset)55 httprequest, _ := httputil.DumpRequest(c.Request, false)56 if brokenPipe {57 logger.Printf("%s\n%s%s", err, string(httprequest), reset)58 } else if IsDebugging() {59 logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s",60 timeFormat(time.Now()), string(httprequest), err, stack, reset)61 } else {46 logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s", timeFormat(time.Now()), err, stack, reset)62 logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s",63 timeFormat(time.Now()), err, stack, reset)64 }65 }6667 // If the connection is dead, we can't write a status to it.68 if brokenPipe {69 c.Error(err.(error))70 c.Abort()71 } else {49 c.AbortWithStatus(http.StatusInternalServerError)72 c.AbortWithStatus(http.StatusInternalServerError)73 }74 }75 }()76 c.Next()77 }
67 unmodified lines