mirror of
https://github.com/gin-gonic/gin.git
synced 2026-12-03 09:43:36 +08:00
fix(recovery): treat http.ErrAbortHandler specially and re-panic
[http.ErrAbortHandler] should be re-panic-ed to be properly handled by [http.Server] which either closes the network connection or sends an HTTP/2 RST_STREAM to the client so that it sees an interrupted response.
This commit is contained in:
parent
0f09c3a9b4
commit
e1c8dda324
17
recovery.go
17
recovery.go
@ -60,15 +60,18 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
|||||||
if rec := recover(); rec != 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 isBrokenPipe bool
|
var (
|
||||||
|
isBrokenPipe bool
|
||||||
|
isAbortHandler bool
|
||||||
|
)
|
||||||
err, ok := rec.(error)
|
err, ok := rec.(error)
|
||||||
if ok {
|
if ok {
|
||||||
|
isAbortHandler = errors.Is(err, http.ErrAbortHandler)
|
||||||
isBrokenPipe = errors.Is(err, syscall.EPIPE) ||
|
isBrokenPipe = errors.Is(err, syscall.EPIPE) ||
|
||||||
errors.Is(err, syscall.ECONNRESET) ||
|
errors.Is(err, syscall.ECONNRESET)
|
||||||
errors.Is(err, http.ErrAbortHandler)
|
|
||||||
}
|
}
|
||||||
if logger != nil {
|
if logger != nil {
|
||||||
if isBrokenPipe {
|
if isBrokenPipe || isAbortHandler {
|
||||||
logger.Printf("%s\n%s%s", rec, secureRequestDump(c.Request), reset)
|
logger.Printf("%s\n%s%s", rec, secureRequestDump(c.Request), 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",
|
||||||
@ -78,13 +81,17 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
|||||||
timeFormat(time.Now()), rec, stack(stackSkip), reset)
|
timeFormat(time.Now()), rec, stack(stackSkip), reset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if isBrokenPipe {
|
if isBrokenPipe || isAbortHandler {
|
||||||
// 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) //nolint: errcheck
|
c.Error(err) //nolint: errcheck
|
||||||
c.Abort()
|
c.Abort()
|
||||||
} else {
|
} else {
|
||||||
handle(c, rec)
|
handle(c, rec)
|
||||||
}
|
}
|
||||||
|
// Re-panic error for http.Server to handle it properly.
|
||||||
|
if isAbortHandler {
|
||||||
|
panic(rec)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|||||||
@ -6,14 +6,17 @@ package gin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPanicClean(t *testing.T) {
|
func TestPanicClean(t *testing.T) {
|
||||||
@ -141,13 +144,16 @@ func TestPanicWithAbortHandler(t *testing.T) {
|
|||||||
c.Header("X-Test", "Value")
|
c.Header("X-Test", "Value")
|
||||||
c.Status(expectCode)
|
c.Status(expectCode)
|
||||||
|
|
||||||
// Panic with ErrAbortHandler which should be treated as broken pipe
|
// Panic with ErrAbortHandler which should be treated as special
|
||||||
panic(http.ErrAbortHandler)
|
panic(http.ErrAbortHandler)
|
||||||
})
|
})
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
srv := httptest.NewServer(router)
|
||||||
|
defer srv.Close()
|
||||||
|
_, err := srv.Client().Get(srv.URL + "/recovery")
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.ErrorIs(t, err, io.EOF)
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, expectCode, w.Code)
|
|
||||||
out := buf.String()
|
out := buf.String()
|
||||||
assert.Contains(t, out, "net/http: abort Handler")
|
assert.Contains(t, out, "net/http: abort Handler")
|
||||||
assert.NotContains(t, out, "panic recovered")
|
assert.NotContains(t, out, "panic recovered")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user