pythongrpcgrpc-python

Cancel downstream gRPC requests in python server when incoming request is cancelled


Is there a simple pattern to follow to cancel all work and all downstream gRPC requests when an incoming request to a synchronous Python gRPC server is cancelled?

I assumed this was handled, but this page suggests it is not: https://github.com/grpc/grpc/tree/master/examples/python/cancellation

It's important to remember that a gRPC Python server is backed by a thread pool with a fixed size. When an RPC is cancelled, the library does not terminate your servicer thread. It is your responsibility as the application author to ensure that your servicer thread terminates soon after the RPC has been cancelled.

There they handle it by passing down a threading.Event() which seems cumbersome for a deep call stack. Are there alternatives?


Solution

  • I'm the author of the document you're referencing.

    I'm afraid there really aren't any alternatives to the application code manually instrumenting a mechanism to stop the servicer thread. This is because there is no mechanism (in Posix or Windows) to "kill a thread."

    If it's the actual plumbing of the threading.Event that you're worried about rather than the Event itself, you could use contextvars to access the Event in a thread-safe way that looks roughly like the usage of a global variable.