From d117d3a5d95a79a9cc350b5eb7c5f61c4b6a873c Mon Sep 17 00:00:00 2001 From: owent Date: Thu, 10 Nov 2022 12:00:16 +0800 Subject: [PATCH] Fix session lock of `OtlpHttpClient` Signed-off-by: owent --- exporters/otlp/src/otlp_http_client.cc | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 8ea20919df..76fc826d76 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -981,16 +981,19 @@ void OtlpHttpClient::addSession(HttpSessionData &&session_data) noexcept return; } - opentelemetry::ext::http::client::Session *key = session_data.session.get(); - ResponseHandler *handle = static_cast(session_data.event_handle.get()); - - handle->Bind(this, *key); + std::shared_ptr session = session_data.session; + std::shared_ptr handle = + session_data.event_handle; + { + std::lock_guard guard{session_manager_lock_}; + static_cast(handle.get())->Bind(this, *session); - HttpSessionData &store_session_data = running_sessions_[key]; - store_session_data = std::move(session_data); + HttpSessionData &store_session_data = running_sessions_[session.get()]; + store_session_data = std::move(session_data); + } // Send request after the session is added - key->SendRequest(store_session_data.event_handle); + session->SendRequest(handle); } bool OtlpHttpClient::cleanupGCSessions() noexcept