From 5af41b62a7dad691de16e7a9f5ab5d9f7ef97933 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Mon, 28 Apr 2025 10:45:50 +0800 Subject: [PATCH] rpc: more accurate checking of handler method signatures #27287 (#990) This changes the RPC server to ignore methods using *context.Context as parameter and *error as return value type. Methods with such types would crash the server when called. Co-authored-by: Stephen Guo --- rpc/service.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/rpc/service.go b/rpc/service.go index 7de14cbae4..b3ea19f7bb 100644 --- a/rpc/service.go +++ b/rpc/service.go @@ -214,19 +214,8 @@ func (c *callback) call(ctx context.Context, method string, args []reflect.Value return results[0].Interface(), nil } -// Is t context.Context or *context.Context? -func isContextType(t reflect.Type) bool { - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - return t == contextType -} - // Does t satisfy the error interface? func isErrorType(t reflect.Type) bool { - for t.Kind() == reflect.Ptr { - t = t.Elem() - } return t.Implements(errorType) } @@ -245,7 +234,7 @@ func isPubSub(methodType reflect.Type) bool { if methodType.NumIn() < 2 || methodType.NumOut() != 2 { return false } - return isContextType(methodType.In(1)) && + return methodType.In(1) == contextType && isSubscriptionType(methodType.Out(0)) && isErrorType(methodType.Out(1)) }