Skip to content

Commit a2526cc

Browse files
author
Florian Kaiser
committed
remove incorrect usage of curls Multi API
There are several issues introduced by #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 a2526cc

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.
@@ -269,16 +272,14 @@ error::Error Response::ReadHeaders() {
269272
return error::SUCCESS;
270273
}
271274

272-
size_t Response::ResponseCallback(curlpp::Multi* const requests,
273-
curlpp::Easy* const request,
275+
size_t Response::ResponseCallback(curlpp::Easy* const request,
274276
const char* const buffer, size_t size,
275277
size_t length) {
276278
size_t realsize = size * length;
277279

278280
// If error occurred previously, just cancel the request.
279281
if (!error.empty()) {
280-
requests->remove(request);
281-
return realsize;
282+
return CURL_WRITEFUNC_ERROR;
282283
}
283284

284285
if (!status_code_read_ || !headers_read_) {
@@ -288,8 +289,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
288289
if (!status_code_read_) {
289290
if (error::Error err = ReadStatusCode()) {
290291
error = err.String();
291-
requests->remove(request);
292-
return realsize;
292+
return CURL_WRITEFUNC_ERROR;
293293
}
294294

295295
if (!status_code_read_) return realsize;
@@ -298,8 +298,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
298298
if (!headers_read_) {
299299
if (error::Error err = ReadHeaders()) {
300300
error = err.String();
301-
requests->remove(request);
302-
return realsize;
301+
return CURL_WRITEFUNC_ERROR;
303302
}
304303

305304
if (!headers_read_ || response_.empty()) return realsize;
@@ -308,7 +307,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
308307
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
309308
DataFunctionArgs args(request, this, std::string(this->response_),
310309
userdata);
311-
if (!datafunc(args)) requests->remove(request);
310+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
312311
} else {
313312
body = response_;
314313
}
@@ -319,7 +318,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests,
319318
// If data function is set and the request is successful, send data.
320319
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
321320
DataFunctionArgs args(request, this, std::string(buffer, length), userdata);
322-
if (!datafunc(args)) requests->remove(request);
321+
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
323322
} else {
324323
body.append(buffer, length);
325324
}
@@ -339,7 +338,6 @@ Request::Request(Method method, Url url) {
339338
Response Request::execute() {
340339
curlpp::Cleanup cleaner;
341340
curlpp::Easy request;
342-
curlpp::Multi requests;
343341

344342
// Request settings.
345343
request.setOpt(new curlpp::options::CustomRequest{MethodToString(method)});
@@ -399,8 +397,7 @@ Response Request::execute() {
399397

400398
using namespace std::placeholders;
401399
request.setOpt(new curlpp::options::WriteFunction(
402-
std::bind(&Response::ResponseCallback, &response, &requests, &request, _1,
403-
_2, _3)));
400+
std::bind(&Response::ResponseCallback, &response, &request, _1, _2, _3)));
404401

405402
auto progress =
406403
[&progressfunc = progressfunc, &progress_userdata = progress_userdata](
@@ -421,31 +418,8 @@ Response Request::execute() {
421418
request.setOpt(new curlpp::options::ProgressFunction(progress));
422419
}
423420

424-
int left = 0;
425-
requests.add(&request);
426-
427421
// 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-
}
422+
request.perform();
449423

450424
if (progressfunc != nullptr) {
451425
ProgressFunctionArgs args;

0 commit comments

Comments
 (0)