How to terminate a C++ client

58 views
Skip to first unread message

michi....@crowdstrike.com

unread,
Oct 31, 2018, 11:12:42 PM10/31/18
to grpc.io
I have a C++ client that reads data from a stream using a ClientReader. The client runs a separate thread that effectively does:

while (reader_stream->Read(buf)) {
    // Do something with buf
}

This works just fine, no problem.

At some point, my process receives a message from an external program (not via gRPC) that instructs it to shut down. The problem is that I can't exit from the read loop because the client thread is stuck in its Read() call, and the server may not send another message for hours.

I need to cancel the Read() call from another thread in my client, but I can't find anything in the API to do this. What am I missing?

Thanks,

Michi.

michi....@crowdstrike.com

unread,
Nov 5, 2018, 2:21:38 AM11/5/18
to grpc.io
On Thursday, November 1, 2018 at 1:12:42 PM UTC+10, michi....@crowdstrike.com wrote:

I need to cancel the Read() call from another thread in my client, but I can't find anything in the API to do this. What am I missing?

No suggestions? Anyone?

Prashant Shubham

unread,
Nov 5, 2018, 5:02:22 AM11/5/18
to grpc.io
Have a look at this discussion: https://groups.google.com/forum/#!topic/grpc-io/hahFSzQBN4s . Hope it helps.

michi....@crowdstrike.com

unread,
Nov 7, 2018, 2:21:51 AM11/7/18
to grpc.io


On Monday, November 5, 2018 at 8:02:22 PM UTC+10, Prashant Shubham wrote:
Have a look at this discussion: https://groups.google.com/forum/#!topic/grpc-io/hahFSzQBN4s . Hope it helps.

Thank you! Calling TryCancel() on the call context does the trick.

It would be a really good idea to add some documentation for this. (It isn't mentioned in the tutorial or examples, as far as I could.) I spent quite a lot of time looking for a way to do this and never found it because it is one method of many that is buried in the call context.

michi....@crowdstrike.com

unread,
Nov 7, 2018, 2:49:29 AM11/7/18
to grpc.io
Thinking about this again, having TryCancel() on the call context is truly surprising to me. It's spooky action at a distance. The call context is used once when the stream is established, and then typically forgotten, and the data is received by calling a method on the stream. So, why isn't TryCancel() on the stream?

What I find particularly jarring is that WritesDone() is on the stream, but TryCancel() is not. It seems that both methods should be in the same place (preferably the stream, rather than the context).
Reply all
Reply to author
Forward
0 new messages