From d4f760ae11b5b730f7ae16d4fbd4af2fc045572c Mon Sep 17 00:00:00 2001 From: Anirudh Ramachandra Date: Mon, 22 Apr 2024 16:32:53 -0700 Subject: [PATCH] Fix a possible memory leak in handshaker. This leak only impacts handshakers that don't have any endpoint associated with them and if the shutdown occurs at the same time as the handshaker finishing successfully. Currently the memory allocated for the read buffer is only cleaned up if the endpoint is not a nullptr triggering the leak. This change unilaterally cleans up the memory for this condition. --- src/core/lib/transport/handshaker.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/lib/transport/handshaker.cc b/src/core/lib/transport/handshaker.cc index 8288002bf4d..bdf8110bcfa 100644 --- a/src/core/lib/transport/handshaker.cc +++ b/src/core/lib/transport/handshaker.cc @@ -101,6 +101,11 @@ bool HandshakeManager::CallNextHandshakerLocked(grpc_error_handle error) { HandshakerArgsString(&args_).c_str()); } GPR_ASSERT(index_ <= handshakers_.size()); + if (args_.read_buffer != nullptr) { + grpc_slice_buffer_destroy(args_.read_buffer); + gpr_free(args_.read_buffer); + args_.read_buffer = nullptr; + } // If we got an error or we've been shut down or we're exiting early or // we've finished the last handshaker, invoke the on_handshake_done // callback. Otherwise, call the next handshaker. @@ -120,9 +125,6 @@ bool HandshakeManager::CallNextHandshakerLocked(grpc_error_handle error) { grpc_endpoint_destroy(args_.endpoint); args_.endpoint = nullptr; args_.args = ChannelArgs(); - grpc_slice_buffer_destroy(args_.read_buffer); - gpr_free(args_.read_buffer); - args_.read_buffer = nullptr; } } if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) {