diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index dfce580d9d..900c5b6e81 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -359,7 +359,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient std::recursive_mutex session_ids_m_; std::unordered_map> sessions_; std::unordered_set pending_to_add_session_ids_; - std::unordered_set pending_to_abort_session_ids_; + std::unordered_map> pending_to_abort_sessions_; std::unordered_map pending_to_remove_session_handles_; std::list> pending_to_remove_sessions_; diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 52942f6433..94bbf9fa7b 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -257,8 +257,8 @@ void HttpClient::CleanupSession(uint64_t session_id) } else if (session->IsSessionActive() && session->GetOperation()) { - // If this session is alread waiting to be removed, just wakeup background thread to call - // doRemoveSessions() + // If this session is already running, give it to the background thread for cleanup. + pending_to_abort_sessions_[session_id] = std::move(session); wakeupBackgroundThread(); } } @@ -392,7 +392,7 @@ void HttpClient::ScheduleAddSession(uint64_t session_id) std::lock_guard lock_guard{session_ids_m_}; pending_to_add_session_ids_.insert(session_id); pending_to_remove_session_handles_.erase(session_id); - pending_to_abort_session_ids_.erase(session_id); + pending_to_abort_sessions_.erase(session_id); } wakeupBackgroundThread(); @@ -401,9 +401,21 @@ void HttpClient::ScheduleAddSession(uint64_t session_id) void HttpClient::ScheduleAbortSession(uint64_t session_id) { { - std::lock_guard lock_guard{session_ids_m_}; - pending_to_abort_session_ids_.insert(session_id); - pending_to_add_session_ids_.erase(session_id); + std::lock_guard lock_guard{sessions_m_}; + auto session = sessions_.find(session_id); + if (session == sessions_.end()) + { + std::lock_guard lock_guard{session_ids_m_}; + pending_to_add_session_ids_.erase(session_id); + } + else + { + std::lock_guard lock_guard{session_ids_m_}; + pending_to_abort_sessions_[session_id] = std::move(session->second); + pending_to_add_session_ids_.erase(session_id); + + sessions_.erase(session); + } } wakeupBackgroundThread(); @@ -472,33 +484,23 @@ bool HttpClient::doAddSessions() bool HttpClient::doAbortSessions() { - std::list> abort_sessions; - std::unordered_set pending_to_abort_session_ids; + std::unordered_map> pending_to_abort_sessions; { std::lock_guard session_id_lock_guard{session_ids_m_}; - pending_to_abort_session_ids_.swap(pending_to_abort_session_ids); + pending_to_abort_sessions_.swap(pending_to_abort_sessions); } + bool has_data = false; + for (auto session : pending_to_abort_sessions) { - std::lock_guard lock_guard{sessions_m_}; - for (auto &session_id : pending_to_abort_session_ids) + if (!session.second) { - auto session = sessions_.find(session_id); - if (session == sessions_.end()) - { - continue; - } - - abort_sessions.push_back(session->second); + continue; } - } - bool has_data = false; - for (auto session : abort_sessions) - { - if (session->GetOperation()) + if (session.second->GetOperation()) { - session->FinishOperation(); + session.second->FinishOperation(); has_data = true; } }