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 3 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
20 changes: 18 additions & 2 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 @@ -84,8 +85,8 @@ type ProxyInvoker struct {
}

// Invoke is used to call service method by invocation
func (pi *ProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) protocol.Result {
result := &protocol.RPCResult{}
func (pi *ProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) (result protocol.Result) {
result = &protocol.RPCResult{}
Copy link
Contributor

Choose a reason for hiding this comment

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

不要在返回值处定义变量,容易造成 shadow var。原来的实现挺好的。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里是为了在出现调用的exception时候,返回result要设置error,要不然没办法返回RPCResult的调用错误信息了。有没有其他方法可以在defer的recover过程中修改返回的结果呢。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

改成了在匿名函数里面用defer来获取异常,cc

result.SetAttachments(invocation.Attachments())

// get providerUrl. The origin url may be is registry URL.
Expand Down Expand Up @@ -143,6 +144,21 @@ func (pi *ProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocati
// in = append(in, replyv)
//}

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)

var retErr interface{}
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