diff options
author | 2018-03-08 15:47:40 -0800 | |
---|---|---|
committer | 2018-04-30 17:40:59 -0700 | |
commit | 38ab21ee0996a54c682488bcf43ecf5ba0f7f24a (patch) | |
tree | c65531bc89dd6916896c3dee0de1c6aab17819f1 /src/core/lib/iomgr/fork_posix.cc | |
parent | 8b875ac9413978370c4eafa2e2fb6a3b2f054297 (diff) |
Add exec_ctx check to fork handlers
Diffstat (limited to 'src/core/lib/iomgr/fork_posix.cc')
-rw-r--r-- | src/core/lib/iomgr/fork_posix.cc | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/src/core/lib/iomgr/fork_posix.cc b/src/core/lib/iomgr/fork_posix.cc index f8645ab157..dd0f9f612f 100644 --- a/src/core/lib/iomgr/fork_posix.cc +++ b/src/core/lib/iomgr/fork_posix.cc @@ -41,47 +41,59 @@ * AROUND VERY SPECIFIC USE CASES. */ +namespace { +bool skipped_handler = true; +bool registered_handlers = false; +} // namespace + void grpc_prefork() { + grpc_core::ExecCtx exec_ctx; + skipped_handler = true; + if (!grpc_is_initialized()) { + return; + } if (!grpc_fork_support_enabled()) { gpr_log(GPR_ERROR, "Fork support not enabled; try running with the " "environment variable GRPC_ENABLE_FORK_SUPPORT=1"); return; } - if (grpc_is_initialized()) { - grpc_core::ExecCtx exec_ctx; - grpc_timer_manager_set_threading(false); - grpc_executor_set_threading(false); - grpc_core::ExecCtx::Get()->Flush(); - if (!grpc_core::Thread::AwaitAll( - gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(3, GPR_TIMESPAN)))) { - gpr_log(GPR_ERROR, "gRPC thread still active! Cannot fork!"); - } + if (!grpc_fork_block_exec_ctx()) { + gpr_log(GPR_INFO, + "Other threads are currently calling into gRPC, skipping fork() " + "handlers"); + return; } + grpc_timer_manager_set_threading(false); + grpc_executor_set_threading(false); + grpc_core::ExecCtx::Get()->Flush(); + grpc_fork_await_thds(); + skipped_handler = false; } void grpc_postfork_parent() { - if (grpc_is_initialized()) { - grpc_timer_manager_set_threading(true); + if (!skipped_handler) { + grpc_fork_allow_exec_ctx(); grpc_core::ExecCtx exec_ctx; + grpc_timer_manager_set_threading(true); grpc_executor_set_threading(true); } } void grpc_postfork_child() { - if (grpc_is_initialized()) { - grpc_timer_manager_set_threading(true); + if (!skipped_handler) { + grpc_fork_allow_exec_ctx(); grpc_core::ExecCtx exec_ctx; + grpc_timer_manager_set_threading(true); grpc_executor_set_threading(true); - grpc_core::ExecCtx::Get()->Flush(); } } void grpc_fork_handlers_auto_register() { - if (grpc_fork_support_enabled()) { + if (grpc_fork_support_enabled() & !registered_handlers) { #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK pthread_atfork(grpc_prefork, grpc_postfork_parent, grpc_postfork_child); + registered_handlers = true; #endif // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK } } |