Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware: recoverer ignores http.ErrAbortHandler #588

Closed
ChristophPech opened this issue Feb 24, 2021 · 4 comments
Closed

middleware: recoverer ignores http.ErrAbortHandler #588

ChristophPech opened this issue Feb 24, 2021 · 4 comments

Comments

@ChristophPech
Copy link

The http.ErrAbortHandler panic usually causes a connection close. The recoverer just ignores the panic instead of reraising it causing connection leaks.

@pkieltyka
Copy link
Member

I'm a bit confused.. how should we handle http.ErrAbortHandler then..?

the current middleware: https://github.com/go-chi/chi/blob/master/middleware/recoverer.go#L22

looks like so:

func Recoverer(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if rvr := recover(); rvr != nil && rvr != http.ErrAbortHandler {

				logEntry := GetLogEntry(r)
				if logEntry != nil {
					logEntry.Panic(rvr, debug.Stack())
				} else {
					PrintPrettyStack(rvr)
				}

				w.WriteHeader(http.StatusInternalServerError)
			}
		}()

		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

suggestion is to change it to....?

func Recoverer(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if rvr := recover(); rvr != nil {

				logEntry := GetLogEntry(r)
				if logEntry != nil {
					logEntry.Panic(rvr, debug.Stack())
				} else {
					PrintPrettyStack(rvr)
				}

				if rvr == http.ErrAbortHandler {
					panic(rvr)
				}

				w.WriteHeader(http.StatusInternalServerError)
			}
		}()

		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

so, we log and re-raise the panic..? or something else is suggested here..?

@drakkan
Copy link
Contributor

drakkan commented Jan 3, 2022

please take a look at #624, the panic should not be logged

https://go.googlesource.com/go/+/master/src/net/http/server.go#1793

@pkieltyka
Copy link
Member

I get it now, thank you. Do you mind rebasing your PR and I will then merge it?

drakkan added a commit to drakkan/chi that referenced this issue Jan 3, 2022
This error is generally used to abort a request while streaming a response
so it should not be recovered otherwise the request is not aborted and
the client does not detect the error

Fixes go-chi#588
@drakkan
Copy link
Contributor

drakkan commented Jan 3, 2022

I get it now, thank you. Do you mind rebasing your PR and I will then merge it?

Dome, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants