Browse Source

http2: conditionally log stacks from panics in Server Handlers like net/http

Updates golang/go#17790

Change-Id: I7bc596d9a80490d545ad3d1de5859efce34714f6
Reviewed-on: https://go-review.googlesource.com/33102
TryBot-Result: Gobot Gobot <gobot@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Brad Fitzpatrick 9 years ago
parent
commit
0e2717dc3c
3 changed files with 15 additions and 5 deletions
  1. 4 0
      http2/go18.go
  2. 4 0
      http2/not_go18.go
  3. 7 5
      http2/server.go

+ 4 - 0
http2/go18.go

@@ -35,3 +35,7 @@ func configureServer18(h1 *http.Server, h2 *Server) error {
 	}
 	return nil
 }
+
+func shouldLogPanic(panicValue interface{}) bool {
+	return panicValue != nil && panicValue != http.ErrAbortHandler
+}

+ 4 - 0
http2/not_go18.go

@@ -12,3 +12,7 @@ func configureServer18(h1 *http.Server, h2 *Server) error {
 	// No IdleTimeout to sync prior to Go 1.8.
 	return nil
 }
+
+func shouldLogPanic(panicValue interface{}) bool {
+	return panicValue != nil
+}

+ 7 - 5
http2/server.go

@@ -1856,15 +1856,17 @@ func (sc *serverConn) runHandler(rw *responseWriter, req *http.Request, handler
 		rw.rws.stream.cancelCtx()
 		if didPanic {
 			e := recover()
-			// Same as net/http:
-			const size = 64 << 10
-			buf := make([]byte, size)
-			buf = buf[:runtime.Stack(buf, false)]
 			sc.writeFrameFromHandler(FrameWriteRequest{
 				write:  handlerPanicRST{rw.rws.stream.id},
 				stream: rw.rws.stream,
 			})
-			sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
+			// Same as net/http:
+			if shouldLogPanic(e) {
+				const size = 64 << 10
+				buf := make([]byte, size)
+				buf = buf[:runtime.Stack(buf, false)]
+				sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
+			}
 			return
 		}
 		rw.handlerDone()