From d8bf64bb39f9bf7680ec78b7b46e7eb35e0c88d9 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Wed, 19 Aug 2026 16:38:29 +0000 Subject: [PATCH 1/4] [BUGFIX] Complete an OTLP HTTP request on every state its client can end on The switch that decides whether a request is over named seven states. The client can end a transfer on three more: Destroyed, ReadError and WriteError. The switch below it recognises all three and logs them, so they arrive, are described, and are then dropped. A request ended that way never reaches Unbind, so its result callback never runs and its session is never released. ForceFlush and Shutdown have nothing to wait on but their own deadlines, and the caller is left waiting for a callback that is not coming. The bundled curl client does not currently produce those three states, so this is reached through the installed HttpClient interface by a client the user supplies. That interface is a supported extension point and the states are part of it, which is why the exporter has to answer them rather than assume which ones a client uses. Ordering is unchanged. stopping_ is a compare-exchange and the first writer wins, so one of these arriving after a response has already been reported still reports nothing. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- CHANGELOG.md | 5 + exporters/otlp/src/otlp_http_client.cc | 9 ++ .../otlp_http_exporter_custom_client_test.cc | 110 ++++++++++++++++++ 3 files changed, 124 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44bfbcf494..7040435fbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,11 @@ Increment the: * [CONFIGURATION] Cleanup build targets and docs [#4486](https://github.com/open-telemetry/opentelemetry-cpp/pull/4486) +* [BUGFIX] Complete an OTLP HTTP request that its client ends with + `Destroyed`, `ReadError` or `WriteError`, rather than logging the state + and leaving the caller waiting + [#4425](https://github.com/open-telemetry/opentelemetry-cpp/issues/4425) + * [CONFIGURATION] Build the configured resource detectors in SdkBuilder, apply the `detection.attributes` include/exclude filter to the detected attributes, and merge the resource per the resource SDK specification. diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 1f2bf98071..089abcd5e9 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -186,6 +186,12 @@ class ResponseHandler : public http_client::EventHandler opentelemetry::nostd::string_view reason) noexcept override { // need to modify stopping_ under lock before calling callback + // + // Every state a request can end on, because a request that ends on one nobody answers leaves + // the caller waiting for a callback that is not coming. Destroyed, ReadError and WriteError + // are the three the client can finish on that used to be logged and then dropped. Ordering is + // decided by the exchange below rather than here, so one of these arriving after a response + // has already been reported does not report a second time. bool need_stop = false; switch (state) { @@ -195,6 +201,9 @@ class ResponseHandler : public http_client::EventHandler case http_client::SessionState::SSLHandshakeFailed: case http_client::SessionState::TimedOut: case http_client::SessionState::NetworkError: + case http_client::SessionState::Destroyed: + case http_client::SessionState::ReadError: + case http_client::SessionState::WriteError: case http_client::SessionState::Cancelled: { need_stop = true; } 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 8eee6fcd00..68fd432aaf 100644 --- a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include #include #include #include @@ -104,6 +105,32 @@ class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test return {new OtlpHttpClient(MakeOtlpHttpClientOptions(), http_client), http_client}; } + // The same, with the outcome recorded. A request that never settles calls this no times, and one + // that settles twice calls it twice, so both failures the terminal states can produce are + // visible in the count rather than only in a timeout. + static void ExportOneRequest(OtlpHttpClient &otlp_client, + std::shared_ptr> calls, + std::shared_ptr result) + { + 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, + [calls, result](opentelemetry::sdk::common::ExportResult outcome, + google::protobuf::Message *) { + calls->fetch_add(1, std::memory_order_release); + *result = outcome; + return true; + }, + 1); + } + // A non-zero request budget keeps the export asynchronous, so it returns while the session runs. static void ExportOneRequest(OtlpHttpClient &otlp_client) { @@ -248,6 +275,89 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReportsSuccessOnceTheSess EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); } +// Every state the client can finish a request on has to end that request. These three used to be +// logged and then dropped: the switch that decides whether the request is over did not name them, +// so a client that ended a transfer this way left the caller waiting for a callback that was not +// coming, and ForceFlush and Shutdown could only give up on their own deadlines. +class OtlpHttpTerminalStateTest : public OtlpHttpExporterCustomClientTestPeer, + public ::testing::WithParamInterface +{}; + +TEST_P(OtlpHttpTerminalStateTest, ATerminalClientEventEndsTheRequest) +{ + 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); + + auto calls = std::make_shared>(0); + auto result = std::make_shared(sdk::common::ExportResult::kSuccess); + ExportOneRequest(otlp_client, calls, result); + ASSERT_NE(pending, nullptr); + + pending->OnEvent(GetParam(), ""); + + EXPECT_EQ(1, calls->load(std::memory_order_acquire)) + << "the request was not ended by the state the client finished on"; + EXPECT_EQ(sdk::common::ExportResult::kFailure, *result); + + // The deadline is short on purpose. Without the request being ended above this waits it out. + EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); +} + +INSTANTIATE_TEST_SUITE_P(TerminalStates, + OtlpHttpTerminalStateTest, + ::testing::Values(http_client::SessionState::ReadError, + http_client::SessionState::WriteError, + http_client::SessionState::Destroyed)); + +// The other direction. A client is free to report one of those states after it has already +// delivered a response, and the outcome the caller was given must not be replaced or repeated. +TEST_F(OtlpHttpExporterCustomClientTestPeer, ALateTerminalEventDoesNotReportASecondTime) +{ + for (const auto state : + {http_client::SessionState::ReadError, http_client::SessionState::WriteError, + http_client::SessionState::Destroyed}) + { + SCOPED_TRACE(static_cast(state)); + + 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); + + auto calls = std::make_shared>(0); + auto result = std::make_shared(sdk::common::ExportResult::kFailure); + ExportOneRequest(otlp_client, calls, result); + ASSERT_NE(pending, nullptr); + + http_client::nosend::Response sent; + sent.Finish(*pending); + ASSERT_EQ(1, calls->load(std::memory_order_acquire)); + + pending->OnEvent(state, ""); + + EXPECT_EQ(1, calls->load(std::memory_order_acquire)) + << "a state arriving after the response reported the request a second time"; + } +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE From 2c095996004c245ba62490cbdf6722b1bbd37f55 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Mon, 24 Aug 2026 02:30:24 +0000 Subject: [PATCH 2/4] [TEST] Keep the new OTLP HTTP cases out of the clang-tidy budget Three warnings, all from scaffolding this branch added rather than from what it is testing. The clang-tidy job counts warnings against a limit and main is at that limit, so three new ones fail it. The parameterised fixture is gone. It existed to run one body over three states, and a loop with SCOPED_TRACE does that without a class that only this translation unit can use, which is also how the case next to it is written. The two shared_ptr parameters of the recording helper are const references. The lambda inside still takes its own copy, so nothing about the lifetime changes; what goes away is a copy per call at the boundary. Checked the way CI checks it, with CMAKE_CXX_CLANG_TIDY over the all-options-abiv1-preview cache file, which reports no warnings for the test file and leaves the one in otlp_http_client.cc that main already has. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- .../otlp_http_exporter_custom_client_test.cc | 63 +++++++++---------- 1 file changed, 30 insertions(+), 33 deletions(-) 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 68fd432aaf..038eb33818 100644 --- a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -109,8 +109,8 @@ class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test // that settles twice calls it twice, so both failures the terminal states can produce are // visible in the count rather than only in a timeout. static void ExportOneRequest(OtlpHttpClient &otlp_client, - std::shared_ptr> calls, - std::shared_ptr result) + const std::shared_ptr> &calls, + const std::shared_ptr &result) { auto arena = std::make_unique(); auto *request = @@ -279,45 +279,42 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReportsSuccessOnceTheSess // logged and then dropped: the switch that decides whether the request is over did not name them, // so a client that ended a transfer this way left the caller waiting for a callback that was not // coming, and ForceFlush and Shutdown could only give up on their own deadlines. -class OtlpHttpTerminalStateTest : public OtlpHttpExporterCustomClientTestPeer, - public ::testing::WithParamInterface -{}; - -TEST_P(OtlpHttpTerminalStateTest, ATerminalClientEventEndsTheRequest) +TEST_F(OtlpHttpExporterCustomClientTestPeer, ATerminalClientEventEndsTheRequest) { - 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_); + for (const auto state : + {http_client::SessionState::ReadError, http_client::SessionState::WriteError, + http_client::SessionState::Destroyed}) + { + SCOPED_TRACE(static_cast(state)); - std::shared_ptr pending; - EXPECT_CALL(*session, SendRequest) - .WillRepeatedly( - [&pending](std::shared_ptr callback) { - pending = std::move(callback); - }); + 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_); - OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); + std::shared_ptr pending; + EXPECT_CALL(*session, SendRequest) + .WillRepeatedly( + [&pending](std::shared_ptr callback) { + pending = std::move(callback); + }); - auto calls = std::make_shared>(0); - auto result = std::make_shared(sdk::common::ExportResult::kSuccess); - ExportOneRequest(otlp_client, calls, result); - ASSERT_NE(pending, nullptr); + OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); - pending->OnEvent(GetParam(), ""); + auto calls = std::make_shared>(0); + auto result = std::make_shared(sdk::common::ExportResult::kSuccess); + ExportOneRequest(otlp_client, calls, result); + ASSERT_NE(pending, nullptr); - EXPECT_EQ(1, calls->load(std::memory_order_acquire)) - << "the request was not ended by the state the client finished on"; - EXPECT_EQ(sdk::common::ExportResult::kFailure, *result); + pending->OnEvent(state, ""); - // The deadline is short on purpose. Without the request being ended above this waits it out. - EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); -} + EXPECT_EQ(1, calls->load(std::memory_order_acquire)) + << "the request was not ended by the state the client finished on"; + EXPECT_EQ(sdk::common::ExportResult::kFailure, *result); -INSTANTIATE_TEST_SUITE_P(TerminalStates, - OtlpHttpTerminalStateTest, - ::testing::Values(http_client::SessionState::ReadError, - http_client::SessionState::WriteError, - http_client::SessionState::Destroyed)); + // The deadline is short on purpose. Without the request being ended above this waits it out. + EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); + } +} // The other direction. A client is free to report one of those states after it has already // delivered a response, and the outcome the caller was given must not be replaced or repeated. From 8248fff8bc48d972d25c00c7b8eb84264a4007a4 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Mon, 24 Aug 2026 15:29:44 +0000 Subject: [PATCH 3/4] [CHORE] Shorten the comments to what the code does not already say The switch comment and the two test comments described what the code used to do before the fix. That reads as history beside code that no longer has it, and it belongs in this PR and in the commit message. What is left states the invariant: every state the client can end a request on is named here, and the exchange below decides ordering. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- exporters/otlp/src/otlp_http_client.cc | 8 +++----- .../test/otlp_http_exporter_custom_client_test.cc | 11 ++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 089abcd5e9..ee902e930a 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -187,11 +187,9 @@ class ResponseHandler : public http_client::EventHandler { // need to modify stopping_ under lock before calling callback // - // Every state a request can end on, because a request that ends on one nobody answers leaves - // the caller waiting for a callback that is not coming. Destroyed, ReadError and WriteError - // are the three the client can finish on that used to be logged and then dropped. Ordering is - // decided by the exchange below rather than here, so one of these arriving after a response - // has already been reported does not report a second time. + // Every state the client can end a request on. A state missing from here leaves the caller + // waiting for a callback that never comes. The exchange below decides ordering, so a state + // arriving after a response does not report a second time. bool need_stop = false; switch (state) { 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 038eb33818..dd6e912e87 100644 --- a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -105,9 +105,8 @@ class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test return {new OtlpHttpClient(MakeOtlpHttpClientOptions(), http_client), http_client}; } - // The same, with the outcome recorded. A request that never settles calls this no times, and one - // that settles twice calls it twice, so both failures the terminal states can produce are - // visible in the count rather than only in a timeout. + // Records the outcome. A request that never settles calls back no times and one that settles + // twice calls back twice, so both failures show in the count rather than only in a timeout. static void ExportOneRequest(OtlpHttpClient &otlp_client, const std::shared_ptr> &calls, const std::shared_ptr &result) @@ -275,10 +274,8 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReportsSuccessOnceTheSess EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); } -// Every state the client can finish a request on has to end that request. These three used to be -// logged and then dropped: the switch that decides whether the request is over did not name them, -// so a client that ended a transfer this way left the caller waiting for a callback that was not -// coming, and ForceFlush and Shutdown could only give up on their own deadlines. +// A client that finishes a transfer on ReadError, WriteError or Destroyed ends the request: the +// result callback runs once, and ForceFlush returns rather than waiting out its deadline. TEST_F(OtlpHttpExporterCustomClientTestPeer, ATerminalClientEventEndsTheRequest) { for (const auto state : From 16aa77c3b8b93fa45571ccad468fd3c7b47626f5 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Tue, 8 Sep 2026 06:53:25 +0000 Subject: [PATCH 4/4] [TEST] Include for the range-for over a braced list Both cases added here iterate a braced list of session states: for (const auto state : {SessionState::ReadError, SessionState::WriteError, ...}) which is a std::initializer_list, so include-what-you-use asks for the header and the job fails at its limit of zero. Reported identically by the all-options-abiv1 and all-options-abiv1-preview runs on 8248fff8, each naming this one file and asking for this one line, with nothing to remove. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com> --- exporters/otlp/test/otlp_http_exporter_custom_client_test.cc | 1 + 1 file changed, 1 insertion(+) 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 dd6e912e87..b9c0157508 100644 --- a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include "gmock/gmock.h"