A way to cancel a streaming RPC without "cancel"

1,053 views
Skip to first unread message

robert...@gmail.com

unread,
Jan 15, 2021, 3:44:03 AM1/15/21
to grpc.io
I'm writing a proxy which is put between a gRPC client and a server, which need to implement a bidirectional streaming RPC.

Problem is that reading the client stream can only be cancelled with context->TryCancel(). And this will make the RPC return status CANCELLED (in the client) regardless of what actually is returned in the server RPC itself.

Any idea on how to interrupt the stream->Read(...) call without TryCancel()? Is it possible to have a polled Read of the stream, so I can monitor and interrupt the RPC call?

Regards
/R

robert...@gmail.com

unread,
Jan 15, 2021, 4:58:54 AM1/15/21
to grpc.io
I'm trying to use server trailing metadata (https://chromium.googlesource.com/external/github.com/grpc/grpc/+/HEAD/examples/cpp/metadata).

But the metadata doesn't seem to be propagated to the client unless the return status is "OK" ?

Vijay Pai

unread,
May 19, 2021, 12:49:32 PM5/19/21
to grpc.io
Hi there,

Cancellation should really be considered an out-of-band unclean termination of the stream. There isn't even a library-level guarantee at that point of what has been seen or not seen, and a cancellation terminates both sides of the stream at the transport level so that the server can't even send trailing metadata on the wire (though you still have to give status for the RPC for library-level cleanup). So I'd really recommend not using it if you want a clean recovery. Instead, consider changing your application-layer protocol. For example, if you're using protobuf, you could change your message type to be a oneof between your existing message type and a new "CleanCancel" message so that way the Read will succeed and then you'll see that it was a CleanCancel message. If you're using the generic API as many proxies are likely to do, you could just instrument your proxy messages with an enum at the beginning to specify the type of message being sent and that would identify the cancellation. In other words, if you want a clean, graceful termination you need to do it via messages and not cancellation.

- vjpai
Reply all
Reply to author
Forward
0 new messages