Merge pull request #13025 from kpayson64/fork_bug_fixes

Bug fixes for fork support
pull/12860/merge
kpayson64 7 years ago committed by GitHub
commit 4690caa19e
  1. 5
      include/grpc/impl/codegen/fork.h
  2. 45
      src/core/lib/iomgr/fork_posix.c
  3. 5
      src/core/lib/iomgr/fork_windows.c
  4. 4
      src/core/lib/iomgr/port.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();

@ -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() {

@ -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() {}

@ -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

Loading…
Cancel
Save