From 7ce0ccdb403b7d96b89b9fd786a853cc56501aba Mon Sep 17 00:00:00 2001 From: Yousuk Seung Date: Mon, 10 Jun 2024 16:50:07 -0700 Subject: [PATCH] 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. Closes #36873 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36873 from yousukseung:python-fork-shutdown-wait 7908a6912a6eeb91cd423e770f07ffe4b34accd9 PiperOrigin-RevId: 642066286 --- .../grpc/_cython/_cygrpc/fork_posix.pyx.pxi | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/fork_posix.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/fork_posix.pyx.pxi index d901cfddf43..05889384945 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/fork_posix.pyx.pxi +++ b/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)