child post-fork gRPC shutdown to wait briefly (#36873)

gRPC shutdown may happen asynchronously with `work_serializer_dispatch`, so wait briefly until shutdown completes in the child's post-fork.

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes #36873

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36873 from yousukseung:python-fork-shutdown-wait 7908a6912a
PiperOrigin-RevId: 642066286
pull/36875/head
Yousuk Seung 8 months ago committed by Copybara-Service
parent c605b98467
commit 7ce0ccdb40
  1. 16
      src/python/grpcio/grpc/_cython/_cygrpc/fork_posix.pyx.pxi

@ -79,11 +79,15 @@ cdef void __postfork_child() noexcept nogil:
_LOGGER.error('Exiting child due to raised exception')
_LOGGER.error(sys.exc_info()[0])
os._exit(os.EX_USAGE)
if grpc_is_initialized() > 0:
with gil:
_LOGGER.error('Failed to shutdown gRPC Core after fork()')
os._exit(os.EX_USAGE)
# Give ~2s to shutdown asynchronously.
wait_ms = 10
while wait_ms < 1500:
if grpc_is_initialized() == 0:
return
time.sleep(wait_ms / 1000)
wait_ms = wait_ms * 2
_LOGGER.error('Failed to shutdown gRPC Core after fork()')
os._exit(os.EX_USAGE)
def fork_handlers_and_grpc_init():
@ -153,7 +157,7 @@ def get_fork_epoch():
def is_fork_support_enabled():
return _GRPC_ENABLE_FORK_SUPPORT
def fork_register_channel(channel):
if _GRPC_ENABLE_FORK_SUPPORT:
_fork_state.channels.add(channel)

Loading…
Cancel
Save