From 0552ac5a997676ced56608194a2e138381c77438 Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Thu, 6 Aug 2026 00:04:28 +0800 Subject: [PATCH 1/4] Report one outcome when a curl session is cancelled after the response The async completion callback tested WasAborted() and a Response session state with two independent ifs, and the two are not exclusive. Cancelling only raises a flag, so a transfer that has already been answered still runs both arms, and a handler treating either as terminal counts one request twice. Only the transfer failing moves the state away from Sending, so a Response state means the server answered. That is the outcome worth reporting, and it is also the safe one: telling a caller its request was cancelled after the server accepted it invites a retry that writes the same batch again. Reachable through Shutdown, which calls CancelAllSessions while requests are in flight. --- CHANGELOG.md | 4 ++ ext/src/http/client/curl/http_client_curl.cc | 13 ++--- ext/test/http/curl_http_test.cc | 55 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f584c3b765..688ebc3c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ Increment the: ## [Unreleased] +* [BUG] Report one outcome per request when a curl session is cancelled after + the response arrives + ([#4363](https://github.com/open-telemetry/opentelemetry-cpp/pull/4363)) + * [CODE HEALTH] Enable clang-tidy `modernize-deprecated-headers` and replace deprecated C headers (`stdint.h`, `stddef.h`, `stdlib.h`, `string.h`, `stdio.h`, `ctype.h`, `limits.h`, `assert.h`) with their C++ equivalents diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 3a25e82181..2da0d502b2 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -207,12 +207,8 @@ void Session::SendRequest( http_request_->is_log_enabled_, http_request_->retry_policy_)); bool success = CURLE_OK == curl_operation_->SendAsync(this, [this, callback](HttpOperation &operation) { - if (operation.WasAborted()) - { - // Manually cancelled - callback->OnEvent(opentelemetry::ext::http::client::SessionState::Cancelled, ""); - } - + // Both can hold at once. Cancelling only raises a flag, and the transfer may already + // have been answered, so the response is reported and one request finishes once. if (operation.GetSessionState() == opentelemetry::ext::http::client::SessionState::Response) { // we have a http response @@ -222,6 +218,11 @@ void Session::SendRequest( response->status_code_ = operation.GetResponseCode(); callback->OnResponse(*response); } + else if (operation.WasAborted()) + { + // Manually cancelled + callback->OnEvent(opentelemetry::ext::http::client::SessionState::Cancelled, ""); + } is_session_active_.store(false, std::memory_order_release); }); diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 02a1eff7ea..753cc5f9f9 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -70,6 +70,36 @@ class CustomEventHandler : public http_client::EventHandler std::atomic got_response_; }; +// Cancels from inside the Response event, which is the one moment both arms of the completion +// callback are eligible: DispatchEvent notifies the handler before it stores the new state, and +// the callback runs after both, so it sees an aborted operation that also has a response. +class CancelAtResponseHandler : public CustomEventHandler +{ +public: + void OnResponse(http_client::Response & /* response */) noexcept override + { + terminal_count_.fetch_add(1, std::memory_order_release); + got_response_.store(true, std::memory_order_release); + } + + void OnEvent(http_client::SessionState state, nostd::string_view /* reason */) noexcept override + { + if (state == http_client::SessionState::Cancelled) + { + terminal_count_.fetch_add(1, std::memory_order_release); + } + else if (state == http_client::SessionState::Response && session_ != nullptr) + { + auto *session = session_; + session_ = nullptr; + session->CancelSession(); + } + } + + http_client::Session *session_ = nullptr; + std::atomic terminal_count_{0}; +}; + class GetEventHandler : public CustomEventHandler { public: @@ -461,6 +491,31 @@ TEST_F(BasicCurlHttpTests, ExponentialBackoffRetry) } #endif // ENABLE_OTLP_RETRY_PREVIEW +// A cancel that arrives once the server has answered used to deliver Cancelled and the response, +// so a handler treating either as terminal saw one request finish twice. +TEST_F(BasicCurlHttpTests, ACancelAfterTheResponseReportsOneOutcome) +{ + received_requests_.clear(); + auto session_manager = std::make_shared()->Create(); + EXPECT_TRUE(session_manager != nullptr); + + auto session = session_manager->CreateSession("http://127.0.0.1:19000"); + auto request = session->CreateRequest(); + request->SetUri("get/"); + + auto handler = std::make_shared(); + handler->session_ = session.get(); + + session->SendRequest(handler); + ASSERT_TRUE(waitForRequests(30, 1)); + session->FinishSession(); + + EXPECT_TRUE(handler->got_response_.load(std::memory_order_acquire)); + EXPECT_EQ(1, handler->terminal_count_.load(std::memory_order_acquire)); + + session_manager->FinishAllSessions(); +} + TEST_F(BasicCurlHttpTests, SendGetRequestSync) { received_requests_.clear(); From 7b4e5834a98bed8424f4da14661a56d03c8fddfa Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Thu, 6 Aug 2026 00:12:15 +0800 Subject: [PATCH 2/4] Say only what the branch decides The comment claimed a request finishes once, which is wider than this change. A cancel arriving while the transfer fails still yields the dispatched failure state and then Cancelled; only the response case is made exclusive here. --- ext/src/http/client/curl/http_client_curl.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 2da0d502b2..dcacc66151 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -207,8 +207,8 @@ void Session::SendRequest( http_request_->is_log_enabled_, http_request_->retry_policy_)); bool success = CURLE_OK == curl_operation_->SendAsync(this, [this, callback](HttpOperation &operation) { - // Both can hold at once. Cancelling only raises a flag, and the transfer may already - // have been answered, so the response is reported and one request finishes once. + // Both can hold at once: cancelling only raises a flag, and the transfer may already + // have been answered. A request that was answered is reported as answered. if (operation.GetSessionState() == opentelemetry::ext::http::client::SessionState::Response) { // we have a http response From 49b7d437a022a85da981133bcf34891ccc76bdea Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Thu, 6 Aug 2026 01:05:52 +0800 Subject: [PATCH 3/4] Cover the cancel arm of the completion callback Codecov put the patch at 50 per cent: nothing in the suite reached a cancel that gets as far as the callback, so the line reporting it never ran. The server handler takes mtx_requests before answering, so holding it keeps a response from racing the cancel while the abort is torn down through Session::FinishOperation. gcov on the changed lines: the else if runs 7 times and its body once, where the body was previously never reached. The case asserts at least one Cancelled rather than exactly one. It measures two, one dispatched by Cleanup while the state is still in flight and one from this callback, and that doubling is not what this change decides. --- ext/test/http/curl_http_test.cc | 51 ++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 753cc5f9f9..9704b9b03a 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -70,10 +70,11 @@ class CustomEventHandler : public http_client::EventHandler std::atomic got_response_; }; -// Cancels from inside the Response event, which is the one moment both arms of the completion -// callback are eligible: DispatchEvent notifies the handler before it stores the new state, and -// the callback runs after both, so it sees an aborted operation that also has a response. -class CancelAtResponseHandler : public CustomEventHandler +// Counts the terminal notifications one request produces. Set cancel_at_response_ to cancel from +// inside the Response event, which is the one moment both arms of the completion callback are +// eligible: DispatchEvent notifies the handler before it stores the new state, and the callback +// runs after both, so it sees an aborted operation that also has a response. +class TerminalCountingHandler : public CustomEventHandler { public: void OnResponse(http_client::Response & /* response */) noexcept override @@ -88,15 +89,15 @@ class CancelAtResponseHandler : public CustomEventHandler { terminal_count_.fetch_add(1, std::memory_order_release); } - else if (state == http_client::SessionState::Response && session_ != nullptr) + else if (state == http_client::SessionState::Response && cancel_at_response_ != nullptr) { - auto *session = session_; - session_ = nullptr; + auto *session = cancel_at_response_; + cancel_at_response_ = nullptr; session->CancelSession(); } } - http_client::Session *session_ = nullptr; + http_client::Session *cancel_at_response_ = nullptr; std::atomic terminal_count_{0}; }; @@ -503,8 +504,8 @@ TEST_F(BasicCurlHttpTests, ACancelAfterTheResponseReportsOneOutcome) auto request = session->CreateRequest(); request->SetUri("get/"); - auto handler = std::make_shared(); - handler->session_ = session.get(); + auto handler = std::make_shared(); + handler->cancel_at_response_ = session.get(); session->SendRequest(handler); ASSERT_TRUE(waitForRequests(30, 1)); @@ -516,6 +517,36 @@ TEST_F(BasicCurlHttpTests, ACancelAfterTheResponseReportsOneOutcome) session_manager->FinishAllSessions(); } +// The other arm of the same callback, which nothing exercised. The server handler takes +// mtx_requests before it answers, so holding it keeps a response from racing the cancel, and the +// abort is torn down through Session::FinishOperation while the transfer is still in flight. +TEST_F(BasicCurlHttpTests, ACancelBeforeTheResponseReportsCancelled) +{ + received_requests_.clear(); + auto session_manager = std::make_shared()->Create(); + EXPECT_TRUE(session_manager != nullptr); + + auto session = session_manager->CreateSession("http://127.0.0.1:19000"); + auto request = session->CreateRequest(); + request->SetUri("get/"); + + auto handler = std::make_shared(); + + { + std::unique_lock lock_requests(mtx_requests); + session->SendRequest(handler); + session->CancelSession(); + session->FinishSession(); + } + + EXPECT_FALSE(handler->got_response_.load(std::memory_order_acquire)); + // At least one, not exactly one: Cleanup dispatches Cancelled itself while the state is still + // in flight, and this callback adds another. That doubling is not what this change decides. + EXPECT_GE(handler->terminal_count_.load(std::memory_order_acquire), 1); + + session_manager->FinishAllSessions(); +} + TEST_F(BasicCurlHttpTests, SendGetRequestSync) { received_requests_.clear(); From ee7d59f93cdf14f0adb2f9e5beedbcd83278ca7f Mon Sep 17 00:00:00 2001 From: thc1006 <84045975+thc1006@users.noreply.github.com> Date: Thu, 6 Aug 2026 01:27:59 +0800 Subject: [PATCH 4/4] Hold the cancel arm with an assertion that can tell it apart The previous assertion counted any Cancelled, so it stayed green whether or not the arm ran: Cleanup dispatches its own before the callback. It therefore covered the line without guarding it. GetCurlErrorMessage never yields an empty string, so Cleanup's Cancelled always carries a reason and the callback's never does. Counting the empty one names the arm exactly. Removing the arm turns the case red with 0 against 1, which the count alone did not do. --- ext/test/http/curl_http_test.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 9704b9b03a..5f11963d83 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -83,11 +83,17 @@ class TerminalCountingHandler : public CustomEventHandler got_response_.store(true, std::memory_order_release); } - void OnEvent(http_client::SessionState state, nostd::string_view /* reason */) noexcept override + void OnEvent(http_client::SessionState state, nostd::string_view reason) noexcept override { if (state == http_client::SessionState::Cancelled) { terminal_count_.fetch_add(1, std::memory_order_release); + // Cleanup dispatches its own Cancelled carrying a curl message, and GetCurlErrorMessage + // never yields an empty one, so an empty reason is the completion callback and only it. + if (reason.empty()) + { + cancelled_from_callback_.fetch_add(1, std::memory_order_release); + } } else if (state == http_client::SessionState::Response && cancel_at_response_ != nullptr) { @@ -99,6 +105,7 @@ class TerminalCountingHandler : public CustomEventHandler http_client::Session *cancel_at_response_ = nullptr; std::atomic terminal_count_{0}; + std::atomic cancelled_from_callback_{0}; }; class GetEventHandler : public CustomEventHandler @@ -540,9 +547,10 @@ TEST_F(BasicCurlHttpTests, ACancelBeforeTheResponseReportsCancelled) } EXPECT_FALSE(handler->got_response_.load(std::memory_order_acquire)); - // At least one, not exactly one: Cleanup dispatches Cancelled itself while the state is still - // in flight, and this callback adds another. That doubling is not what this change decides. - EXPECT_GE(handler->terminal_count_.load(std::memory_order_acquire), 1); + // Counted by its empty reason so the assertion holds the arm this covers rather than whatever + // else reports a cancel. Two arrive in all, the other from Cleanup, and the total is left + // unasserted because that doubling is not what this change decides. + EXPECT_EQ(1, handler->cancelled_from_callback_.load(std::memory_order_acquire)); session_manager->FinishAllSessions(); }