[Java] Correct way to terminate call in a ServerInterceptor?

496 views
Skip to first unread message

Jacques

unread,
Sep 10, 2018, 8:04:35 PM9/10/18
to grpc.io
Hello,

I have a GRPC protocol I'm working on where I want the ServerInterceptor to terminate the call if I find the metadata invalid. What is the correct way to do it while returning an appropriate status code (permission denied in this case). I'm currently calling call.close() in the interceptor but then returning the original next.startCall(). The next.startCall() throws an exception because it is trying to work against a now closed connection. The method declaration doesn't allow throwing a StatusException. I'm sure there is an example of this somewhere but I didn't run across it. Example of current code below:

public class ServerAuthInterceptor implements ServerInterceptor {


  @Override

  public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,

      ServerCallHandler<ReqT, RespT> next) {

    if (!MyThing.isValid(headers)) {

      call.close(Status.PERMISSION_DENIED, new Metadata());

      // TODO: what do we return here to avoid the exception caused by startCall below?

    }


    return next.startCall(call, headers);

  }


}



Thanks!

Evan Jones

unread,
Sep 11, 2018, 5:50:21 PM9/11/18
to grpc.io
I've never used this part of the Java implementation, but the Javadoc claims: "If the implementation throws an exception, call will be closed with an error. Implementations must not throw an exception if they started processing that may use call on another thread." https://grpc.io/grpc-java/javadoc/io/grpc/ServerInterceptor.html

Based on that and what I know about interceptors in other implementations I'm assuming you should be throwing Status.PERMISSION_DENIED.asRuntimeException() instead, and not calling the next interceptor in this case.


Hope that helps,

Evan

Carl Mastrangelo

unread,
Sep 11, 2018, 8:55:07 PM9/11/18
to grpc.io
Personally, I would return a Noop Listener and not call next.startCall.  There's an example in our testing package called NoopServerCallListener, but you should make your own, as we may change that one in the future.

Jacques Nadeau

unread,
Sep 11, 2018, 8:56:51 PM9/11/18
to not...@google.com, grp...@googlegroups.com
Got it, thanks!

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/096d69d5-f859-4183-8e5e-245fa78fc20e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages