[fork] Move init to once-only, remove shutdown (#30839)

* [fork] Move init to once-only, remove shutdown

* fix build

* Automated change: Fix sanity tests

Co-authored-by: ctiller <ctiller@users.noreply.github.com>
pull/30891/head
Craig Tiller 3 years ago committed by GitHub
parent fb4fd9ced0
commit ec27e98adc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      BUILD
  2. 1
      CMakeLists.txt
  3. 15
      build_autogenerated.yaml
  4. 36
      src/core/lib/gprpp/fork.cc
  5. 8
      src/core/lib/gprpp/fork.h
  6. 7
      src/core/lib/surface/init.cc
  7. 6
      test/core/gprpp/fork_test.cc

@ -1033,6 +1033,7 @@ grpc_cc_library(
"examine_stack",
"gpr_tls",
"grpc_codegen",
"no_destruct",
"tchar",
"useful",
],
@ -1262,7 +1263,7 @@ grpc_cc_library(
public_hdrs = ["src/core/lib/gprpp/no_destruct.h"],
deps = [
"construct_destruct",
"gpr",
"gpr_platform",
],
)

1
CMakeLists.txt generated

@ -13747,7 +13747,6 @@ target_include_directories(no_destruct_test
target_link_libraries(no_destruct_test
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_ALLTARGETS_LIBRARIES}
gpr
)

@ -226,6 +226,7 @@ libs:
- src/core/lib/gprpp/manual_constructor.h
- src/core/lib/gprpp/memory.h
- src/core/lib/gprpp/mpscq.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/stat.h
- src/core/lib/gprpp/sync.h
- src/core/lib/gprpp/tchar.h
@ -804,7 +805,6 @@ libs:
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/dual_ref_counted.h
- src/core/lib/gprpp/match.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/overload.h
- src/core/lib/gprpp/packed_table.h
@ -2027,7 +2027,6 @@ libs:
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/dual_ref_counted.h
- src/core/lib/gprpp/match.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/overload.h
- src/core/lib/gprpp/packed_table.h
@ -3705,7 +3704,6 @@ targets:
- src/core/lib/gprpp/atomic_utils.h
- src/core/lib/gprpp/bitset.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h
@ -4838,7 +4836,6 @@ targets:
- src/core/lib/gprpp/chunked_vector.h
- src/core/lib/gprpp/cpp_impl_of.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h
@ -5811,7 +5808,6 @@ targets:
- src/core/lib/gprpp/atomic_utils.h
- src/core/lib/gprpp/bitset.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h
@ -6069,7 +6065,6 @@ targets:
- src/core/lib/gprpp/bitset.h
- src/core/lib/gprpp/cpp_impl_of.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h
@ -6159,7 +6154,6 @@ targets:
- src/core/lib/gprpp/bitset.h
- src/core/lib/gprpp/cpp_impl_of.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h
@ -7486,7 +7480,6 @@ targets:
- src/core/lib/gprpp/atomic_utils.h
- src/core/lib/gprpp/bitset.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h
@ -7840,11 +7833,11 @@ targets:
build: test
language: c++
headers:
- src/core/lib/gprpp/construct_destruct.h
- src/core/lib/gprpp/no_destruct.h
src:
- test/core/gprpp/no_destruct_test.cc
deps:
- gpr
deps: []
uses_polling: false
- name: nonblocking_test
gtest: true
@ -7875,7 +7868,6 @@ targets:
headers:
- src/core/lib/gprpp/atomic_utils.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h
@ -8146,7 +8138,6 @@ targets:
- src/core/lib/gprpp/bitset.h
- src/core/lib/gprpp/cpp_impl_of.h
- src/core/lib/gprpp/debug_location.h
- src/core/lib/gprpp/no_destruct.h
- src/core/lib/gprpp/orphanable.h
- src/core/lib/gprpp/ref_counted.h
- src/core/lib/gprpp/ref_counted_ptr.h

@ -26,6 +26,7 @@
#include <grpc/support/time.h>
#include "src/core/lib/gprpp/global_config_env.h"
#include "src/core/lib/gprpp/no_destruct.h"
/*
* NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
@ -43,7 +44,7 @@ GPR_GLOBAL_CONFIG_DEFINE_BOOL(grpc_enable_fork_support,
"Enable fork support");
namespace grpc_core {
namespace internal {
namespace {
// The exec_ctx_count has 2 modes, blocked and unblocked.
// When unblocked, the count is 2-indexed; exec_ctx_count=2 indicates
// 0 active ExecCtxs, exex_ctx_count=3 indicates 1 active ExecCtxs...
@ -161,24 +162,13 @@ class ThreadState {
int count_;
};
} // namespace internal
} // namespace
void Fork::GlobalInit() {
if (!override_enabled_) {
support_enabled_.store(GPR_GLOBAL_CONFIG_GET(grpc_enable_fork_support),
std::memory_order_relaxed);
}
if (support_enabled_.load(std::memory_order_relaxed)) {
exec_ctx_state_ = new internal::ExecCtxState();
thread_state_ = new internal::ThreadState();
}
}
void Fork::GlobalShutdown() {
if (support_enabled_.load(std::memory_order_relaxed)) {
delete exec_ctx_state_;
delete thread_state_;
}
}
bool Fork::Enabled() {
@ -191,9 +181,13 @@ void Fork::Enable(bool enable) {
support_enabled_.store(enable, std::memory_order_relaxed);
}
void Fork::DoIncExecCtxCount() { exec_ctx_state_->IncExecCtxCount(); }
void Fork::DoIncExecCtxCount() {
NoDestructSingleton<ExecCtxState>::Get()->IncExecCtxCount();
}
void Fork::DoDecExecCtxCount() { exec_ctx_state_->DecExecCtxCount(); }
void Fork::DoDecExecCtxCount() {
NoDestructSingleton<ExecCtxState>::Get()->DecExecCtxCount();
}
void Fork::SetResetChildPollingEngineFunc(
Fork::child_postfork_func reset_child_polling_engine) {
@ -205,36 +199,34 @@ Fork::child_postfork_func Fork::GetResetChildPollingEngineFunc() {
bool Fork::BlockExecCtx() {
if (support_enabled_.load(std::memory_order_relaxed)) {
return exec_ctx_state_->BlockExecCtx();
return NoDestructSingleton<ExecCtxState>::Get()->BlockExecCtx();
}
return false;
}
void Fork::AllowExecCtx() {
if (support_enabled_.load(std::memory_order_relaxed)) {
exec_ctx_state_->AllowExecCtx();
NoDestructSingleton<ExecCtxState>::Get()->AllowExecCtx();
}
}
void Fork::IncThreadCount() {
if (support_enabled_.load(std::memory_order_relaxed)) {
thread_state_->IncThreadCount();
NoDestructSingleton<ThreadState>::Get()->IncThreadCount();
}
}
void Fork::DecThreadCount() {
if (support_enabled_.load(std::memory_order_relaxed)) {
thread_state_->DecThreadCount();
NoDestructSingleton<ThreadState>::Get()->DecThreadCount();
}
}
void Fork::AwaitThreads() {
if (support_enabled_.load(std::memory_order_relaxed)) {
thread_state_->AwaitThreads();
NoDestructSingleton<ThreadState>::Get()->AwaitThreads();
}
}
internal::ExecCtxState* Fork::exec_ctx_state_ = nullptr;
internal::ThreadState* Fork::thread_state_ = nullptr;
std::atomic<bool> Fork::support_enabled_(false);
bool Fork::override_enabled_ = false;
Fork::child_postfork_func Fork::reset_child_polling_engine_ = nullptr;

@ -30,17 +30,11 @@
namespace grpc_core {
namespace internal {
class ExecCtxState;
class ThreadState;
} // namespace internal
class Fork {
public:
typedef void (*child_postfork_func)(void);
static void GlobalInit();
static void GlobalShutdown();
// Returns true if fork suppport is enabled, false otherwise
static bool Enabled();
@ -91,8 +85,6 @@ class Fork {
static void DoIncExecCtxCount();
static void DoDecExecCtxCount();
static internal::ExecCtxState* exec_ctx_state_;
static internal::ThreadState* thread_state_;
static std::atomic<bool> support_enabled_;
static bool override_enabled_;
static child_postfork_func reset_child_polling_engine_;

@ -122,6 +122,9 @@ static void do_basic_init(void) {
grpc_register_built_in_plugins();
gpr_time_init();
grpc_core::PrintExperimentsList();
grpc_core::Fork::GlobalInit();
grpc_event_engine::experimental::RegisterForkHandlers();
grpc_fork_handlers_auto_register();
grpc_tracer_init();
}
@ -151,9 +154,6 @@ void grpc_init(void) {
g_shutting_down = false;
g_shutting_down_cv->SignalAll();
}
grpc_core::Fork::GlobalInit();
grpc_event_engine::experimental::RegisterForkHandlers();
grpc_fork_handlers_auto_register();
grpc_iomgr_init();
for (int i = 0; i < g_number_of_plugins; i++) {
if (g_all_of_the_plugins[i].init != nullptr) {
@ -182,7 +182,6 @@ void grpc_shutdown_internal_locked(void)
}
grpc_event_engine::experimental::ResetDefaultEventEngine();
grpc_iomgr_shutdown();
grpc_core::Fork::GlobalShutdown();
}
g_shutting_down = false;
g_shutting_down_cv->SignalAll();

@ -29,19 +29,16 @@ TEST(ForkTest, Init) {
// Default fork support (disabled)
grpc_core::Fork::GlobalInit();
ASSERT_FALSE(grpc_core::Fork::Enabled());
grpc_core::Fork::GlobalShutdown();
// Explicitly disabled fork support
grpc_core::Fork::Enable(false);
grpc_core::Fork::GlobalInit();
ASSERT_FALSE(grpc_core::Fork::Enabled());
grpc_core::Fork::GlobalShutdown();
// Explicitly enabled fork support
grpc_core::Fork::Enable(true);
grpc_core::Fork::GlobalInit();
ASSERT_TRUE(grpc_core::Fork::Enabled());
grpc_core::Fork::GlobalShutdown();
}
// This spawns CONCURRENT_TEST_THREADS that last up to
@ -63,7 +60,6 @@ TEST(ForkTest, ThdCount) {
grpc_core::Fork::Enable(true);
grpc_core::Fork::GlobalInit();
grpc_core::Fork::AwaitThreads();
grpc_core::Fork::GlobalShutdown();
grpc_core::Fork::Enable(true);
grpc_core::Fork::GlobalInit();
@ -86,7 +82,6 @@ TEST(ForkTest, ThdCount) {
thd.Join();
}
ASSERT_TRUE(gpr_time_similar(end_time, est_end_time, tolerance));
grpc_core::Fork::GlobalShutdown();
}
static void exec_ctx_thread(void* arg) {
@ -128,7 +123,6 @@ TEST(ForkTest, ExecCount) {
ASSERT_FALSE(exec_ctx_created);
grpc_core::Fork::AllowExecCtx();
thd.Join(); // This ensure that the call got un-blocked
grpc_core::Fork::GlobalShutdown();
}
int main(int argc, char** argv) {

Loading…
Cancel
Save