Skip to content

Commit 7ade364

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 b8ebbf3 commit 7ade364

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>
@@ -269,16 +268,14 @@ error::Error Response::ReadHeaders() {
269268
return error::SUCCESS;
270269
}
271270

272-
size_t Response::ResponseCallback(curlpp::Multi* const requests,
273-
curlpp::Easy* const request,
271+
size_t Response::ResponseCallback(curlpp::Easy* const request,
274272
const char* const buffer, size_t size,
275273
size_t length) {
276274
size_t realsize = size * length;
277275

278276
// If error occurred previously, just cancel the request.
279277
if (!error.empty()) {
280-
requests->remove(request);
281-
return realsize;
278+
return CURL_WRITEFUNC_ERROR;
282279
}
283280

284281
if (!status_code_read_ || !headers_read_) {
@@ -288,8 +285,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
288285
if (!status_code_read_) {
289286
if (error::Error err = ReadStatusCode()) {
290287
error = err.String();
291-
requests->remove(request);
292-
return realsize;
288+
return CURL_WRITEFUNC_ERROR;
293289
}
294290

295291
if (!status_code_read_) return realsize;
@@ -298,8 +294,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
298294
if (!headers_read_) {
299295
if (error::Error err = ReadHeaders()) {
300296
error = err.String();
301-
requests->remove(request);
302-
return realsize;
297+
return CURL_WRITEFUNC_ERROR;
303298
}
304299

305300
if (!headers_read_ || response_.empty()) return realsize;
@@ -308,7 +303,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
308303
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
309304
DataFunctionArgs args(request, this, std::string(this->response_),
310305
userdata);
311-
if (!datafunc(args)) requests->remove(request);
306+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
312307
} else {
313308
body = response_;
314309
}
@@ -319,7 +314,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
319314
// If data function is set and the request is successful, send data.
320315
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
321316
DataFunctionArgs args(request, this, std::string(buffer, length), userdata);
322-
if (!datafunc(args)) requests->remove(request);
317+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
323318
} else {
324319
body.append(buffer, length);
325320
}
@@ -339,7 +334,6 @@ Request::Request(Method method, Url url) {
339334
Response Request::execute() {
340335
curlpp::Cleanup cleaner;
341336
curlpp::Easy request;
342-
curlpp::Multi requests;
343337

344338
// Request settings.
345339
request.setOpt(new curlpp::options::CustomRequest{MethodToString(method)});
@@ -399,8 +393,7 @@ Response Request::execute() {
399393

400394
using namespace std::placeholders;
401395
request.setOpt(new curlpp::options::WriteFunction(
402-
std::bind(&Response::ResponseCallback, &response, &requests, &request, _1,
403-
_2, _3)));
396+
std::bind(&Response::ResponseCallback, &response, &request, _1, _2, _3)));
404397

405398
auto progress =
406399
[&progressfunc = progressfunc, &progress_userdata = progress_userdata](
@@ -421,31 +414,8 @@ Response Request::execute() {
421414
request.setOpt(new curlpp::options::ProgressFunction(progress));
422415
}
423416

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

450420
if (progressfunc != nullptr) {
451421
ProgressFunctionArgs args;

0 commit comments

Comments
 (0)