Skip to content

Commit 1657b6e

Browse files
author
Florian Kaiser
committed
remove incorrect usage of curls Multi API
There are several issues introduced by minio#29 When running an application that is rarely restarted, that links to this sdk I encountered hangs inside of the select() call inside of http.c multiple times, that required a restart of the application. Looking into the man pages of curl, it is quite obvious, that the usage of select is a bad idea here. On one hand, the requests.fdset() call may return no filedescriptors, if there is currently nothing to wait for. (https://curl.se/libcurl/c/curl_multi_fdset.html) when fdset() returns no filedescriptors, maxfd is set to -1. this leads to a select call, that has nfds set to 0 and a disabled timeout. -> this leads to an infinite select() until either the program is terminated or a signal is received. On the other hand, if your application is running for some time, new sockets may have filedescriptors larger than FD_SETSIZE (1024), according to the docs (https://curl.se/libcurl/c/curl_multi_fdset.html), fdset does not return any filedescriptors in that case, which leads to the infinite select() again. There are two possible solutions to this: 1. waiting for this PR to be merged: jpbarrette/curlpp#173 - Use the poll() or wait() call from there 2. revert to the Easy API again. - If I understand it correctly the only reason for using the Multi API was to be able to abort transfers inside of the ResponseCallback, but that was implemented incorrectly anyway. Currently the request is removed by requests.remove(request) inside of the ResponseCallback, which is forbidden by: 'It is fine to remove a handle at any time during a transfer, just not from within any libcurl callback function.' (https://curl.se/libcurl/c/curl_multi_remove_handle.html) Due to these reasons I propose to revert back to the Easy API. To keep the possibility to abort a running request, CURL_WRITEFUNC_ERROR is returned at the corresponding locations inside of the ResponseCallback() function.
1 parent 0b6be6a commit 1657b6e

File tree

2 files changed

+9
-41
lines changed

2 files changed

+9
-41
lines changed

include/miniocpp/http.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#define MINIO_CPP_HTTP_H_INCLUDED
2020

2121
#include <curlpp/Easy.hpp>
22-
#include <curlpp/Multi.hpp>
2322
#include <exception>
2423
#include <functional>
2524
#include <iostream>
@@ -142,8 +141,7 @@ struct Response {
142141
Response() = default;
143142
~Response() = default;
144143

145-
size_t ResponseCallback(curlpp::Multi* const requests,
146-
curlpp::Easy* const request, const char* const buffer,
144+
size_t ResponseCallback(curlpp::Easy* const request, const char* const buffer,
147145
size_t size, size_t length);
148146

149147
explicit operator bool() const {

src/http.cc

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <curlpp/Easy.hpp>
2323
#include <curlpp/Exception.hpp>
2424
#include <curlpp/Infos.hpp>
25-
#include <curlpp/Multi.hpp>
2625
#include <curlpp/Options.hpp>
2726
#include <curlpp/cURLpp.hpp>
2827
#include <exception>
@@ -273,16 +272,14 @@ error::Error Response::ReadHeaders() {
273272
return error::SUCCESS;
274273
}
275274

276-
size_t Response::ResponseCallback(curlpp::Multi* const requests,
277-
curlpp::Easy* const request,
275+
size_t Response::ResponseCallback(curlpp::Easy* const request,
278276
const char* const buffer, size_t size,
279277
size_t length) {
280278
size_t realsize = size * length;
281279

282280
// If error occurred previously, just cancel the request.
283281
if (!error.empty()) {
284-
requests->remove(request);
285-
return realsize;
282+
return CURL_WRITEFUNC_ERROR;
286283
}
287284

288285
if (!status_code_read_ || !headers_read_) {
@@ -292,8 +289,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
292289
if (!status_code_read_) {
293290
if (error::Error err = ReadStatusCode()) {
294291
error = err.String();
295-
requests->remove(request);
296-
return realsize;
292+
return CURL_WRITEFUNC_ERROR;
297293
}
298294

299295
if (!status_code_read_) return realsize;
@@ -302,8 +298,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
302298
if (!headers_read_) {
303299
if (error::Error err = ReadHeaders()) {
304300
error = err.String();
305-
requests->remove(request);
306-
return realsize;
301+
return CURL_WRITEFUNC_ERROR;
307302
}
308303

309304
if (!headers_read_ || response_.empty()) return realsize;
@@ -312,7 +307,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
312307
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
313308
DataFunctionArgs args(request, this, std::string(this->response_),
314309
userdata);
315-
if (!datafunc(args)) requests->remove(request);
310+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
316311
} else {
317312
body = response_;
318313
}
@@ -323,7 +318,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
323318
// If data function is set and the request is successful, send data.
324319
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
325320
DataFunctionArgs args(request, this, std::string(buffer, length), userdata);
326-
if (!datafunc(args)) requests->remove(request);
321+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
327322
} else {
328323
body.append(buffer, length);
329324
}
@@ -343,7 +338,6 @@ Request::Request(Method method, Url url) {
343338
Response Request::execute() {
344339
curlpp::Cleanup cleaner;
345340
curlpp::Easy request;
346-
curlpp::Multi requests;
347341

348342
// Request settings.
349343
request.setOpt(new curlpp::options::CustomRequest{MethodToString(method)});
@@ -403,8 +397,7 @@ Response Request::execute() {
403397

404398
using namespace std::placeholders;
405399
request.setOpt(new curlpp::options::WriteFunction(
406-
std::bind(&Response::ResponseCallback, &response, &requests, &request, _1,
407-
_2, _3)));
400+
std::bind(&Response::ResponseCallback, &response, &request, _1, _2, _3)));
408401

409402
auto progress =
410403
[&progressfunc = progressfunc, &progress_userdata = progress_userdata](
@@ -425,31 +418,8 @@ Response Request::execute() {
425418
request.setOpt(new curlpp::options::ProgressFunction(progress));
426419
}
427420

428-
int left = 0;
429-
requests.add(&request);
430-
431421
// Execute.
432-
while (!requests.perform(&left)) {
433-
}
434-
while (left) {
435-
fd_set fdread{};
436-
fd_set fdwrite{};
437-
fd_set fdexcep{};
438-
int maxfd = 0;
439-
440-
FD_ZERO(&fdread);
441-
FD_ZERO(&fdwrite);
442-
FD_ZERO(&fdexcep);
443-
444-
requests.fdset(&fdread, &fdwrite, &fdexcep, &maxfd);
445-
446-
if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, nullptr) < 0) {
447-
std::cerr << "select() failed; this should not happen" << std::endl;
448-
std::terminate();
449-
}
450-
while (!requests.perform(&left)) {
451-
}
452-
}
422+
request.perform();
453423

454424
if (progressfunc != nullptr) {
455425
ProgressFunctionArgs args;

0 commit comments

Comments
 (0)