Merge 4732fa776706f5a6f8f87cdc9ccc2ad1bd16e6e6 into cf4775283ec30cda685355b5016c5abd2a56884e

This commit is contained in:
OHZEKI Naoki 2025-06-21 04:41:18 +00:00 committed by GitHub
commit c6dc0e337c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 22 deletions

View File

@ -10,12 +10,12 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"syscall"
"time" "time"
) )
@ -52,19 +52,15 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
} }
return func(c *Context) { return func(c *Context) {
defer func() { defer func() {
if err := recover(); err != nil { if rec := recover(); rec != nil {
// Check for a broken connection, as it is not really a // Check for a broken connection, as it is not really a
// condition that warrants a panic stack trace. // condition that warrants a panic stack trace.
var brokenPipe bool var isBrokenPipeOrConnReset bool
if ne, ok := err.(*net.OpError); ok { err, ok := rec.(error)
var se *os.SyscallError if ok {
if errors.As(ne, &se) { isBrokenPipeOrConnReset = errors.Is(err, syscall.EPIPE) || errors.Is(err, syscall.ECONNRESET)
seStr := strings.ToLower(se.Error()) } else {
if strings.Contains(seStr, "broken pipe") || err = fmt.Errorf("%v", rec)
strings.Contains(seStr, "connection reset by peer") {
brokenPipe = true
}
}
} }
if logger != nil { if logger != nil {
stack := stack(3) stack := stack(3)
@ -72,7 +68,7 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
headers := strings.Split(string(httpRequest), "\r\n") headers := strings.Split(string(httpRequest), "\r\n")
maskAuthorization(headers) maskAuthorization(headers)
headersToStr := strings.Join(headers, "\r\n") headersToStr := strings.Join(headers, "\r\n")
if brokenPipe { if isBrokenPipeOrConnReset {
logger.Printf("%s\n%s%s", err, headersToStr, reset) logger.Printf("%s\n%s%s", err, headersToStr, reset)
} else if IsDebugging() { } else if IsDebugging() {
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s", logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s",
@ -82,12 +78,12 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
timeFormat(time.Now()), err, stack, reset) timeFormat(time.Now()), err, stack, reset)
} }
} }
if brokenPipe { if isBrokenPipeOrConnReset {
// If the connection is dead, we can't write a status to it. // If the connection is dead, we can't write a status to it.
c.Error(err.(error)) //nolint: errcheck c.Error(err) //nolint: errcheck
c.Abort() c.Abort()
} else { } else {
handle(c, err) handle(c, rec)
} }
} }
}() }()

View File

@ -131,13 +131,13 @@ func TestFunction(t *testing.T) {
func TestPanicWithBrokenPipe(t *testing.T) { func TestPanicWithBrokenPipe(t *testing.T) {
const expectCode = 204 const expectCode = 204
expectMsgs := map[syscall.Errno]string{ expectErrnos := []syscall.Errno{
syscall.EPIPE: "broken pipe", syscall.EPIPE,
syscall.ECONNRESET: "connection reset by peer", syscall.ECONNRESET,
} }
for errno, expectMsg := range expectMsgs { for _, errno := range expectErrnos {
t.Run(expectMsg, func(t *testing.T) { t.Run("Recovery from "+errno.Error(), func(t *testing.T) {
var buf strings.Builder var buf strings.Builder
router := New() router := New()
@ -155,7 +155,8 @@ func TestPanicWithBrokenPipe(t *testing.T) {
w := PerformRequest(router, http.MethodGet, "/recovery") w := PerformRequest(router, http.MethodGet, "/recovery")
// TEST // TEST
assert.Equal(t, expectCode, w.Code) assert.Equal(t, expectCode, w.Code)
assert.Contains(t, strings.ToLower(buf.String()), expectMsg) assert.Contains(t, strings.ToLower(buf.String()), errno.Error())
assert.NotContains(t, strings.ToLower(buf.String()), "[Recovery]")
}) })
} }
} }