gRPC executor threads and timer thread

451 views
Skip to first unread message

Alex Zuo

unread,
Jun 29, 2021, 12:19:31 AM6/29/21
to grpc.io
For executor threads, we can use Executor::SetThreadingAll(false) to shut down. If there is no thread, it still works according to the following code.

void Executor::Enqueue(grpc_closure* closure, grpc_error_handle error,
bool is_short)
...
do {
retry_push = false;
size_t cur_thread_count =
static_cast<size_t>(gpr_atm_acq_load(&num_threads_));

// If the number of threads is zero(i.e either the executor is not threaded
// or already shutdown), then queue the closure on the exec context itself
if (cur_thread_count == 0) {
#ifndef NDEBUG
EXECUTOR_TRACE("(%s) schedule %p (created %s:%d) inline", name_, closure,
closure->file_created, closure->line_created);
#else
EXECUTOR_TRACE("(%s) schedule %p inline", name_, closure);
#endif
grpc_closure_list_append(grpc_core::ExecCtx::Get()->closure_list(),
closure, error);
return;
}

For the timer thread, there is a function to shut it down. However I cannot tell what is the impact if there is no such a thread. I also don't know the timer is used.

void grpc_timer_manager_set_threading(bool enabled);

Anybody has any insight? 

Thanks,
Alex

Jonathan Basseri

unread,
Jun 29, 2021, 2:00:36 PM6/29/21
to grpc.io
The context, following from our previous thread, is that we want to add grpc endpoints to an existing high-performance application. Our application already has extensive control over the allocations and threading on the system, so we would prefer a single-threaded grpc server that hands off async requests to our own work queue.

All of the above seems to be working in Alex's prototype, but we want to make sure that stopping these threads is not going to cause problems down the line.

1. What is the purpose of the timer thread?
2. Should everything Just Work™ even if we call `grpc_timer_manager_set_threading(false)`

Thanks,
Jonathan

yas...@google.com

unread,
Jul 8, 2021, 3:23:43 PM7/8/21
to grpc.io

1. What is the purpose of the timer thread?
Throughout the gRPC stack, there are a bunch of deadlines and timeouts that need to be tracked. The way gRPC Core does this is through timers. It schedules a closure to be executed when that timer expires and this closure is run on the timer thread.

2. Should everything Just Work™ even if we call `grpc_timer_manager_set_threading(false)`
No, it won't. :)

AJ Heller

unread,
Jul 13, 2021, 1:35:45 AM7/13/21
to grpc.io
We're adding a new API to gRPC core for exactly these kinds of situations. Please see https://github.com/grpc/proposal/pull/245 for information on the EventEngine API, I'd appreciate your feedback! In short, if you need fine-grained control over threading/eventing behaviors in gRPC, or want to hook into an external event loop, writing a custom EventEngine will give you that control. Please note this is currently experimental, the team is still working on the reference implementation, and the API will likely undergo some changes in the coming months.

Cheers,
-aj
Reply all
Reply to author
Forward
0 new messages