How grpc cancell a thread/running function in server side.

2,139 views
Skip to first unread message

x...@autonomic.ai

unread,
Jul 9, 2018, 3:39:40 PM7/9/18
to grpc.io
Hi, all

How grpc cancel a thread/running function in server side.
When a request comes in the server side, a function in the server side starts to run and server this request.
But if this function run out of deadline. How grpc stop / suspend this function in the current thread?

Thanks!

Carl Mastrangelo

unread,
Jul 9, 2018, 4:24:17 PM7/9/18
to grpc.io
What language are you using?

Grpc learner

unread,
Jul 9, 2018, 6:03:49 PM7/9/18
to grpc.io
Hi Carl,
I am using Java

Carl Mastrangelo

unread,
Jul 10, 2018, 2:30:30 PM7/10/18
to grpc.io
In the server side stub you can call responseObserver.onError(Status.CANCELLED.asRuntimeException())

Grpc learner

unread,
Jul 10, 2018, 2:34:47 PM7/10/18
to grpc.io
IMO, this is for telling the client side that there is an error. But how this thread or task is terminated in server side?

Carl Mastrangelo

unread,
Jul 11, 2018, 1:46:23 PM7/11/18
to grpc.io
If the server is cancelling the RPC, you can call the observer like I showed above.  

If the client is cancelling the RPC, it depends which API you use.  

* If you are using the blocking API, you can use Context to wrap an RPC call.  Another thread can cancel that context which should terminate the RPC.   
* If you are using the Future API, you can call cancel(false) on the future.  
* If you are using the Async API, you can call the requestObserver.onError(Status.CANCELLED.asRuntimeException()), just like you could on the server.

The client side gRPC library will end the stream and send a RST_STREAM (implementation detail) to the server.  The server will either notice the cancellation via its own request observer, or by checking the Context.

Hope that helps!

Grpc learner

unread,
Jul 11, 2018, 1:51:03 PM7/11/18
to grpc.io
Thanks Carl for those very detail introductions!
Another question of mine is how grpc-java can cancel the processing, for example, the grpc function in the application server side could be nested calls, in order word, the grpc function call our functions in the server bean and then the server bean call DAO bean, how grpc cancel it if the program is in process?

Eric Anderson

unread,
Jul 11, 2018, 6:15:49 PM7/11/18
to x...@autonomic.ai, grpc-io
On Wed, Jul 11, 2018 at 10:51 AM Grpc learner <x...@autonomic.ai> wrote:
Another question of mine is how grpc-java can cancel the processing, for example, the grpc function in the application server side could be nested calls, in order word, the grpc function call our functions in the server bean and then the server bean call DAO bean, how grpc cancel it if the program is in process?

grpc-java relies on io.grpc.Context to propagate cancellations. It's sort of like a thread-local, but scoped to the current call stack. If interacting with other code, you may need to figure out a way to notify that other code of the cancellation. The Context can have listeners attached, which could do things like interrupt the thread. But Context is local only to the current process; if you do any non-gRPC communication you will probably need to adapt the Context cancellation to whatever other API you are using.

Grpc learner

unread,
Jul 13, 2018, 6:14:19 PM7/13/18
to grpc.io
Hi Eric,

Got it. Thank you so much!

Akshay Gayale

unread,
Jun 20, 2022, 6:15:36 AM6/20/22
to grpc.io
Hi, do you have any code examples here to cancel a ongoing thread on server from client? Please help. Im stuck here. 

sanjay...@google.com

unread,
Jun 24, 2022, 12:21:02 AM6/24/22
to grpc.io

Ashok Krishna

unread,
May 26, 2023, 1:38:22 AM5/26/23
to grpc.io
Hi, 

Is there a better way to this requirement other than adding multiple checkpoints in the code like the one below which would be cumbersome ?
if (observer.isCancelled()) {
    return;

sanjay...@google.com

unread,
May 26, 2023, 4:39:52 PM5/26/23
to grpc.io
You can register an onCancel handler : check this one out https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/ServerCallStreamObserver.html#setOnCancelHandler-java.lang.Runnable-  and restructure your code a bit so you can still achieve the same result.
Reply all
Reply to author
Forward
0 new messages