Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: otlp http exporter block thread (#1141) #1163

Merged
merged 3 commits into from
Dec 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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