Client disconnect detection over keepAlive?

133 views
Skip to first unread message

Dmitry Gorelov

unread,
Jul 5, 2023, 9:30:07 AM7/5/23
to grpc.io
Hello,
Please help to setup the detection of the client disconnect on a server side.

and my code on server side is:
...
ServerBuilder builder;
  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  builder.RegisterService(this);
  builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIME_MS, 30000);
  builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIMEOUT_MS, 2000);
  builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS, 1);
 builder.AddChannelArgument(GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS, 9000);
  builder.AddChannelArgument(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA, 0);
  builder.AddChannelArgument(GRPC_ARG_HTTP2_MAX_PING_STRIKES, 2);
  builder.AddChannelArgument(GRPC_ARG_MAX_MESSAGE_LENGTH, 1024 * 1024 * 8); 
etc...

and on client side is:
grpc::ChannelArguments args;
    args.SetLoadBalancingPolicyName("round_robin");
    args.SetMaxReceiveMessageSize(MAX_RECEIVE_MESSAGE_SIZE);
    args.SetMaxSendMessageSize(MAX_SEND_MESSAGE_SIZE);
    args.SetInt(GRPC_ARG_KEEPALIVE_TIME_MS, 30000);
    args.SetInt(GRPC_ARG_KEEPALIVE_TIMEOUT_MS, 2000);
    args.SetInt(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS, 1);
    args.SetInt(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA, 0);
    m_channel = grpc::CreateCustomChannel(target, grpc::InsecureChannelCredentials(), args);
    auto ptr_stub = Rnd::NewStub(m_channel);
etc...

But when the client application closes, nothing happens on the server side.
It is built with gRPC Callback API.

I also wondering what should I expect on a server side as an indication of the fact that the client has disconnected... Any callback to overwrite? Any server's property to check periodically?

Thank you.

Dmitry Gorelov

unread,
Jul 25, 2023, 9:54:00 AM7/25/23
to grpc.io
Could you please help me to understand the subject? 
If no telegrams sent for some time, but connection is alive until the very moment when the client closed, 
how would the server detect that client dead without sending any grpc message to him?
I use callback grpc api, v1.56


среда, 5 июля 2023 г. в 16:30:07 UTC+3, Dmitry Gorelov:

Dmitry Gorelov

unread,
Jul 27, 2023, 8:12:57 AM7/27/23
to grpc.io
I found OnCancel() method in  grpc::ServerWriteReactor   and override it. So now I get a notification when the client disconnects.
Unfortunatelly, there is no single example in grpc foler that could show me how this function is to be used.

The magor problem is, how to relesase the instance of the grpc::ServerWriteReactor to free the resources, after the OnCancel() triggered?

If I delete the instance itself from the OnDone() callback, it works.
If I do the same from OnCancel, I get exceptions in grpc internal modules.

Could you point me to the brief example how to delete the instance after grpc::ServerWriteReactor::OnCancel() triggered?

вторник, 25 июля 2023 г. в 16:54:00 UTC+3, Dmitry Gorelov:

Adam Heller

unread,
Sep 11, 2025, 1:38:22 PMSep 11
to grpc.io
This example illustrates one way to free resources in a server that overrides the OnCancel method: https://github.com/grpc/grpc/blob/7017c09824b0806a69d2b62912a1dd830a057009/examples/cpp/route_guide/route_guide_callback_server.cc

You may benefit from reading the Callback API documentation, specifically the guarantees around when OnCancel and when OnDone must be called. https://github.com/grpc/proposal/blob/master/L67-cpp-callback-api.md

Best,
Adam

Reply all
Reply to author
Forward
0 new messages