Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions ext/src/http/client/curl/http_client_curl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});

Expand Down
94 changes: 94 additions & 0 deletions ext/test/http/curl_http_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ class CustomEventHandler : public http_client::EventHandler
std::atomic<bool> 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<int> terminal_count_{0};
std::atomic<int> cancelled_from_callback_{0};
};

class GetEventHandler : public CustomEventHandler
{
public:
Expand Down Expand Up @@ -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<http_client::curl::HttpCurlClientFactory>()->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<TerminalCountingHandler>();
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<http_client::curl::HttpCurlClientFactory>()->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<TerminalCountingHandler>();

{
std::unique_lock<std::mutex> 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();
Expand Down
Loading