From a2526cc73ee51db151ed7fd1676f6912adb2ff3e Mon Sep 17 00:00:00 2001 From: Florian Kaiser Date: Sun, 5 Jan 2025 12:43:55 +0100 Subject: [PATCH] remove incorrect usage of curls Multi API There are several issues introduced by https://github.com/minio/minio-cpp/pull/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: https://github.com/jpbarrette/curlpp/pull/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. --- include/miniocpp/http.h | 4 +--- src/http.cc | 50 ++++++++++------------------------------- 2 files changed, 13 insertions(+), 41 deletions(-) diff --git a/include/miniocpp/http.h b/include/miniocpp/http.h index a3754a3..4d9de06 100644 --- a/include/miniocpp/http.h +++ b/include/miniocpp/http.h @@ -19,7 +19,6 @@ #define MINIO_CPP_HTTP_H_INCLUDED #include -#include #include #include #include @@ -142,8 +141,7 @@ struct Response { Response() = default; ~Response() = default; - size_t ResponseCallback(curlpp::Multi* const requests, - curlpp::Easy* const request, const char* const buffer, + size_t ResponseCallback(curlpp::Easy* const request, const char* const buffer, size_t size, size_t length); explicit operator bool() const { diff --git a/src/http.cc b/src/http.cc index 8af3dfa..8328035 100644 --- a/src/http.cc +++ b/src/http.cc @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -48,6 +47,10 @@ #include #endif +#ifndef CURL_WRITEFUNC_ERROR +#define CURL_WRITEFUNC_ERROR static_cast(0xFFFFFFFF) +#endif + namespace minio::http { // MethodToString converts http Method enum to string. @@ -269,16 +272,14 @@ error::Error Response::ReadHeaders() { return error::SUCCESS; } -size_t Response::ResponseCallback(curlpp::Multi* const requests, - curlpp::Easy* const request, +size_t Response::ResponseCallback(curlpp::Easy* const request, const char* const buffer, size_t size, size_t length) { size_t realsize = size * length; // If error occurred previously, just cancel the request. if (!error.empty()) { - requests->remove(request); - return realsize; + return CURL_WRITEFUNC_ERROR; } if (!status_code_read_ || !headers_read_) { @@ -288,8 +289,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests, if (!status_code_read_) { if (error::Error err = ReadStatusCode()) { error = err.String(); - requests->remove(request); - return realsize; + return CURL_WRITEFUNC_ERROR; } if (!status_code_read_) return realsize; @@ -298,8 +298,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests, if (!headers_read_) { if (error::Error err = ReadHeaders()) { error = err.String(); - requests->remove(request); - return realsize; + return CURL_WRITEFUNC_ERROR; } if (!headers_read_ || response_.empty()) return realsize; @@ -308,7 +307,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests, if (datafunc != nullptr && status_code >= 200 && status_code <= 299) { DataFunctionArgs args(request, this, std::string(this->response_), userdata); - if (!datafunc(args)) requests->remove(request); + if (!datafunc(args)) return CURL_WRITEFUNC_ERROR; } else { body = response_; } @@ -319,7 +318,7 @@ size_t Response::ResponseCallback(curlpp::Multi* const requests, // If data function is set and the request is successful, send data. if (datafunc != nullptr && status_code >= 200 && status_code <= 299) { DataFunctionArgs args(request, this, std::string(buffer, length), userdata); - if (!datafunc(args)) requests->remove(request); + if (!datafunc(args)) return CURL_WRITEFUNC_ERROR; } else { body.append(buffer, length); } @@ -339,7 +338,6 @@ Request::Request(Method method, Url url) { Response Request::execute() { curlpp::Cleanup cleaner; curlpp::Easy request; - curlpp::Multi requests; // Request settings. request.setOpt(new curlpp::options::CustomRequest{MethodToString(method)}); @@ -399,8 +397,7 @@ Response Request::execute() { using namespace std::placeholders; request.setOpt(new curlpp::options::WriteFunction( - std::bind(&Response::ResponseCallback, &response, &requests, &request, _1, - _2, _3))); + std::bind(&Response::ResponseCallback, &response, &request, _1, _2, _3))); auto progress = [&progressfunc = progressfunc, &progress_userdata = progress_userdata]( @@ -421,31 +418,8 @@ Response Request::execute() { request.setOpt(new curlpp::options::ProgressFunction(progress)); } - int left = 0; - requests.add(&request); - // Execute. - while (!requests.perform(&left)) { - } - while (left) { - fd_set fdread{}; - fd_set fdwrite{}; - fd_set fdexcep{}; - int maxfd = 0; - - FD_ZERO(&fdread); - FD_ZERO(&fdwrite); - FD_ZERO(&fdexcep); - - requests.fdset(&fdread, &fdwrite, &fdexcep, &maxfd); - - if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, nullptr) < 0) { - std::cerr << "select() failed; this should not happen" << std::endl; - std::terminate(); - } - while (!requests.perform(&left)) { - } - } + request.perform(); if (progressfunc != nullptr) { ProgressFunctionArgs args;