Skip to content

Commit

Permalink
fix: otlp http exporter block thread (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
wxl374 committed Dec 23, 2021
1 parent 1cf2422 commit 096464f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion 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,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<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,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;

Expand Down

0 comments on commit 096464f

Please sign in to comment.