From a21ff713732d40e176de11e0face1e16d12d596a Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 15 Aug 2021 15:33:45 +0800 Subject: [PATCH] fix #889 (#912) --- core/fx/timeout.go | 6 +++++- zrpc/internal/serverinterceptors/crashinterceptor.go | 2 +- zrpc/internal/serverinterceptors/timeoutinterceptor.go | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/core/fx/timeout.go b/core/fx/timeout.go index f8caec28..66c61870 100644 --- a/core/fx/timeout.go +++ b/core/fx/timeout.go @@ -2,6 +2,9 @@ package fx import ( "context" + "fmt" + "runtime/debug" + "strings" "time" ) @@ -30,7 +33,8 @@ func DoWithTimeout(fn func() error, timeout time.Duration, opts ...DoOption) err go func() { defer func() { if p := recover(); p != nil { - panicChan <- p + // attach call stack to avoid missing in different goroutine + panicChan <- fmt.Sprintf("%+v\n\n%s", p, strings.TrimSpace(string(debug.Stack()))) } }() done <- fn() diff --git a/zrpc/internal/serverinterceptors/crashinterceptor.go b/zrpc/internal/serverinterceptors/crashinterceptor.go index e3126860..77e7a361 100644 --- a/zrpc/internal/serverinterceptors/crashinterceptor.go +++ b/zrpc/internal/serverinterceptors/crashinterceptor.go @@ -39,6 +39,6 @@ func handleCrash(handler func(interface{})) { } func toPanicError(r interface{}) error { - logx.Errorf("%+v %s", r, debug.Stack()) + logx.Errorf("%+v\n\n%s", r, debug.Stack()) return status.Errorf(codes.Internal, "panic: %v", r) } diff --git a/zrpc/internal/serverinterceptors/timeoutinterceptor.go b/zrpc/internal/serverinterceptors/timeoutinterceptor.go index c763b285..83ed9977 100644 --- a/zrpc/internal/serverinterceptors/timeoutinterceptor.go +++ b/zrpc/internal/serverinterceptors/timeoutinterceptor.go @@ -2,6 +2,9 @@ package serverinterceptors import ( "context" + "fmt" + "runtime/debug" + "strings" "sync" "time" @@ -24,7 +27,8 @@ func UnaryTimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor go func() { defer func() { if p := recover(); p != nil { - panicChan <- p + // attach call stack to avoid missing in different goroutine + panicChan <- fmt.Sprintf("%+v\n\n%s", p, strings.TrimSpace(string(debug.Stack()))) } }()