Skip to content
Snippets Groups Projects
Verified Commit 0a49dfe5 authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Fix lockup when adding requests during shutdown

parent 84a7f7da
No related branches found
No related tags found
No related merge requests found
Pipeline #2758 failed
......@@ -177,6 +177,7 @@ struct Client {
CURLM *multi{};
int still_running = 0;
std::atomic<bool> stopped{false};
std::atomic<bool> prevent_new_requests{false};
bool verify_peer_ = true;
long connection_timeout_ = 0;
......
......@@ -6,9 +6,8 @@
#include <curl/curl.h>
namespace coeurl {
const char* to_string(CURLcode c);
//const char* to_string(CURLUcode c);
const char* to_string(CURLMcode c);
const char* to_string(CURLSHcode c);
};
const char *to_string(CURLcode c);
// const char* to_string(CURLUcode c);
const char *to_string(CURLMcode c);
const char *to_string(CURLSHcode c);
}; // namespace coeurl
......@@ -133,15 +133,21 @@ void Client::cancel_requests_cb(evutil_socket_t, short, void *userp) {
Client *g = (Client *)userp;
{
// prevent new requests from being added
const std::scoped_lock lock(g->pending_requests_mutex);
{
g->prevent_new_requests = true;
}
// safe to access now, since we are running on the worker thread and only
// there running_requests is modified
while (!g->running_requests.empty())
g->remove_request(g->running_requests.back().get());
}
// Allow for new requests
{
g->prevent_new_requests = false;
}
CURLMcode rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running);
mcode_or_die("timer_cb: curl_multi_socket_action", rc);
......@@ -297,6 +303,14 @@ void Client::check_multi_info() {
void Client::submit_request(std::shared_ptr<Request> conn) {
Client::log->trace("SUBMIT");
if (this->prevent_new_requests) {
conn->curl_error = CURLE_ABORTED_BY_CALLBACK;
conn->status = Request::Status::Canceled;
if (conn->on_complete_)
conn->on_complete_(*conn.get());
return;
}
{
const std::scoped_lock lock(pending_requests_mutex);
......
......@@ -201,3 +201,18 @@ TEST_CASE("Basic simple DELETE request with body") {
CHECK(r.response() == "hello");
});
}
TEST_CASE("Shutdown") {
Client g{};
g.get("http://localhost:5000/", [&g](const Request &r) {
CHECK(r.url() == "http://localhost:5000/");
CHECK(r.response_code() != 200);
g.get("http://localhost:5000/", [](const Request &r) {
CHECK(r.url() == "http://localhost:5000/");
CHECK(r.error_code() == CURLE_ABORTED_BY_CALLBACK);
});
});
g.shutdown();
}
......@@ -219,3 +219,18 @@ TEST_CASE("Basic simple DELETE request with body") {
CHECK(r.response() == "hello");
});
}
TEST_CASE("Shutdown") {
Client g{};
g.get("https://localhost:5443/", [&g](const Request &r) {
CHECK(r.url() == "https://localhost:5443/");
CHECK(r.response_code() != 200);
g.get("https://localhost:5000/", [](const Request &r) {
CHECK(r.url() == "https://localhost:5443/");
CHECK(r.error_code() == CURLE_ABORTED_BY_CALLBACK);
});
});
g.shutdown();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment