Commit 0702ed104f for golang
commit 0702ed104fe818ffe78e37bf81a4e384e52577af
Author: Damien Neil <dneil@google.com>
Date: Mon Nov 30 14:56:20 2026 -0800
net/http: handle runtime.Goexit in handlers, panics in some hooks
CL 859161 moved handler panic recovery into Server.HandleRequest,
which converts panics into an error return. Move some post-handler
cleanup back into defers to recover from a handler which calls
runtime.Goexit (which HandleRequest does not handle).
Add a recover back into the HTTP/1 serving path to catch panics
from various hooks (like ConnState). We don't promise this,
but it's long-standing behavior.
Fixes #83259
Change-Id: I847a86307336efb8c0fd097286e9eea16a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/868801
Reviewed-by: Nicholas Husin <nsh@golang.org>
Reviewed-by: Nicholas Husin <husin@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/src/net/http/internal/http2/server.go b/src/net/http/internal/http2/server.go
index 0a6d2ba45c..75ce147931 100644
--- a/src/net/http/internal/http2/server.go
+++ b/src/net/http/internal/http2/server.go
@@ -2589,22 +2589,29 @@ func (sc *serverConn) handlerDone() {
// Run on its own goroutine.
func (sc *serverConn) runHandler(rw *responseWriter, req *ServerRequest, handleRequest func(*ResponseWriter, *ServerRequest) error) {
defer sc.sendServeMsg(handlerDoneMsg)
- err := handleRequest(rw, req)
- rw.rws.stream.cancelCtx()
- if b := rw.rws.stream.reqBody; b != nil {
- // Closing the body refunds flow control credit for any unconsumed data.
- // (reqBody is nil for Upgrade: h2c requests, but those do not use flow
- // control for the request body.)
- b.Close()
- }
- if err != nil {
- sc.writeFrameFromHandler(FrameWriteRequest{
- write: handlerPanicRST{rw.rws.stream.id},
- stream: rw.rws.stream,
- })
- return
+ handlerPanic := true // or runtime.Goexit
+ defer func() {
+ // handleRequest will recover panics, but do cleanup in a defer
+ // in case the handler calls runtime.Goexit.
+ rw.rws.stream.cancelCtx()
+ if b := rw.rws.stream.reqBody; b != nil {
+ // Closing the body refunds flow control credit for any unconsumed data.
+ // (reqBody is nil for Upgrade: h2c requests, but those do not use flow
+ // control for the request body.)
+ b.Close()
+ }
+ if handlerPanic {
+ sc.writeFrameFromHandler(FrameWriteRequest{
+ write: handlerPanicRST{rw.rws.stream.id},
+ stream: rw.rws.stream,
+ })
+ return
+ }
+ rw.handlerDone()
+ }()
+ if err := handleRequest(rw, req); err == nil {
+ handlerPanic = false
}
- rw.handlerDone()
}
func handleHeaderListTooLong(w *ResponseWriter, r *ServerRequest) error {
diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index ed03ca9fa5..8a5c44a81f 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -3169,6 +3169,12 @@ func TestHandlerPanicWithHijack(t *testing.T) {
}, []testMode{http1Mode})
}
+func TestHandlerGoexit(t *testing.T) {
+ runNoSynctest(t, func(t *testing.T, mode testMode) {
+ testHandlerPanic(t, false, mode, nil, "goexit")
+ }, testNotParallel)
+}
+
func testHandlerPanic(t *testing.T, withHijack bool, mode testMode, wrapper func(Handler) Handler, panicValue any) {
synctest.Test(t, func(t *testing.T) {
var handler Handler = HandlerFunc(func(w ResponseWriter, r *Request) {
@@ -3179,6 +3185,9 @@ func testHandlerPanic(t *testing.T, withHijack bool, mode testMode, wrapper func
}
defer rwc.Close()
}
+ if panicValue == "goexit" {
+ runtime.Goexit()
+ }
panic(panicValue)
})
if wrapper != nil {
@@ -3194,18 +3203,21 @@ func testHandlerPanic(t *testing.T, withHijack bool, mode testMode, wrapper func
_, err := cst.c.Get(cst.ts.URL)
if err == nil {
- t.Logf("expected an error")
+ t.Errorf("expected an error")
}
cst.ts.Close()
synctest.Wait()
- if panicValue == ErrAbortHandler {
+ switch panicValue {
+ case ErrAbortHandler, "goexit":
if got := logBuf.String(); got != "" {
t.Errorf("unexpected log output:\n%v", got)
}
- } else if logBuf.String() == "" {
- t.Errorf("nothing logged after panic; want something")
+ default:
+ if logBuf.String() == "" {
+ t.Errorf("nothing logged after panic; want something")
+ }
}
})
}
diff --git a/src/net/http/server.go b/src/net/http/server.go
index e486450bd7..7445871aab 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -2034,6 +2034,15 @@ func (c *conn) serve(ctx context.Context) {
ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
var inFlightResponse *response
defer func() {
+ if err := recover(); err != nil {
+ // This recover catches panics from places other than the server handler
+ // such as a ConnState hook. We don't promise this behavior, but it's
+ // long-standing so we preserve it.
+ const size = 64 << 10
+ buf := make([]byte, size)
+ buf = buf[:runtime.Stack(buf, false)]
+ c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf)
+ }
if inFlightResponse != nil {
inFlightResponse.cancelCtx()
inFlightResponse.disableWriteContinue(true)