Skip to content

Commit d3b18cd

Browse files
committed
Refine curl handle lifetime.
1 parent ca6f43e commit d3b18cd

4 files changed

Lines changed: 13 additions & 31 deletions

File tree

libs/internal/src/network/curl_requester.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void CurlRequester::PerformRequestWithMulti(std::shared_ptr<CurlMultiManager> mu
225225

226226
// Add handle to multi manager for async processing
227227
// Headers will be freed automatically by CurlMultiManager
228-
multi_manager->add_handle(curl, headers, [ctx](CURL* easy, CURLcode result) {
228+
multi_manager->add_handle(curl, headers, [ctx](std::shared_ptr<CURL> easy, CURLcode result) {
229229
// This callback runs on the executor when the request completes
230230

231231
// Check for errors
@@ -238,7 +238,7 @@ void CurlRequester::PerformRequestWithMulti(std::shared_ptr<CurlMultiManager> mu
238238

239239
// Get HTTP response code
240240
long response_code = 0;
241-
curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &response_code);
241+
curl_easy_getinfo(easy.get(), CURLINFO_RESPONSE_CODE, &response_code);
242242

243243
// Invoke the user's callback with the result
244244
ctx->callback(HttpResult(

libs/networking/include/launchdarkly/network/curl_multi_manager.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CurlMultiManager : public std::enable_shared_from_this<CurlMultiManager> {
3737
* Callback invoked when an easy handle completes (success or error).
3838
* Parameters: CURL* easy handle, CURLcode result
3939
*/
40-
using CompletionCallback = std::function<void(CURL*, CURLcode)>;
40+
using CompletionCallback = std::function<void(std::shared_ptr<CURL>, CURLcode)>;
4141

4242
/**
4343
* Create a CurlMultiManager on the given executor.
@@ -64,12 +64,6 @@ class CurlMultiManager : public std::enable_shared_from_this<CurlMultiManager> {
6464
curl_slist* headers,
6565
CompletionCallback callback);
6666

67-
/**
68-
* Remove an easy handle from management.
69-
* @param easy The CURL easy handle to remove
70-
*/
71-
void remove_handle(CURL* easy);
72-
7367
private:
7468
explicit CurlMultiManager(boost::asio::any_io_executor executor);
7569

libs/networking/src/curl_multi_manager.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,6 @@ void CurlMultiManager::add_handle(const std::shared_ptr<CURL>& easy,
7070
}
7171
}
7272

73-
void CurlMultiManager::remove_handle(CURL* easy) {
74-
curl_multi_remove_handle(multi_handle_.get(), easy);
75-
76-
std::lock_guard lock(mutex_);
77-
78-
// Free headers if they exist
79-
if (const auto header_it = headers_.find(easy);
80-
header_it != headers_.end() && header_it->second) {
81-
curl_slist_free_all(header_it->second);
82-
}
83-
84-
callbacks_.erase(easy);
85-
headers_.erase(easy);
86-
handles_.erase(easy);
87-
}
88-
8973
int CurlMultiManager::socket_callback(CURL* easy,
9074
curl_socket_t s,
9175
int what,
@@ -193,8 +177,6 @@ void CurlMultiManager::check_multi_info() {
193177
headers = header_it->second;
194178
headers_.erase(header_it);
195179
}
196-
197-
handles_.erase(easy);
198180
}
199181

200182
// Remove from multi handle
@@ -205,11 +187,17 @@ void CurlMultiManager::check_multi_info() {
205187
curl_slist_free_all(headers);
206188
}
207189

190+
// We don't need to keep the curl handle alive any longer, but we
191+
// do want the handle to remain active for the duration of the
192+
// callback.
193+
auto handle = handles_[easy];
194+
handles_.erase(easy);
195+
208196
// Invoke completion callback
209197
if (callback) {
210198
boost::asio::post(executor_, [callback = std::move(callback),
211-
easy, result]() {
212-
callback(easy, result);
199+
easy, result, handle]() {
200+
callback(handle, result);
213201
});
214202
}
215203
}

libs/server-sent-events/src/curl_client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,15 @@ void CurlClient::PerformRequestWithMulti(
441441
// Add handle to multi manager for async processing
442442
// Headers will be freed automatically by CurlMultiManager
443443
std::weak_ptr<RequestContext> weak_context = context;
444-
multi_manager->add_handle(curl, headers, [weak_context](CURL* easy, CURLcode res) {
444+
multi_manager->add_handle(curl, headers, [weak_context](std::shared_ptr<CURL> easy, CURLcode res) {
445445
auto context = weak_context.lock();
446446
if (!context) {
447447
return;
448448
}
449449

450450
// Get response code
451451
long response_code = 0;
452-
curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &response_code);
452+
curl_easy_getinfo(easy.get(), CURLINFO_RESPONSE_CODE, &response_code);
453453

454454
// Handle HTTP status codes
455455
auto status = static_cast<http::status>(response_code);

0 commit comments

Comments
 (0)