From 34d863b9c1a46fe48243f5943c2feca29670bd7c Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 1 Jun 2022 09:11:20 -0700 Subject: [PATCH] [init] Keep gRPC initialized until TCP destruction completes (#29852) We've seen a case internally whereby TCP destruction can be delayed behind grpc_shutdown, leading to crashes. --- src/core/lib/iomgr/tcp_posix.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/lib/iomgr/tcp_posix.cc b/src/core/lib/iomgr/tcp_posix.cc index 0526fc5370f..2afb148934e 100644 --- a/src/core/lib/iomgr/tcp_posix.cc +++ b/src/core/lib/iomgr/tcp_posix.cc @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -357,10 +358,19 @@ using grpc_core::TcpZerocopySendCtx; using grpc_core::TcpZerocopySendRecord; namespace { +// Keep grpc initialized whilst there are TCP connections. +// This should not get ported to EventEngine! +class KeepGrpcInitializedForTCP { + public: + KeepGrpcInitializedForTCP() { grpc_init(); } + ~KeepGrpcInitializedForTCP() { grpc_shutdown(); } +}; + struct grpc_tcp { grpc_tcp(int max_sends, size_t send_bytes_threshold) : tcp_zerocopy_send_ctx(max_sends, send_bytes_threshold) {} grpc_endpoint base; + GPR_NO_UNIQUE_ADDRESS KeepGrpcInitializedForTCP grpc_init_keep_alive; grpc_fd* em_fd; int fd; /* Used by the endpoint read function to distinguish the very first read call