Skip to content

Commit 9a824c7

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 aa9b27f commit 9a824c7

File tree

2 files changed

+13
-41
lines changed

2 files changed

+13
-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: 12 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>
@@ -48,6 +47,10 @@
4847
#include <arpa/inet.h>
4948
#endif
5049

50+
#ifndef CURL_WRITEFUNC_ERROR
51+
#define CURL_WRITEFUNC_ERROR static_cast<size_t>(0xFFFFFFFF)
52+
#endif
53+
5154
namespace minio::http {
5255

5356
// MethodToString converts http Method enum to string.
@@ -273,16 +276,14 @@ error::Error Response::ReadHeaders() {
273276
return error::SUCCESS;
274277
}
275278

276-
size_t Response::ResponseCallback(curlpp::Multi* const requests,
277-
curlpp::Easy* const request,
279+
size_t Response::ResponseCallback(curlpp::Easy* const request,
278280
const char* const buffer, size_t size,
279281
size_t length) {
280282
size_t realsize = size * length;
281283

282284
// If error occurred previously, just cancel the request.
283285
if (!error.empty()) {
284-
requests->remove(request);
285-
return realsize;
286+
return CURL_WRITEFUNC_ERROR;
286287
}
287288

288289
if (!status_code_read_ || !headers_read_) {
@@ -292,8 +293,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
292293
if (!status_code_read_) {
293294
if (error::Error err = ReadStatusCode()) {
294295
error = err.String();
295-
requests->remove(request);
296-
return realsize;
296+
return CURL_WRITEFUNC_ERROR;
297297
}
298298

299299
if (!status_code_read_) return realsize;
@@ -302,8 +302,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
302302
if (!headers_read_) {
303303
if (error::Error err = ReadHeaders()) {
304304
error = err.String();
305-
requests->remove(request);
306-
return realsize;
305+
return CURL_WRITEFUNC_ERROR;
307306
}
308307

309308
if (!headers_read_ || response_.empty()) return realsize;
@@ -312,7 +311,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
312311
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
313312
DataFunctionArgs args(request, this, std::string(this->response_),
314313
userdata);
315-
if (!datafunc(args)) requests->remove(request);
314+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
316315
} else {
317316
body = response_;
318317
}
@@ -323,7 +322,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
323322
// If data function is set and the request is successful, send data.
324323
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
325324
DataFunctionArgs args(request, this, std::string(buffer, length), userdata);
326-
if (!datafunc(args)) requests->remove(request);
325+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
327326
} else {
328327
body.append(buffer, length);
329328
}
@@ -343,7 +342,6 @@ Request::Request(Method method, Url url) {
343342
Response Request::execute() {
344343
curlpp::Cleanup cleaner;
345344
curlpp::Easy request;
346-
curlpp::Multi requests;
347345

348346
// Request settings.
349347
request.setOpt(new curlpp::options::CustomRequest{MethodToString(method)});
@@ -403,8 +401,7 @@ Response Request::execute() {
403401

404402
using namespace std::placeholders;
405403
request.setOpt(new curlpp::options::WriteFunction(
406-
std::bind(&Response::ResponseCallback, &response, &requests, &request, _1,
407-
_2, _3)));
404+
std::bind(&Response::ResponseCallback, &response, &request, _1, _2, _3)));
408405

409406
auto progress =
410407
[&progressfunc = progressfunc, &progress_userdata = progress_userdata](
@@ -425,31 +422,8 @@ Response Request::execute() {
425422
request.setOpt(new curlpp::options::ProgressFunction(progress));
426423
}
427424

428-
int left = 0;
429-
requests.add(&request);
430-
431425
// 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-
}
426+
request.perform();
453427

454428
if (progressfunc != nullptr) {
455429
ProgressFunctionArgs args;

0 commit comments

Comments
 (0)