sourcegraph / jsonrpc2

Package jsonrpc2 provides a client and server implementation of JSON-RPC 2.0 (http://www.jsonrpc.org/specification)
MIT License
190 stars 62 forks source link

Add the ability to recover panic in the handler #76

Closed vaintrub closed 1 year ago

vaintrub commented 1 year ago

Hi! Thank you very much for your library and the work on it!

We encountered a problem that the entire application crashes if a panic occurs in our handlers, but as it turns out, you create a separate goroutine that reads messages, which is why we are unable to recover the panic ourselves.

https://github.com/sourcegraph/jsonrpc2/blob/8a0bf06edfb027f0c070c1f6e6515aaf295a051f/conn.go#L62

it would be very cool if it were possible to add jsonrpc2.HandlerWithPanicRecover

keegancsmith commented 1 year ago

Hi @vaintrub I am not sure why you can't implement this yourself. Can't you start your Handler with

defer func() {
  if r := recover(); r != nil {
    // do something reasonable on panic
  }
}()
vaintrub commented 1 year ago

I tried to do this, but I still couldn’t catch the panic. I solved the problem, thanks. It turned out that I had a goroutine leak 😅