diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 2864af46a0..8d54d4c906 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -100,6 +100,7 @@ class ResponseHandler : public http_client::EventHandler // Set the response_received_ flag to true and notify any threads waiting on this result response_received_ = true; + stop_waiting_ = true; } cv_.notify_all(); } @@ -111,7 +112,7 @@ class ResponseHandler : public http_client::EventHandler bool waitForResponse() { std::unique_lock lk(mutex_); - cv_.wait(lk); + cv_.wait(lk, [this]{ return stop_waiting_; }); return response_received_; } @@ -129,6 +130,26 @@ class ResponseHandler : public http_client::EventHandler void OnEvent(http_client::SessionState state, opentelemetry::nostd::string_view reason) noexcept override { + // need to modify stop_waiting_ under lock before calling notify_all + switch (state) + { + case http_client::SessionState::CreateFailed: + case http_client::SessionState::ConnectFailed: + case http_client::SessionState::SendFailed: + case http_client::SessionState::SSLHandshakeFailed: + case http_client::SessionState::TimedOut: + case http_client::SessionState::NetworkError: + case http_client::SessionState::Cancelled: + { + std::unique_lock lk(mutex_); + stop_waiting_ = true; + } + break; + + default: + break; + } + // If any failure event occurs, release the condition variable to unblock main thread switch (state) { @@ -233,6 +254,9 @@ class ResponseHandler : public http_client::EventHandler std::condition_variable cv_; std::mutex mutex_; + // Whether notify has been called + bool stop_waiting_ = false; + // Whether the response from Elasticsearch has been received bool response_received_ = false;