Skip to content

Commit 0b29cc3

Browse files
committed
ForceFlush before cancling the running requests when shutdown.
1 parent 5e21b8a commit 0b29cc3

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class OtlpHttpClient
275275
std::shared_ptr<ext::http::client::HttpClient> http_client);
276276

277277
// Stores if this HTTP client had its Shutdown() method called
278-
bool is_shutdown_;
278+
std::atomic<bool> is_shutdown_;
279279

280280
// The configuration options associated with this HTTP client.
281281
const OtlpHttpClientOptions options_;

exporters/otlp/src/otlp_grpc_client.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,21 @@ bool OtlpGrpcClient::Shutdown(std::chrono::microseconds timeout) noexcept
567567
return true;
568568
}
569569

570+
bool force_flush_result;
570571
if (false == is_shutdown_.exchange(true, std::memory_order_acq_rel))
571572
{
572573
is_shutdown_ = true;
573574

575+
force_flush_result = ForceFlush(timeout);
576+
574577
async_data_->cq.Shutdown();
575578
}
579+
else
580+
{
581+
force_flush_result = ForceFlush(timeout);
582+
}
576583

577-
return ForceFlush(timeout);
584+
return force_flush_result;
578585
}
579586

580587
#endif

exporters/otlp/src/otlp_http_client.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#include "opentelemetry/exporters/otlp/otlp_http_client.h"
5+
#include <chrono>
56

67
#if defined(HAVE_GSL)
78
# include <gsl/gsl>
@@ -819,19 +820,23 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept
819820

820821
bool OtlpHttpClient::Shutdown(std::chrono::microseconds timeout) noexcept
821822
{
823+
is_shutdown_.store(true, std::memory_order_release);
824+
825+
ForceFlush(timeout);
826+
822827
{
823828
std::lock_guard<std::recursive_mutex> guard{session_manager_lock_};
824-
is_shutdown_ = true;
825829

826830
// Shutdown the session manager
827831
http_client_->CancelAllSessions();
828832
http_client_->FinishAllSessions();
829833
}
830834

831-
ForceFlush(timeout);
832-
835+
// Wait util all sessions are canceled.
833836
while (cleanupGCSessions())
834-
;
837+
{
838+
ForceFlush(std::chrono::milliseconds{1});
839+
}
835840
return true;
836841
}
837842

@@ -1017,7 +1022,7 @@ bool OtlpHttpClient::cleanupGCSessions() noexcept
10171022

10181023
bool OtlpHttpClient::IsShutdown() const noexcept
10191024
{
1020-
return is_shutdown_;
1025+
return is_shutdown_.load(std::memory_order_acquire);
10211026
}
10221027

10231028
} // namespace otlp

0 commit comments

Comments
 (0)