Skip to content

Commit 0440728

Browse files
committed
Fix infinitely waiting when shutdown with more than one running http sessions.
Signed-off-by: owentou <[email protected]>
1 parent 319d854 commit 0440728

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

exporters/otlp/src/otlp_http_client.cc

+12-4
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,12 @@ OtlpHttpClient::~OtlpHttpClient()
668668
}
669669
}
670670
// When changes of running_sessions_ and notify_one/notify_all happen between predicate
671-
// checking and waiting, we should not wait forever.
672-
session_waker_.wait_for(lock, options_.timeout);
671+
// checking and waiting, we should not wait forever. We should cleanup gc sessions here as soon
672+
// as possible to call FinishSession() and cleanup resources.
673+
if (std::cv_status::timeout == session_waker_.wait_for(lock, options_.timeout))
674+
{
675+
cleanupGCSessions();
676+
}
673677
}
674678

675679
// And then remove all session datas
@@ -781,8 +785,12 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept
781785
}
782786
}
783787
// When changes of running_sessions_ and notify_one/notify_all happen between predicate
784-
// checking and waiting, we should not wait forever.
785-
session_waker_.wait_for(lock, options_.timeout);
788+
// checking and waiting, we should not wait forever.We should cleanup gc sessions here as soon
789+
// as possible to call FinishSession() and cleanup resources.
790+
if (std::cv_status::timeout == session_waker_.wait_for(lock, options_.timeout))
791+
{
792+
cleanupGCSessions();
793+
}
786794
}
787795
return true;
788796
}

ext/src/http/client/curl/http_client_curl.cc

+9-4
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ void Session::SendRequest(
7777
// reuse it instead of creating a new one.
7878
http_client_.MaybeSpawnBackgroundThread();
7979
}
80-
else if (callback)
80+
else
8181
{
82-
callback->OnEvent(opentelemetry::ext::http::client::SessionState::CreateFailed, "");
82+
if (callback)
83+
{
84+
callback->OnEvent(opentelemetry::ext::http::client::SessionState::CreateFailed, "");
85+
}
8386
is_session_active_.store(false, std::memory_order_release);
8487
}
8588
}
@@ -176,8 +179,9 @@ bool HttpClient::CancelAllSessions() noexcept
176179
{
177180
std::unordered_map<uint64_t, std::shared_ptr<Session>> sessions;
178181
{
182+
// We can only cleanup session and curl handles in the IO thread.
179183
std::lock_guard<std::mutex> lock_guard{sessions_m_};
180-
sessions.swap(sessions_);
184+
sessions = sessions_;
181185
}
182186

183187
if (sessions.empty())
@@ -200,8 +204,9 @@ bool HttpClient::FinishAllSessions() noexcept
200204
{
201205
std::unordered_map<uint64_t, std::shared_ptr<Session>> sessions;
202206
{
207+
// We can only cleanup session and curl handles in the IO thread.
203208
std::lock_guard<std::mutex> lock_guard{sessions_m_};
204-
sessions.swap(sessions_);
209+
sessions = sessions_;
205210
}
206211

207212
if (sessions.empty())

ext/src/http/client/curl/http_operation_curl.cc

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include <cstring>
5+
46
#include "opentelemetry/ext/http/client/curl/http_operation_curl.h"
57

68
#include "opentelemetry/ext/http/client/curl/http_client_curl.h"

0 commit comments

Comments
 (0)