pr33495_fox_nested_fork.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. --- a/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.cc (index)
  2. +++ b/grpc/src/core/lib/event_engine/posix_engine/ev_epoll1_linux.cc (working tree)
  3. @@ -255,10 +255,6 @@ void ResetEventManagerOnFork() {
  4. poller->Close();
  5. }
  6. gpr_mu_unlock(&fork_fd_list_mu);
  7. - if (grpc_core::Fork::Enabled()) {
  8. - gpr_mu_destroy(&fork_fd_list_mu);
  9. - grpc_core::Fork::SetResetChildPollingEngineFunc(nullptr);
  10. - }
  11. InitEpoll1PollerLinux();
  12. }
  13. @@ -273,8 +269,10 @@ bool InitEpoll1PollerLinux() {
  14. return false;
  15. }
  16. if (grpc_core::Fork::Enabled()) {
  17. - gpr_mu_init(&fork_fd_list_mu);
  18. - grpc_core::Fork::SetResetChildPollingEngineFunc(ResetEventManagerOnFork);
  19. + if (grpc_core::Fork::RegisterResetChildPollingEngineFunc(
  20. + ResetEventManagerOnFork)) {
  21. + gpr_mu_init(&fork_fd_list_mu);
  22. + }
  23. }
  24. close(fd);
  25. return true;
  26. --- a/src/core/lib/event_engine/posix_engine/ev_poll_posix.cc (index)
  27. +++ b/grpc/src/core/lib/event_engine/posix_engine/ev_poll_posix.cc (working tree)
  28. @@ -323,10 +323,6 @@ void ResetEventManagerOnFork() {
  29. delete poller;
  30. }
  31. gpr_mu_unlock(&fork_fd_list_mu);
  32. - if (grpc_core::Fork::Enabled()) {
  33. - gpr_mu_destroy(&fork_fd_list_mu);
  34. - grpc_core::Fork::SetResetChildPollingEngineFunc(nullptr);
  35. - }
  36. InitPollPollerPosix();
  37. }
  38. @@ -337,8 +333,10 @@ bool InitPollPollerPosix() {
  39. return false;
  40. }
  41. if (grpc_core::Fork::Enabled()) {
  42. - gpr_mu_init(&fork_fd_list_mu);
  43. - grpc_core::Fork::SetResetChildPollingEngineFunc(ResetEventManagerOnFork);
  44. + if (grpc_core::Fork::RegisterResetChildPollingEngineFunc(
  45. + ResetEventManagerOnFork)) {
  46. + gpr_mu_init(&fork_fd_list_mu);
  47. + }
  48. }
  49. return true;
  50. }
  51. --- a/src/core/lib/gprpp/fork.cc (index)
  52. +++ b/src/core/lib/gprpp/fork.cc (working tree)
  53. @@ -20,6 +20,8 @@
  54. #include "src/core/lib/gprpp/fork.h"
  55. +#include <utility>
  56. +
  57. #include <grpc/support/atm.h>
  58. #include <grpc/support/sync.h>
  59. #include <grpc/support/time.h>
  60. @@ -195,19 +197,16 @@ void Fork::DoDecExecCtxCount() {
  61. NoDestructSingleton<ExecCtxState>::Get()->DecExecCtxCount();
  62. }
  63. -void Fork::SetResetChildPollingEngineFunc(
  64. +bool Fork::RegisterResetChildPollingEngineFunc(
  65. Fork::child_postfork_func reset_child_polling_engine) {
  66. if (reset_child_polling_engine_ == nullptr) {
  67. - reset_child_polling_engine_ = new std::vector<Fork::child_postfork_func>();
  68. - }
  69. - if (reset_child_polling_engine == nullptr) {
  70. - reset_child_polling_engine_->clear();
  71. - } else {
  72. - reset_child_polling_engine_->emplace_back(reset_child_polling_engine);
  73. + reset_child_polling_engine_ = new std::set<Fork::child_postfork_func>();
  74. }
  75. + auto ret = reset_child_polling_engine_->insert(reset_child_polling_engine);
  76. + return ret.second;
  77. }
  78. -const std::vector<Fork::child_postfork_func>&
  79. +const std::set<Fork::child_postfork_func>&
  80. Fork::GetResetChildPollingEngineFunc() {
  81. return *reset_child_polling_engine_;
  82. }
  83. @@ -244,6 +243,6 @@ void Fork::AwaitThreads() {
  84. std::atomic<bool> Fork::support_enabled_(false);
  85. bool Fork::override_enabled_ = false;
  86. -std::vector<Fork::child_postfork_func>* Fork::reset_child_polling_engine_ =
  87. +std::set<Fork::child_postfork_func>* Fork::reset_child_polling_engine_ =
  88. nullptr;
  89. } // namespace grpc_core
  90. --- a/src/core/lib/gprpp/fork.h (index)
  91. +++ b/src/core/lib/gprpp/fork.h (working tree)
  92. @@ -22,7 +22,7 @@
  93. #include <grpc/support/port_platform.h>
  94. #include <atomic>
  95. -#include <vector>
  96. +#include <set>
  97. //
  98. // NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
  99. @@ -57,10 +57,11 @@ class Fork {
  100. // Provide a function that will be invoked in the child's postfork handler to
  101. // reset the polling engine's internal state.
  102. - static void SetResetChildPollingEngineFunc(
  103. + // Returns true if reset_child_polling_engine was not previously registered,
  104. + // otherwise returns false and does nothing.
  105. + static bool RegisterResetChildPollingEngineFunc(
  106. child_postfork_func reset_child_polling_engine);
  107. - static const std::vector<child_postfork_func>&
  108. - GetResetChildPollingEngineFunc();
  109. + static const std::set<child_postfork_func>& GetResetChildPollingEngineFunc();
  110. // Check if there is a single active ExecCtx
  111. // (the one used to invoke this function). If there are more,
  112. @@ -89,7 +90,7 @@ class Fork {
  113. static std::atomic<bool> support_enabled_;
  114. static bool override_enabled_;
  115. - static std::vector<child_postfork_func>* reset_child_polling_engine_;
  116. + static std::set<child_postfork_func>* reset_child_polling_engine_;
  117. };
  118. } // namespace grpc_core
  119. --- a/src/core/lib/iomgr/ev_epoll1_linux.cc (index)
  120. +++ b/src/core/lib/iomgr/ev_epoll1_linux.cc (working tree)
  121. @@ -1237,10 +1237,6 @@ static void shutdown_engine(void) {
  122. fd_global_shutdown();
  123. pollset_global_shutdown();
  124. epoll_set_shutdown();
  125. - if (grpc_core::Fork::Enabled()) {
  126. - gpr_mu_destroy(&fork_fd_list_mu);
  127. - grpc_core::Fork::SetResetChildPollingEngineFunc(nullptr);
  128. - }
  129. g_is_shutdown = true;
  130. }
  131. @@ -1297,6 +1293,7 @@ const grpc_event_engine_vtable grpc_ev_epoll1_posix = {
  132. // the global epoll fd. This allows gRPC to shutdown in the child process
  133. // without interfering with connections or RPCs ongoing in the parent.
  134. static void reset_event_manager_on_fork() {
  135. + if (g_is_shutdown) return;
  136. gpr_mu_lock(&fork_fd_list_mu);
  137. while (fork_fd_list_head != nullptr) {
  138. close(fork_fd_list_head->fd);
  139. @@ -1331,9 +1328,10 @@ static bool init_epoll1_linux() {
  140. }
  141. if (grpc_core::Fork::Enabled()) {
  142. - gpr_mu_init(&fork_fd_list_mu);
  143. - grpc_core::Fork::SetResetChildPollingEngineFunc(
  144. - reset_event_manager_on_fork);
  145. + if (grpc_core::Fork::RegisterResetChildPollingEngineFunc(
  146. + reset_event_manager_on_fork)) {
  147. + gpr_mu_init(&fork_fd_list_mu);
  148. + }
  149. }
  150. g_is_shutdown = false;
  151. return true;
  152. --- a/src/core/lib/iomgr/ev_poll_posix.cc (index)
  153. +++ b/src/core/lib/iomgr/ev_poll_posix.cc (working tree)
  154. @@ -1405,10 +1405,11 @@ const grpc_event_engine_vtable grpc_ev_poll_posix = {
  155. return false;
  156. }
  157. if (grpc_core::Fork::Enabled()) {
  158. - track_fds_for_fork = true;
  159. - gpr_mu_init(&fork_fd_list_mu);
  160. - grpc_core::Fork::SetResetChildPollingEngineFunc(
  161. - reset_event_manager_on_fork);
  162. + if (grpc_core::Fork::RegisterResetChildPollingEngineFunc(
  163. + reset_event_manager_on_fork)) {
  164. + track_fds_for_fork = true;
  165. + gpr_mu_init(&fork_fd_list_mu);
  166. + }
  167. }
  168. return true;
  169. },