Skip to content

Commit

Permalink
fix: otlp http exporter block thread (#1141) (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
wxl374 authored Dec 23, 2021
1 parent bb9d5de commit dcea27b
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -111,7 +112,7 @@ class ResponseHandler : public http_client::EventHandler
bool waitForResponse()
{
std::unique_lock<std::mutex> lk(mutex_);
cv_.wait(lk);
cv_.wait(lk, [this] { return stop_waiting_; });
return response_received_;
}

Expand All @@ -129,6 +130,25 @@ 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<std::mutex> lk(mutex_);
stop_waiting_ = true;
}
break;

default:
break;
}

// If any failure event occurs, release the condition variable to unblock main thread
switch (state)
{
Expand Down Expand Up @@ -233,7 +253,10 @@ class ResponseHandler : public http_client::EventHandler
std::condition_variable cv_;
std::mutex mutex_;

// Whether the response from Elasticsearch has been received
// Whether notify has been called
bool stop_waiting_ = false;

// Whether the response has been received
bool response_received_ = false;

// A string to store the response body
Expand Down

0 comments on commit dcea27b

Please sign in to comment.