diff options
-rw-r--r-- | include/grpc/impl/codegen/fork.h | 5 | ||||
-rw-r--r-- | src/core/lib/iomgr/fork_posix.c | 45 | ||||
-rw-r--r-- | src/core/lib/iomgr/fork_windows.c | 5 | ||||
-rw-r--r-- | src/core/lib/iomgr/port.h | 4 |
4 files changed, 32 insertions, 27 deletions
diff --git a/include/grpc/impl/codegen/fork.h b/include/grpc/impl/codegen/fork.h index 03c0c4eede..baec7a2f10 100644 --- a/include/grpc/impl/codegen/fork.h +++ b/include/grpc/impl/codegen/fork.h @@ -24,10 +24,9 @@ * active gRPC function calls between calling grpc_prefork() and * grpc_postfork_parent()/grpc_postfork_child(). * - * Returns 1 on success, 0 otherwise. * * Typical use: - * assert(grpc_prefork() == 1); + * grpc_prefork(); * int pid = fork(); * if (pid) { * grpc_postfork_parent(); @@ -38,7 +37,7 @@ * } */ -int grpc_prefork(); +void grpc_prefork(); void grpc_postfork_parent(); diff --git a/src/core/lib/iomgr/fork_posix.c b/src/core/lib/iomgr/fork_posix.c index 5f2880db37..a55b3a349a 100644 --- a/src/core/lib/iomgr/fork_posix.c +++ b/src/core/lib/iomgr/fork_posix.c @@ -34,44 +34,49 @@ #include "src/core/lib/support/env.h" #include "src/core/lib/support/fork.h" #include "src/core/lib/support/thd_internal.h" +#include "src/core/lib/surface/init.h" /* * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK * AROUND VERY SPECIFIC USE CASES. */ -int grpc_prefork() { +void grpc_prefork() { 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 0; + return; } - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - grpc_timer_manager_set_threading(false); - grpc_executor_set_threading(&exec_ctx, false); - grpc_exec_ctx_finish(&exec_ctx); - if (!gpr_await_threads( - 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!"); - return 0; + if (grpc_is_initialized()) { + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + grpc_timer_manager_set_threading(false); + grpc_executor_set_threading(&exec_ctx, false); + grpc_exec_ctx_finish(&exec_ctx); + if (!gpr_await_threads( + 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!"); + } } - return 1; } void grpc_postfork_parent() { - grpc_timer_manager_set_threading(true); - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - grpc_executor_set_threading(&exec_ctx, true); - grpc_exec_ctx_finish(&exec_ctx); + if (grpc_is_initialized()) { + grpc_timer_manager_set_threading(true); + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + grpc_executor_set_threading(&exec_ctx, true); + grpc_exec_ctx_finish(&exec_ctx); + } } void grpc_postfork_child() { - grpc_timer_manager_set_threading(true); - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - grpc_executor_set_threading(&exec_ctx, true); - grpc_exec_ctx_finish(&exec_ctx); + if (grpc_is_initialized()) { + grpc_timer_manager_set_threading(true); + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + grpc_executor_set_threading(&exec_ctx, true); + grpc_exec_ctx_finish(&exec_ctx); + } } void grpc_fork_handlers_auto_register() { diff --git a/src/core/lib/iomgr/fork_windows.c b/src/core/lib/iomgr/fork_windows.c index 965ddfbd0b..f9986f33c7 100644 --- a/src/core/lib/iomgr/fork_windows.c +++ b/src/core/lib/iomgr/fork_windows.c @@ -28,10 +28,7 @@ * AROUND VERY SPECIFIC USE CASES. */ -int grpc_prefork() { - gpr_log(GPR_ERROR, "Forking not supported on Windows"); - return 0; -} +void grpc_prefork() { gpr_log(GPR_ERROR, "Forking not supported on Windows"); } void grpc_postfork_parent() {} diff --git a/src/core/lib/iomgr/port.h b/src/core/lib/iomgr/port.h index 42033d0ba4..1970d106d5 100644 --- a/src/core/lib/iomgr/port.h +++ b/src/core/lib/iomgr/port.h @@ -30,6 +30,7 @@ #define GRPC_HAVE_IP_PKTINFO 1 #define GRPC_HAVE_MSG_NOSIGNAL 1 #define GRPC_HAVE_UNIX_SOCKET 1 +#define GRPC_POSIX_FORK 1 #define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1 #define GRPC_POSIX_SOCKET 1 #define GRPC_POSIX_SOCKETADDR 1 @@ -59,6 +60,7 @@ #define GRPC_HAVE_MSG_NOSIGNAL 1 #define GRPC_HAVE_UNIX_SOCKET 1 #define GRPC_LINUX_MULTIPOLL_WITH_EPOLL 1 +#define GRPC_POSIX_FORK 1 #define GRPC_POSIX_HOST_NAME_MAX 1 #define GRPC_POSIX_SOCKET 1 #define GRPC_POSIX_SOCKETADDR 1 @@ -90,6 +92,7 @@ #define GRPC_HAVE_SO_NOSIGPIPE 1 #define GRPC_HAVE_UNIX_SOCKET 1 #define GRPC_MSG_IOVLEN_TYPE int +#define GRPC_POSIX_FORK 1 #define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1 #define GRPC_POSIX_SOCKET 1 #define GRPC_POSIX_SOCKETADDR 1 @@ -103,6 +106,7 @@ #define GRPC_HAVE_IPV6_RECVPKTINFO 1 #define GRPC_HAVE_SO_NOSIGPIPE 1 #define GRPC_HAVE_UNIX_SOCKET 1 +#define GRPC_POSIX_FORK 1 #define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1 #define GRPC_POSIX_SOCKET 1 #define GRPC_POSIX_SOCKETADDR 1 |