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

fix: complete grpc based protocol panic recover handle. #1866

Merged
merged 7 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions common/proxy/proxy_factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package proxy_factory

import (
"context"
"fmt"
"reflect"
"strings"
)
Expand Down Expand Up @@ -137,21 +138,39 @@ func (pi *ProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocati

// prepare replyv
var replyv reflect.Value
var retErr interface{}
//if method.ReplyType() == nil && len(method.ArgsType()) > 0 {
//
// replyv = reflect.New(method.ArgsType()[len(method.ArgsType())-1].Elem())
// in = append(in, replyv)
//}

returnValues := method.Method().Func.Call(in)
func() {
// Handle invoke exception in user func.
defer func() {
if e := recover(); e != nil {
if err, ok := e.(error); ok {
logger.Errorf("Invoke function error: %+v, service: %#v", perrors.WithStack(err), url)
result.SetError(e.(error))
} else if err, ok := e.(string); ok {
logger.Errorf("Invoke function error: %+v, service: %#v", perrors.New(err), url)
result.SetError(perrors.New(err))
} else {
logger.Errorf("Invoke function error: %+v, this is impossible. service: %#v", e, url)
result.SetError(fmt.Errorf("invoke function error, unknow exception: %+v", e))
}
}
}()
returnValues := method.Method().Func.Call(in)

if len(returnValues) == 1 {
retErr = returnValues[0].Interface()
} else {
replyv = returnValues[0]
retErr = returnValues[1].Interface()
}
}()

var retErr interface{}
if len(returnValues) == 1 {
retErr = returnValues[0].Interface()
} else {
replyv = returnValues[0]
retErr = returnValues[1].Interface()
}
if retErr != nil {
result.SetError(retErr.(error))
} else {
Expand Down
27 changes: 23 additions & 4 deletions common/proxy/proxy_factory/pass_through.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package proxy_factory

import (
"context"
"fmt"
"reflect"
)

Expand All @@ -30,6 +31,7 @@ import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/common/proxy"
"dubbo.apache.org/dubbo-go/v3/protocol"
)
Expand Down Expand Up @@ -106,11 +108,28 @@ func (pi *PassThroughProxyInvoker) Invoke(ctx context.Context, invocation protoc
in = append(in, reflect.ValueOf(args))
in = append(in, reflect.ValueOf(invocation.Attachments()))

returnValues := method.Method().Func.Call(in)

var replyv reflect.Value
var retErr interface{}
replyv := returnValues[0]
retErr = returnValues[1].Interface()
func() {
// Handle invoke exception in user func.
defer func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和default 是一样的defer func, 提取出一个共用的func吧

if e := recover(); e != nil {
if err, ok := e.(error); ok {
logger.Errorf("Invoke function error: %+v, service: %#v", perrors.WithStack(err), url)
result.SetError(e.(error))
} else if err, ok := e.(string); ok {
logger.Errorf("Invoke function error: %+v, service: %#v", perrors.New(err), url)
result.SetError(perrors.New(err))
} else {
logger.Errorf("Invoke function error: %+v, this is impossible. service: %#v", e, url)
result.SetError(fmt.Errorf("invoke function error, unknow exception: %+v", e))
}
}
}()
returnValues := method.Method().Func.Call(in)
replyv = returnValues[0]
retErr = returnValues[1].Interface()
}()

if retErr != nil {
result.SetError(retErr.(error))
Expand Down
23 changes: 1 addition & 22 deletions remoting/getty/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package getty

import (
"fmt"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -265,27 +264,6 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) {
return
}

defer func() {
if e := recover(); e != nil {
resp.Status = hessian.Response_SERVER_ERROR
if err, ok := e.(error); ok {
logger.Errorf("OnMessage panic: %+v, req: %#v", perrors.WithStack(err), req.Data)
resp.Error = perrors.WithStack(err)
} else if err, ok := e.(string); ok {
logger.Errorf("OnMessage panic: %+v, req: %#v", perrors.New(err), req.Data)
resp.Error = perrors.New(err)
} else {
logger.Errorf("OnMessage panic: %+v, this is impossible. req: %#v", e, req.Data)
resp.Error = fmt.Errorf("OnMessage panic unknow exception. %+v", e)
}

if !req.TwoWay {
return
}
reply(session, resp)
}
}()

invoc, ok := req.Data.(*invocation.RPCInvocation)
if !ok {
panic("create invocation occur some exception for the type is not suitable one.")
Expand All @@ -299,6 +277,7 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) {
return
}
resp.Result = result

reply(session, resp)
}

Expand Down