-
Notifications
You must be signed in to change notification settings - Fork 769
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
Returning null from a grpc interceptor when there is a failure status code #1555
Comments
Throw a RpcException to terminate a call with an error from within an interceptor. With the current APIs, you have to throw an exception. There is no non-exceptional way to terminate a call from within an interceptor. |
What is the motivation for this block of code? if (response == null)
{
// This is consistent with Grpc.Core when a null value is returned
throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
} Afaik it's not always the case that there will be a response. There can be no response if something went wrong. That doesn't mean the request was Cancelled. I might have set appropriate status code and error messages using ServerCallContext. |
As the comment mentions, it is to be consistent with Grpc.Core. Either a response should be returned, or an RpcException should be thrown. Returning a null response is not supported. |
Is it only to be consistent with Grpc.Core? because I cant make sense of using StatusCode.Cancelled when nothing is returned from an rpc call. |
Yes |
@JamesNK Checking the status code for ok code would be a good option. if (response == null && serverCallContext.Status.StatusCode == StatusCode.OK)
{
throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
} because if the status code is not OK that means it has been set manually. So why mess with that then? |
And what about the code below it that then writes the message? It will error because response is null. If you want to return a status then throw |
I think there is a reason. Most of the time one of the reasons for using grpc over rest is performance. Exceptions are not really the performant way because it breaks the normal code flow. Yeah, the performance difference for the exceptions is most of the time is unnoticeable and negligible. But what about other times? Also, exceptions can make code harder to read because of its nature of breaking the flow. For these reasons you can see that the usage of exceptions is really been reduced from Asp.Net to Asp.Net Core. You want to throw an exception when something went wrong in your application(not in the request). |
gRPC doesn't use exceptions in typical application flow. If an HTTP request is being canceled, causing gRPC to throw a RpcException, an exception being thrown will have no impact at all on performance. |
Yea. But I was talking about keeping both ways open. Sending status code manually and throwing RpcException |
@jtattermusch Do you have any thoughts about this? Should a unary method be allowed to set the status on the ServerCallContext, return null, and have the status be sent in the response to the client? |
We use MessagePack for However, as discussed here, Grpc.AspNetCore.Server will block if it receives |
It can be changed. or can't? |
I think that's a different issue from what's being discussed here though. Generally, we didn't allow messages to be be |
So I don't think there's much value in supporting
This will do exactly what you need and it won't require returning what's otherwise and illegal return value (null message). The argument about performance doesn't apply here since throwing and catching an exception has completely negligible overhead compared to starting/handling an RPC. I'd very surprised if you saw any noticeable difference in a benchmark.
(and if you're inside a regular server side handler, returning a default instance of a message as a dummy response is even simpler). The problem with allowing I suggest closing this issue as it doesn't really limit the usability. |
Closing as I don't think the cost of this change is worth any potential benefit. Additionally, the workaround Jan mentioned, to return an empty dummy object, is available. We'd need to see real-world scenarios where this exception has a noticeable impact on performance to consider this further. |
Why can't return null from an interceptor there is a failure status code.
I don't think I'm supposed to return a response when there is a failure status code. Or do I? When I return null with failure status code I get this error message
The text was updated successfully, but these errors were encountered: