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..dcacc66151 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. A request that was answered is reported as answered. 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..5f11963d83 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -70,6 +70,44 @@ class CustomEventHandler : public http_client::EventHandler std::atomic got_response_; }; +// 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 + { + 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); + // 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) + { + auto *session = cancel_at_response_; + cancel_at_response_ = nullptr; + session->CancelSession(); + } + } + + http_client::Session *cancel_at_response_ = nullptr; + std::atomic terminal_count_{0}; + std::atomic cancelled_from_callback_{0}; +}; + class GetEventHandler : public CustomEventHandler { public: @@ -461,6 +499,62 @@ 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->cancel_at_response_ = 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(); +} + +// 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)); + // 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(); +} + TEST_F(BasicCurlHttpTests, SendGetRequestSync) { received_requests_.clear();