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 1 commit
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
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
Copy link
Member

@owent owent Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also fix the error message in this comment?

bool response_received_ = false;

Expand Down