Long lived bidirectional channel - how to communicate client errors

139 views
Skip to first unread message

Matt Mitchell

unread,
Oct 27, 2017, 8:38:07 PM10/27/17
to grpc.io
I have an application where the client establishes a bidirectional connection to the server, and the server begins sending requests to the client and vice versa. This app runs jobs, so that once a job starts, the messages flow back and forth from server to client and client to server. I would like to gracefully deal with exceptions thrown from the client. I'm not entirely sure if I should be calling onError for non-critical errors, or if I create a new Error message type and send via onNext. If I use onError, then the client needs to re-connect with a new Channel -- What is the better way to deal with this problem? Is there another way?

Related question - for the Java impl of gRPC - is it possible to have the server log the stack trace of an error sent by the client via onError? I'm setting the cause on the io.grpc.Status instance, but it doesn't seem to be available on the receiving end.

Thanks!
- Matt

matt.m...@lucidworks.com

unread,
Oct 30, 2017, 12:44:02 PM10/30/17
to grpc.io
OK I've answered one of my questions, which is that the Status exception clearly states in the JavaDoc that it does not send the strack-trace with the error.

Looking more into the other question (how to properly send errors from the client) - it seems as though no matter what error I send from the client (using onError) - the server always gets a CANCELLED status, and no details from the actual errors I send. This happens regardless of the type of Status error - is this the correct behavior?

- Matt

Arpit Baldeva

unread,
Oct 30, 2017, 2:03:33 PM10/30/17
to grpc.io
Check out https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/grpc-io/k6QFNxWDmv0/AVpTJ3LMEAAJ to see how we deal with this particular problem (Surfacing bi-directional streaming error without breaking the stream). 

Carl Mastrangelo

unread,
Oct 30, 2017, 8:45:17 PM10/30/17
to grpc.io
Under the hood, the Client sends a RST_STREAM HTTP/2 frame with the cancelled error code.  If you want to gracefully stop, you can send a halfClose() from the client and make the server interpret that as the end.  Alternatively, you can try to send one last error message, and then call onError.  

The client and the server are asymmetric in this regard; the server can send back more detailed info, while the client cannot.


On Monday, October 30, 2017 at 9:44:02 AM UTC-7, matt.m...@lucidworks.com wrote:

matt.m...@lucidworks.com

unread,
Oct 31, 2017, 11:19:25 AM10/31/17
to grpc.io
Got it, thanks Carl - that helps a lot. Can you point me to an example of how you'd call halfClose()?

- Matt

Carl Mastrangelo

unread,
Oct 31, 2017, 1:22:39 PM10/31/17
to grpc.io
It's present on the ClientCall object, which are wrapped by StreamObservers.  The ClientCall API is more advanced so most people don't use it.   If you look at the generated stubs, you can see how to create a client call.   All you need to do is not to wrap it in a StreamObserver.

matt.m...@lucidworks.com

unread,
Oct 31, 2017, 1:24:28 PM10/31/17
to grpc.io
Perfect, thanks again.

- Matt

Eric Anderson

unread,
Nov 1, 2017, 7:23:57 PM11/1/17
to Matt Mitchell, grpc.io
Actually, StreamObserver.onCompleted() is the same as ClientCall.halfClose() on client-side outbound. So to say you're done to the server just call onCompleted() on the client. You should be able to use StreamObserver (but if you like ClientCall better, that's fine, too).

Note however, after you call StreamObserver.onCompleted() you can't technically call StreamObserver.onError() if you want to cancel (say, graceful termination is taking too long). To cancel, cast the StreamObserver to ClientCallStreamObserver (or make your StreamObserver implement ClientResponseObserver) and call cancel().

"If I use onError, then the client needs to re-connect with a new Channel". Note you should be able to reuse the Channel and gRPC can reuse the connection after you call onError().

--
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+unsubscribe@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/7445b97e-1095-422c-8458-e5ca96f7ee06%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages