Skip to content

Commit

Permalink
http3: handle ErrAbortHandler when the handler panics (#3575)
Browse files Browse the repository at this point in the history
* Handle abort like the stdlib does

* Using the sentinel value from the stdlib instead of redefining.

* we return before logging out

* Added test to hand abort handler

* Added in two tests but apparently only saved the first one.

* remove one test case because it wasn't needed
  • Loading branch information
shade34321 committed Oct 9, 2022
1 parent 424a663 commit a905648
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion http3/server.go
Expand Up @@ -576,12 +576,15 @@ func (s *Server) handleRequest(conn quic.Connection, str quic.Stream, decoder *q
func() {
defer func() {
if p := recover(); p != nil {
panicked = true
if p == http.ErrAbortHandler {
return
}
// Copied from net/http/server.go
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
s.logger.Errorf("http: panic serving: %v\n%s", p, buf)
panicked = true
}
}()
handler.ServeHTTP(r, req)
Expand Down
17 changes: 17 additions & 0 deletions http3/server_test.go
Expand Up @@ -201,6 +201,23 @@ var _ = Describe("Server", func() {
Expect(hfs).To(HaveKeyWithValue(":status", []string{"200"}))
})

It("handles a aborting handler", func() {
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic(http.ErrAbortHandler)
})

responseBuf := &bytes.Buffer{}
setRequest(encodeRequest(exampleGetRequest))
str.EXPECT().Context().Return(reqContext)
str.EXPECT().Write(gomock.Any()).DoAndReturn(responseBuf.Write).AnyTimes()
str.EXPECT().CancelRead(gomock.Any())

serr := s.handleRequest(conn, str, qpackDecoder, nil)
Expect(serr.err).ToNot(HaveOccurred())
hfs := decodeHeader(responseBuf)
Expect(hfs).To(HaveKeyWithValue(":status", []string{"500"}))
})

It("handles a panicking handler", func() {
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("foobar")
Expand Down

0 comments on commit a905648

Please sign in to comment.