From 1d4bf6cbc67191155e34add55cb428ccd495c299 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Wed, 5 Aug 2026 02:35:32 +0800 Subject: [PATCH 1/6] [BUG] Stop OTLP ForceFlush returning on the first notification and overrunning its deadline OtlpGrpcClient::ForceFlush left its wait loop on any notification without re-reading the completion counter, so one finished request reported the whole flush as complete. The loop condition is the only place that counter is read, and the break skipped it. Drop the break, let the loop condition decide, and return that condition instead of the leftover duration. Both clients waited for the configured export timeout on every iteration, so ForceFlush could block well past the deadline it was given. Bound each wait by the caller's remaining time as well. OtlpHttpClient::Shutdown calls ForceFlush(1ms) in a loop, so shutdown cost a multiple of the export timeout. The wait stays bounded and the condition is re-read on every wakeup, which is what covers a notification lost between the check and the wait. ForceFlushReturnsWithinTheCallerDeadline holds a request open with the nosend HTTP client and asserts the call returns inside its own deadline rather than the client timeout. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- CHANGELOG.md | 3 + exporters/otlp/src/otlp_grpc_client.cc | 18 +++--- exporters/otlp/src/otlp_http_client.cc | 19 +++++- .../otlp_http_exporter_custom_client_test.cc | 63 ++++++++++++++++++- 4 files changed, 90 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d32921f859..f8ce2e1c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ Increment the: [#4334](https://github.com/open-telemetry/opentelemetry-cpp/pull/4334) * [BUG] Stop reading past a `nostd::string_view` that is not NUL terminated [#4346](https://github.com/open-telemetry/opentelemetry-cpp/pull/4346) +* [BUG] Stop OTLP `ForceFlush` returning on the first notification and + overrunning the caller's deadline + [#4357](https://github.com/open-telemetry/opentelemetry-cpp/pull/4357) * [OTLP/HTTP] Honor `Retry-After` response header when retrying exports, supporting both delay-seconds and HTTP-date formats per RFC 7231 ยง7.1.3. diff --git a/exporters/otlp/src/otlp_grpc_client.cc b/exporters/otlp/src/otlp_grpc_client.cc index a64355d5d6..21ee9da550 100644 --- a/exporters/otlp/src/otlp_grpc_client.cc +++ b/exporters/otlp/src/otlp_grpc_client.cc @@ -757,20 +757,20 @@ bool OtlpGrpcClient::ForceFlush( while (timeout_steady > std::chrono::steady_clock::duration::zero() && request_counter > async_data_->finished_request_counter.load(std::memory_order_acquire)) { - // When changes of running_sessions_ and notify_one/notify_all happen between predicate - // checking and waiting, we should not wait forever.We should cleanup gc sessions here as soon - // as possible to call FinishSession() and cleanup resources. + // A completion can land between the loop condition and the wait, so the wait is bounded and the + // condition re-read on every wakeup. Never wait past the caller's deadline. + const std::chrono::steady_clock::duration wait_interval = + (std::min)(std::chrono::duration_cast( + async_data_->export_timeout), + timeout_steady); + std::chrono::steady_clock::time_point start_timepoint = std::chrono::steady_clock::now(); - if (std::cv_status::timeout != - async_data_->session_waker.wait_for(lock, async_data_->export_timeout)) - { - break; - } + async_data_->session_waker.wait_for(lock, wait_interval); timeout_steady -= std::chrono::steady_clock::now() - start_timepoint; } - return timeout_steady > std::chrono::steady_clock::duration::zero(); + return request_counter <= async_data_->finished_request_counter.load(std::memory_order_acquire); #else return true; #endif diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 5153e7141e..0e7d0b597f 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -1,6 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include #include #include #include @@ -857,8 +858,12 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept // When changes of running_sessions_ and notify_one/notify_all happen between predicate // checking and waiting, we should not wait forever.We should cleanup gc sessions here as soon // as possible to call FinishSession() and cleanup resources. + const std::chrono::steady_clock::duration wait_interval = (std::min)( + std::chrono::duration_cast(options_.timeout), + timeout_steady); + std::chrono::steady_clock::time_point start_timepoint = std::chrono::steady_clock::now(); - if (std::cv_status::timeout == session_waker_.wait_for(lock, options_.timeout)) + if (std::cv_status::timeout == session_waker_.wait_for(lock, wait_interval)) { cleanupGCSessions(); } @@ -870,7 +875,17 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept timeout_steady -= std::chrono::steady_clock::now() - start_timepoint; } - return timeout_steady > std::chrono::steady_clock::duration::zero(); + // What the wait was for, rather than what is left of the deadline. A session that finishes + // between the last check and the last wait takes its notification with it, and the flush it was + // holding up has happened. + { + std::lock_guard guard{session_manager_lock_}; + if (running_sessions_.empty()) + { + return true; + } + } + return finished_session_counter_.load(std::memory_order_acquire) >= wait_counter; } bool OtlpHttpClient::Shutdown(std::chrono::microseconds timeout) noexcept diff --git a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc index e49ad776a7..6367acebbb 100644 --- a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -4,13 +4,25 @@ #ifndef OPENTELEMETRY_STL_VERSION # include +# include +# include # include "opentelemetry/exporters/otlp/otlp_http_client.h" # include "opentelemetry/exporters/otlp/otlp_http_exporter.h" # include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" # include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" # include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" + +# include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" + +# include +# include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" + +# include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" + +# include "opentelemetry/ext/http/client/http_client.h" # include "opentelemetry/ext/http/client/http_client_factory.h" +# include "opentelemetry/sdk/common/exporter_utils.h" # include "opentelemetry/sdk/trace/batch_span_processor.h" # include "opentelemetry/sdk/trace/batch_span_processor_options.h" # include "opentelemetry/sdk/trace/tracer_provider.h" @@ -33,12 +45,13 @@ namespace http_client = opentelemetry::ext::http::client; using NosendHttpClientFactory = opentelemetry::test_common::ext::http::client::nosend::HttpClientFactoryNosend; -static OtlpHttpClientOptions MakeOtlpHttpClientOptions() +static OtlpHttpClientOptions MakeOtlpHttpClientOptions( + std::chrono::system_clock::duration timeout = std::chrono::system_clock::duration::zero()) { std::shared_ptr not_instrumented; OtlpHttpExporterOptions options; options.console_debug = true; - options.timeout = std::chrono::system_clock::duration::zero(); + options.timeout = timeout; options.retry_policy_max_attempts = 0U; options.retry_policy_initial_backoff = std::chrono::duration::zero(); options.retry_policy_max_backoff = std::chrono::duration::zero(); @@ -145,6 +158,52 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ExportCallsSendRequest) provider->ForceFlush(); } +TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReturnsWithinTheCallerDeadline) +{ + auto client = http_client::HttpClientTestFactory::Create(); + auto no_send_client = std::static_pointer_cast(client); + auto session = std::static_pointer_cast(no_send_client->session_); + + // Hold the request open, so ForceFlush has something to wait for. + std::shared_ptr pending; + EXPECT_CALL(*session, SendRequest) + .WillRepeatedly( + [&pending](std::shared_ptr callback) { + pending = std::move(callback); + }); + + // A client timeout far longer than the deadline ForceFlush is given below. + OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); + + auto arena = std::make_unique(); + auto *request = + google::protobuf::Arena::Create( + arena.get()); + auto *response = + google::protobuf::Arena::Create( + arena.get()); + + // A non-zero request budget keeps the export asynchronous, so it returns while the session runs. + otlp_client.Export( + *request, std::move(arena), response, + [](opentelemetry::sdk::common::ExportResult, google::protobuf::Message *) { return true; }, + 1); + ASSERT_NE(pending, nullptr); + + const auto started = std::chrono::steady_clock::now(); + const bool flushed = otlp_client.ForceFlush(std::chrono::milliseconds{50}); + const auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - started) + .count(); + + EXPECT_FALSE(flushed); + EXPECT_LT(elapsed, 1000) << "waited on the client timeout rather than the one it was given"; + + // Complete the export, or the client destructor waits on it. + http_client::nosend::Response sent; + sent.Finish(*pending); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE From d0dc3c994ed85dcd16acdc2c4c6ce740f8d94682 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Wed, 5 Aug 2026 11:20:00 +0800 Subject: [PATCH 2/6] Cover the gRPC ForceFlush loop with a stub that defers completion The mock stub ran the completion callback inline, so a request was always finished before ForceFlush() could be called and the loop under test never had anything to wait for. Holding the callback leaves the request in flight, which is all the two cases need; the counters stay private to the .cc. ForceFlushReturnsWithinTheCallerDeadline asks for 50 ms against the 10 s export timeout. ForceFlushKeepsWaitingWhenAnotherRequestCompletes leaves two requests in flight and completes one while the flush is parked, which is the notification that used to end the wait. Registration is by source text, so both cases exist in every build and opt out in the fixture rather than being compiled away. --- .../otlp/test/otlp_grpc_exporter_test.cc | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index 239f788c49..c285a41f85 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -40,7 +40,13 @@ # include # include # include +# include +# include +# include # include +# include +# include +# include # include # include @@ -86,6 +92,13 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse *response, std::function callback) override { + if (stub_->defer_completions_) + { + std::lock_guard guard(stub_->deferred_lock_); + stub_->deferred_.push_back(std::move(callback)); + return; + } + stub_->last_async_status_ = stub_->Export(context, *request, response); callback(stub_->last_async_status_); } @@ -118,8 +131,46 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe ::grpc::Status GetLastAsyncStatus() const noexcept { return last_async_status_; } + // Keep the completion callbacks instead of running them inline, so a test can call ForceFlush() + // with requests still in flight. Call before the first export. + void DeferCompletions() noexcept { defer_completions_ = true; } + + std::size_t DeferredCount() + { + std::lock_guard guard(deferred_lock_); + return deferred_.size(); + } + + // Completes whichever deferred request has been waiting longest, false when there is none. + bool CompleteOneDeferred() + { + std::function callback; + { + std::lock_guard guard(deferred_lock_); + if (deferred_.empty()) + { + return false; + } + callback = std::move(deferred_.front()); + deferred_.erase(deferred_.begin()); + } + + callback(::grpc::Status::OK); + return true; + } + + void CompleteAllDeferred() + { + while (CompleteOneDeferred()) + { + } + } + private: ::grpc::Status last_async_status_; + bool defer_completions_{false}; + std::mutex deferred_lock_; + std::vector> deferred_; async_interface async_interface_; }; @@ -244,6 +295,90 @@ TEST_F(OtlpGrpcExporterTestPeer, ExportPartialSuccess) EXPECT_TRUE(contains("too many spans!!")); } +// ForceFlush() only has requests to wait for when the client tracks them, which it does under +// ENABLE_ASYNC_EXPORT. gtest_add_tests registers by source text, so the cases below have to exist +// in every build and opt out here rather than be compiled away. +class OtlpGrpcExporterFlushTestPeer : public OtlpGrpcExporterTestPeer +{ +protected: + void SetUp() override + { +# if !defined(ENABLE_ASYNC_EXPORT) + GTEST_SKIP() << "ForceFlush has no in-flight requests to wait for without async export"; +# endif + } +}; + +// The wait is bounded by the exporter's timeout, so a caller asking for less than that used to +// wait for the larger of the two. +TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushReturnsWithinTheCallerDeadline) +{ + auto *mock_stub = new OtlpMockTraceServiceStub(); + mock_stub->DeferCompletions(); + std::unique_ptr stub_interface( + mock_stub); + auto exporter = GetExporter(stub_interface); + + auto recordable = exporter->MakeRecordable(); + recordable->SetName("Test span"); + nostd::span> batch(&recordable, 1); + exporter->Export(batch); + ASSERT_EQ(1u, mock_stub->DeferredCount()); + + const auto started = std::chrono::steady_clock::now(); + const bool flushed = exporter->ForceFlush(std::chrono::milliseconds{50}); + const auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - started); + + EXPECT_FALSE(flushed); + EXPECT_LT(elapsed.count(), 5000); + + mock_stub->CompleteAllDeferred(); +} + +// A completion for one request used to end the wait for every other request, and the leftover +// deadline was then reported as success. +TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushKeepsWaitingWhenAnotherRequestCompletes) +{ + auto *mock_stub = new OtlpMockTraceServiceStub(); + mock_stub->DeferCompletions(); + std::unique_ptr stub_interface( + mock_stub); + auto exporter = GetExporter(stub_interface); + + auto first = exporter->MakeRecordable(); + first->SetName("Test span 1"); + nostd::span> first_batch(&first, 1); + exporter->Export(first_batch); + + auto second = exporter->MakeRecordable(); + second->SetName("Test span 2"); + nostd::span> second_batch(&second, 1); + exporter->Export(second_batch); + + ASSERT_EQ(2u, mock_stub->DeferredCount()); + + // Completed once the flush below is parked, so it arrives as a notification rather than as a + // counter the loop condition reads on its way in. + std::thread completer([mock_stub] { + std::this_thread::sleep_for(std::chrono::milliseconds{100}); + mock_stub->CompleteOneDeferred(); + }); + + const auto started = std::chrono::steady_clock::now(); + const bool flushed = exporter->ForceFlush(std::chrono::milliseconds{1000}); + const auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - started); + completer.join(); + + EXPECT_FALSE(flushed); + // Ran its own deadline down rather than returning on the notification. + EXPECT_GE(elapsed.count(), 500); + EXPECT_EQ(1u, mock_stub->DeferredCount()); + + mock_stub->CompleteAllDeferred(); +} + // Create spans, let processor call Export() TEST_F(OtlpGrpcExporterTestPeer, ExportIntegrationTest) { From 09ea66f7b13af4a69f743905b6061a5af988b720 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Wed, 5 Aug 2026 20:58:28 +0800 Subject: [PATCH 3/6] Return the finished counter alone from the HTTP ForceFlush The empty running_sessions_ short circuit could not change the answer. ReleaseSession is the only place a session leaves the map, and it bumps finished_session_counter_ under the same lock, so the counter equals the number of sessions removed. addSession is the only place one enters, and its increment follows the insert, so the entry snapshot is never larger than the number inserted. An empty map therefore already implies the counter has reached the snapshot. ForceFlushReportsSuccessOnceTheSessionIsDone asserts that invariant. It passes on main as well, where the old return happened to agree, so it guards the new predicate rather than reproducing a bug. --- exporters/otlp/src/otlp_http_client.cc | 7 --- .../otlp_http_exporter_custom_client_test.cc | 57 ++++++++++++++----- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 0e7d0b597f..94557cbe1c 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -878,13 +878,6 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept // What the wait was for, rather than what is left of the deadline. A session that finishes // between the last check and the last wait takes its notification with it, and the flush it was // holding up has happened. - { - std::lock_guard guard{session_manager_lock_}; - if (running_sessions_.empty()) - { - return true; - } - } return finished_session_counter_.load(std::memory_order_acquire) >= wait_counter; } diff --git a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc index 6367acebbb..d9767c5cf9 100644 --- a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -90,6 +90,23 @@ class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test auto http_client = http_client::HttpClientTestFactory::Create(); return {new OtlpHttpClient(MakeOtlpHttpClientOptions(), http_client), http_client}; } + + // A non-zero request budget keeps the export asynchronous, so it returns while the session runs. + static void ExportOneRequest(OtlpHttpClient &otlp_client) + { + auto arena = std::make_unique(); + auto *request = + google::protobuf::Arena::Create( + arena.get()); + auto *response = + google::protobuf::Arena::Create( + arena.get()); + + otlp_client.Export( + *request, std::move(arena), response, + [](opentelemetry::sdk::common::ExportResult, google::protobuf::Message *) { return true; }, + 1); + } }; TEST_F(OtlpHttpExporterCustomClientTestPeer, FactoryInjectionCreatesExporter) @@ -175,19 +192,7 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReturnsWithinTheCallerDea // A client timeout far longer than the deadline ForceFlush is given below. OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); - auto arena = std::make_unique(); - auto *request = - google::protobuf::Arena::Create( - arena.get()); - auto *response = - google::protobuf::Arena::Create( - arena.get()); - - // A non-zero request budget keeps the export asynchronous, so it returns while the session runs. - otlp_client.Export( - *request, std::move(arena), response, - [](opentelemetry::sdk::common::ExportResult, google::protobuf::Message *) { return true; }, - 1); + ExportOneRequest(otlp_client); ASSERT_NE(pending, nullptr); const auto started = std::chrono::steady_clock::now(); @@ -204,6 +209,32 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReturnsWithinTheCallerDea sent.Finish(*pending); } +// The result is the finished counter alone, so a flush with nothing left to wait for has to say +// so. Guards the counter against the session bookkeeping it is derived from. +TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReportsSuccessOnceTheSessionIsDone) +{ + auto client = http_client::HttpClientTestFactory::Create(); + auto no_send_client = std::static_pointer_cast(client); + auto session = std::static_pointer_cast(no_send_client->session_); + + std::shared_ptr pending; + EXPECT_CALL(*session, SendRequest) + .WillRepeatedly( + [&pending](std::shared_ptr callback) { + pending = std::move(callback); + }); + + OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); + + ExportOneRequest(otlp_client); + ASSERT_NE(pending, nullptr); + + http_client::nosend::Response sent; + sent.Finish(*pending); + + EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE From 69ea88be80efd404e8a5cb7be211145dd5eb7e6e Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Wed, 5 Aug 2026 21:29:28 +0800 Subject: [PATCH 4/6] Fail the deferred gRPC cases instead of hanging them An assertion that ended a case before it completed the deferred requests reached ~OtlpGrpcClient, which loops until running_requests reaches zero and has no deadline. TryCancel() cannot move that counter for a request the stub is holding, so the binary hung rather than reporting the failure. Measured: exit 124 under a 30 s bound before, exit 1 after. A scope guard declared after the exporter drains on the way out, so the completion happens however the case leaves. --- .../otlp/test/otlp_grpc_exporter_test.cc | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index c285a41f85..e756b9855e 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -174,6 +174,23 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe async_interface async_interface_; }; +// Completes whatever is still deferred on the way out. ~OtlpGrpcClient waits for +// running_requests to reach zero with no deadline, and TryCancel() cannot move it for a request +// the stub is holding, so an assertion that ends a case early would hang the binary instead of +// failing it. Declare after the exporter, so this runs first. +class DrainDeferredCompletions +{ +public: + explicit DrainDeferredCompletions(OtlpMockTraceServiceStub *stub) noexcept : stub_(stub) {} + ~DrainDeferredCompletions() { stub_->CompleteAllDeferred(); } + + DrainDeferredCompletions(const DrainDeferredCompletions &) = delete; + DrainDeferredCompletions &operator=(const DrainDeferredCompletions &) = delete; + +private: + OtlpMockTraceServiceStub *stub_; +}; + using opentelemetry::test_common::ScopedTestLogHandler; } // namespace @@ -318,6 +335,7 @@ TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushReturnsWithinTheCallerDeadline) std::unique_ptr stub_interface( mock_stub); auto exporter = GetExporter(stub_interface); + DrainDeferredCompletions drain{mock_stub}; auto recordable = exporter->MakeRecordable(); recordable->SetName("Test span"); @@ -332,8 +350,6 @@ TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushReturnsWithinTheCallerDeadline) EXPECT_FALSE(flushed); EXPECT_LT(elapsed.count(), 5000); - - mock_stub->CompleteAllDeferred(); } // A completion for one request used to end the wait for every other request, and the leftover @@ -345,6 +361,7 @@ TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushKeepsWaitingWhenAnotherRequestCo std::unique_ptr stub_interface( mock_stub); auto exporter = GetExporter(stub_interface); + DrainDeferredCompletions drain{mock_stub}; auto first = exporter->MakeRecordable(); first->SetName("Test span 1"); @@ -375,8 +392,6 @@ TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushKeepsWaitingWhenAnotherRequestCo // Ran its own deadline down rather than returning on the notification. EXPECT_GE(elapsed.count(), 500); EXPECT_EQ(1u, mock_stub->DeferredCount()); - - mock_stub->CompleteAllDeferred(); } // Create spans, let processor call Export() From e45f053475ec4b603eda0e841d3a1d42111b41b6 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Wed, 5 Aug 2026 21:38:20 +0800 Subject: [PATCH 5/6] Keep the new gRPC flush cases off the clang-tidy ratchet Two warnings the checks the repository runs would have flagged. The drain guard declared a destructor and deleted copies without the matching move operations, which cppcoreguidelines-special-member-functions wants; and the fixture sat at namespace scope where nothing outside the file names it, which misc-use-internal-linkage wants in an anonymous namespace. Measured against main with the CI header filters: one warning before, one after, both bugprone-derived-method-shadowing-base-method from the mock stub. Zero delta. The anonymous namespace is safe here: the fixture name appears nowhere else in the tree, and Bazel builds this file as its own cc_test rather than linking it into a combined binary. --- exporters/otlp/test/otlp_grpc_exporter_test.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index e756b9855e..f1750d31e5 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -186,6 +186,8 @@ class DrainDeferredCompletions DrainDeferredCompletions(const DrainDeferredCompletions &) = delete; DrainDeferredCompletions &operator=(const DrainDeferredCompletions &) = delete; + DrainDeferredCompletions(DrainDeferredCompletions &&) = delete; + DrainDeferredCompletions &operator=(DrainDeferredCompletions &&) = delete; private: OtlpMockTraceServiceStub *stub_; @@ -312,6 +314,8 @@ TEST_F(OtlpGrpcExporterTestPeer, ExportPartialSuccess) EXPECT_TRUE(contains("too many spans!!")); } +namespace +{ // ForceFlush() only has requests to wait for when the client tracks them, which it does under // ENABLE_ASYNC_EXPORT. gtest_add_tests registers by source text, so the cases below have to exist // in every build and opt out here rather than be compiled away. @@ -393,6 +397,7 @@ TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushKeepsWaitingWhenAnotherRequestCo EXPECT_GE(elapsed.count(), 500); EXPECT_EQ(1u, mock_stub->DeferredCount()); } +} // namespace // Create spans, let processor call Export() TEST_F(OtlpGrpcExporterTestPeer, ExportIntegrationTest) From 25e616ef57b071bb24a545b96be3b2326bbdf61b Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Thu, 6 Aug 2026 04:15:24 +0800 Subject: [PATCH 6/6] Give the gRPC deadline case the same ceiling as the HTTP one Five seconds caught the ten second overrun this fixes but would have let a multi-second regression through. The HTTP case next to it uses one second against the same 50 ms deadline, and the gRPC case measures 66 ms. --- exporters/otlp/test/otlp_grpc_exporter_test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index f1750d31e5..754e58dd26 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -353,7 +353,8 @@ TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushReturnsWithinTheCallerDeadline) std::chrono::steady_clock::now() - started); EXPECT_FALSE(flushed); - EXPECT_LT(elapsed.count(), 5000); + EXPECT_LT(elapsed.count(), 1000) + << "waited on the export timeout rather than the one it was given"; } // A completion for one request used to end the wait for every other request, and the leftover