Panic recovery in net/http Server

173 views
Skip to first unread message

Amit Saha

unread,
Jul 16, 2021, 7:23:24 PM7/16/21
to golang-nuts
I recently posted a query [1] to the grpc mailing list to clarify a
behavior in the Go gRPC server - a panic() in a request handler
function would terminate the server process. Now, I know that's not
the case in the net/http server implementation - a demo here [2].

However, the interesting thing that I read in one of the replies is
that this isn't considered something that is desirable in the net/http
package either. but is a historical choice that we wish could have
been different. I was wondering if someone here would know about that
and why it may not be desirable to have automatic recovery in the http
server implementation because even with the automatic recovery in
place:

1. The application author/operator user will get to know when a panic
has occurred unless the
ErrAbortHandler value is used to panic.
2. The application author can still implement a middleware and not let
the stdlib's panic handling mechanism get
invoked at all to run any custom cleanup for example.


[1] https://groups.google.com/g/grpc-io/c/J8T43gWMwnI
[2] https://gist.github.com/amitsaha/ec7ce96e47bbfca0e7a52d5bff53485b

Sean Liao

unread,
Jul 16, 2021, 7:40:58 PM7/16/21
to golang-nuts
Typically things panic when something goes seriously wrong, 
leaving no guarantees on the consistency of the system making it unsafe to continue.
It's also much easier to never look at your logs to notice a panic
than to ignore a crashing server.

https://github.com/golang/go/issues/25245
Reply all
Reply to author
Forward
0 new messages