[Java] Clean way to detect client crash

93 views
Skip to first unread message

julien...@sonarsource.com

unread,
Jul 10, 2017, 9:03:25 AM7/10/17
to grpc.io
Hi,

We are using grpc for C# <-> Java integration. The C# client application will start the Java process containing the grpc server, and then create a Chanel used for calling methods on Java server side.

One problem we have is to properly terminate the Java server in case the C# client crashed badly. In the current state, the Java process keeps running in the background forever.

Is there a way to be notified on server side when a client died?

Thanks

Julien

Eric Anderson

unread,
Jul 11, 2017, 7:43:14 PM7/11/17
to julien...@sonarsource.com, grpc.io
I'd suggest the C# client creates a long-running RPC to the Java server, and the RPC will be seen as cancelled on the server when the connection is broken. I don't believe you need it in your case, but in similar cases you'd want to enable keepalive on the server-side.

julien...@sonarsource.com

unread,
Jul 12, 2017, 5:59:28 AM7/12/17
to grpc.io, julien...@sonarsource.com
Hi Eric,

Thanks for your idea. Here is how I have implemented it, tell me if this is correct:

rpc HeartBeat(stream Void) returns Void;

C# Client do:
using (var heartBeat = client.HeartBeat(new CallOptions(null, null, channel.ShutdownToken).WithWaitForReady(true)))
{
    await heartBeat
.ResponseAsync;
}



On Java server side:

 
  @Override
 
public StreamObserver<Void> heartBeat(StreamObserver<Void> responseObserver) {
   
return new StreamObserver<Void>() {

     
@Override
     
public void onNext(Void value) {
       
// Nothing to do
     
}

     
@Override
     
public void onError(Throwable t) {
        shutdown
();
     
}

     
@Override
     
public void onCompleted() {
        shutdown
();
     
}
   
};
 
}


Does it seems correct for you? Why would have to enable keepalive?

Thanks,

Julien

Eric Anderson

unread,
Aug 2, 2017, 11:16:23 AM8/2/17
to Julien HENRY, grpc.io
On Wed, Jul 12, 2017 at 2:59 AM, <julien...@sonarsource.com> wrote:
Thanks for your idea. Here is how I have implemented it, tell me if this is correct:

Yeah. That looked good.

Why would have to enable keepalive?

If a connection doesn't have any writes then TCP won't be able to detect breakages. So I normally recommend enabling keepalive when you have a long-lived stream with little activity, as it will trigger writes. However, in your case you are local, so we'd expect the connection to be reliable and provide a prompt notification if the connection is closed (and thus keepalive probably is unnecessary). 
Reply all
Reply to author
Forward
0 new messages