Merge pull request #1402 from filecoin-project/fix/jsonrpc-resanderr

Fix Error and Result being returne at the same time in JSON-RPC
This commit is contained in:
Łukasz Magiera 2020-03-16 19:31:48 +01:00 committed by GitHub
commit 9e6ec71520
2 changed files with 19 additions and 2 deletions

View File

@ -230,10 +230,17 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
} }
} }
} }
var kind reflect.Kind
var res interface{}
var nonZero bool
if handler.valOut != -1 { if handler.valOut != -1 {
resp.Result = callResult[handler.valOut].Interface() res = callResult[handler.valOut].Interface()
kind = callResult[handler.valOut].Kind()
nonZero = !callResult[handler.valOut].IsZero()
} }
if resp.Result != nil && reflect.TypeOf(resp.Result).Kind() == reflect.Chan {
if res != nil && kind == reflect.Chan {
// Channel responses are sent from channel control goroutine. // Channel responses are sent from channel control goroutine.
// Sending responses here could cause deadlocks on writeLk, or allow // Sending responses here could cause deadlocks on writeLk, or allow
// sending channel messages before this rpc call returns // sending channel messages before this rpc call returns
@ -250,6 +257,12 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
Code: 1, Code: 1,
Message: err.(error).Error(), Message: err.(error).Error(),
} }
} else if resp.Error == nil {
// check error as JSON-RPC spec prohibits error and value at the same time
resp.Result = res
}
if resp.Error != nil && nonZero {
log.Errorw("error and res returned", "request", req, "r.err", resp.Error, "res", res)
} }
w(func(w io.Writer) { w(func(w io.Writer) {

View File

@ -19,6 +19,10 @@ func (p *param) UnmarshalJSON(raw []byte) error {
} }
func (p *param) MarshalJSON() ([]byte, error) { func (p *param) MarshalJSON() ([]byte, error) {
if p.v.Kind() == reflect.Invalid {
return p.data, nil
}
return json.Marshal(p.v.Interface()) return json.Marshal(p.v.Interface())
} }