Skip to content

Commit 16972ed

Browse files
authored
[BUG] Send one request per curl session (#4431)
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 9d6f398 commit 16972ed

4 files changed

Lines changed: 125 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Increment the:
4848
to compile standalone on newer standard library implementations.
4949
[#4574](https://github.com/open-telemetry/opentelemetry-cpp/pull/4574)
5050

51+
* [BUG] Send one request per curl session, rather than replacing the operation
52+
a running request still belongs to
53+
[#4396](https://github.com/open-telemetry/opentelemetry-cpp/issues/4396)
54+
5155
## [1.29.0] 2026-09-13
5256

5357
* [RELEASE] Bump main branch to 1.29.0-dev (#4259)

‎ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,25 @@ class Session : public opentelemetry::ext::http::client::Session,
186186

187187
std::shared_ptr<opentelemetry::ext::http::client::Request> CreateRequest() noexcept override
188188
{
189+
if (send_started_.load(std::memory_order_acquire))
190+
{
191+
// The request that has been sent stays where it is. libcurl does not copy the body or the
192+
// header list, so the transfer reads them for as long as it runs.
193+
return http_request_;
194+
}
195+
189196
http_request_.reset(new Request());
190197
return http_request_;
191198
}
192199

200+
/**
201+
* Send the request this session carries.
202+
*
203+
* A session carries one request. A second call reports CreateFailed to its own handler and
204+
* sends nothing: the easy handle of the first request holds this session and the operation
205+
* that owns it, and replacing that operation takes away what libcurl and the client are still
206+
* reading.
207+
*/
193208
void SendRequest(
194209
std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) noexcept override;
195210

@@ -235,6 +250,9 @@ class Session : public opentelemetry::ext::http::client::Session,
235250
uint64_t session_id_ = 0UL;
236251
HttpClient &http_client_;
237252
std::atomic<bool> is_session_active_{false};
253+
254+
// Raised by the first SendRequest and never lowered. A session carries one request.
255+
std::atomic<bool> send_started_{false};
238256
};
239257

240258
class HttpClientSync : public opentelemetry::ext::http::client::HttpClientSync

‎ext/src/http/client/curl/http_client_curl.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ static int deflateInPlace(z_stream *strm, unsigned char *buf, uint32_t len, uint
141141
void Session::SendRequest(
142142
std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) noexcept
143143
{
144+
if (send_started_.exchange(true, std::memory_order_acq_rel))
145+
{
146+
// The first request is not finished with this session. Its easy handle names the session in
147+
// CURLOPT_PRIVATE and names its operation in every callback it was given, and the message
148+
// loop resolves that name to whichever operation the session owns, so a second operation
149+
// would be handed the first one's completion. Worse from a handler that sends again from
150+
// OnResponse, where the operation being replaced is the one running that handler.
151+
if (callback)
152+
{
153+
callback->OnEvent(opentelemetry::ext::http::client::SessionState::CreateFailed,
154+
"a session carries one request");
155+
}
156+
return;
157+
}
158+
144159
is_session_active_.store(true, std::memory_order_release);
145160
const auto &url = host_ + http_request_->uri_;
146161
auto callback_ptr = callback.get();

‎ext/test/http/curl_http_test.cc‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,37 @@ class CustomEventHandler : public http_client::EventHandler
120120
// inside the Response event, which is the one moment both arms of the completion callback are
121121
// eligible: DispatchEvent notifies the handler before it stores the new state, and the callback
122122
// runs after both, so it sees an aborted operation that also has a response.
123+
class ReentrantSendHandler : public CustomEventHandler
124+
{
125+
public:
126+
void OnResponse(http_client::Response & /* response */) noexcept override
127+
{
128+
got_response_.store(true, std::memory_order_release);
129+
130+
if (session_ != nullptr && !resent_.exchange(true, std::memory_order_acq_rel))
131+
{
132+
auto again = session_->CreateRequest();
133+
again->SetUri("get/");
134+
session_->SendRequest(self_.lock());
135+
}
136+
}
137+
138+
void OnEvent(http_client::SessionState state, nostd::string_view /* reason */) noexcept override
139+
{
140+
if (state == http_client::SessionState::CreateFailed)
141+
{
142+
create_failed_.fetch_add(1, std::memory_order_release);
143+
}
144+
}
145+
146+
http_client::Session *session_ = nullptr;
147+
std::weak_ptr<ReentrantSendHandler> self_;
148+
std::atomic<int> create_failed_{0};
149+
150+
private:
151+
std::atomic<bool> resent_{false};
152+
};
153+
123154
class TerminalCountingHandler : public CustomEventHandler
124155
{
125156
public:
@@ -742,6 +773,63 @@ TEST_F(BasicCurlHttpTests, ResetMultiHandleWithASessionDoesNotDeadlock)
742773
client->FinishAllSessions();
743774
}
744775

776+
// The reproduction from #4396. A handler that sends again from OnResponse replaces the operation
777+
// whose Cleanup is running that handler, and Cleanup reads that operation again on the way out.
778+
TEST_F(BasicCurlHttpTests, ASecondRequestFromInsideTheResponseIsRefused)
779+
{
780+
received_requests_.clear();
781+
auto session_manager = std::make_shared<http_client::curl::HttpCurlClientFactory>()->Create();
782+
ASSERT_TRUE(session_manager != nullptr);
783+
784+
auto session = session_manager->CreateSession("http://127.0.0.1:19000");
785+
auto request = session->CreateRequest();
786+
request->SetUri("get/");
787+
788+
auto handler = std::make_shared<ReentrantSendHandler>();
789+
handler->session_ = session.get();
790+
handler->self_ = handler;
791+
792+
session->SendRequest(handler);
793+
ASSERT_TRUE(waitForRequests(30, 1));
794+
session->FinishSession();
795+
796+
EXPECT_TRUE(handler->got_response_.load(std::memory_order_acquire));
797+
EXPECT_EQ(1, handler->create_failed_.load(std::memory_order_acquire));
798+
799+
session_manager->FinishAllSessions();
800+
}
801+
802+
// The same thing without the re-entrancy, and the request the first send is reading from is not
803+
// replaced under it either.
804+
TEST_F(BasicCurlHttpTests, ASessionSendsOneRequest)
805+
{
806+
received_requests_.clear();
807+
auto session_manager = std::make_shared<http_client::curl::HttpCurlClientFactory>()->Create();
808+
ASSERT_TRUE(session_manager != nullptr);
809+
810+
auto session = session_manager->CreateSession("http://127.0.0.1:19000");
811+
auto request = session->CreateRequest();
812+
request->SetUri("get/");
813+
814+
auto first = std::make_shared<ReentrantSendHandler>();
815+
session->SendRequest(first);
816+
ASSERT_TRUE(waitForRequests(30, 1));
817+
session->FinishSession();
818+
819+
EXPECT_TRUE(first->got_response_.load(std::memory_order_acquire));
820+
EXPECT_EQ(0, first->create_failed_.load(std::memory_order_acquire));
821+
822+
auto again = session->CreateRequest();
823+
EXPECT_EQ(request.get(), again.get());
824+
825+
auto second = std::make_shared<ReentrantSendHandler>();
826+
session->SendRequest(second);
827+
EXPECT_EQ(1, second->create_failed_.load(std::memory_order_acquire));
828+
EXPECT_FALSE(second->got_response_.load(std::memory_order_acquire));
829+
830+
session_manager->FinishAllSessions();
831+
}
832+
745833
// The caller-thread side of the same cancel. The server handler takes mtx_requests before it
746834
// answers, so holding it keeps a response from racing the cancel and the abort lands while the
747835
// IO thread is still driving the easy handle. That pairing is what #4369 caught.

0 commit comments

Comments
 (0)