Merge pull request #83 from filecoin-project/fix/jsonrpc-catch-panic

jsonrpc: Catch panics in jsonrpc calls
This commit is contained in:
Jakub Sztandera 2019-07-25 16:18:32 +02:00 committed by GitHub
commit 90da9e60ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -111,6 +111,18 @@ func (h handlers) handleReader(ctx context.Context, r io.Reader, w io.Writer, rp
h.handle(ctx, req, wf, rpcError, func(bool) {}, nil)
}
func doCall(f reflect.Value, params []reflect.Value) (out []reflect.Value, err error) {
defer func() {
if i := recover(); i != nil {
err = xerrors.Errorf("panic in rpc method: %s", i)
log.Error(err)
}
}()
out = f.Call(params)
return out, nil
}
func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer)), rpcError rpcErrFunc, done func(keepCtx bool), chOut chanOut) {
handler, ok := h[req.Method]
if !ok {
@ -151,7 +163,11 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
///////////////////
callResult := handler.handlerFunc.Call(callParams)
callResult, err := doCall(handler.handlerFunc, callParams)
if err != nil {
rpcError(w, &req, 0, xerrors.Errorf("fatal error calling '%s': %w", req.Method, err))
return
}
if req.ID == nil {
return // notification
}