diff --git a/Userland/Services/RequestServer/ConnectionCache.cpp b/Userland/Services/RequestServer/ConnectionCache.cpp index efcb88900e..74a9b6a532 100644 --- a/Userland/Services/RequestServer/ConnectionCache.cpp +++ b/Userland/Services/RequestServer/ConnectionCache.cpp @@ -38,9 +38,15 @@ void request_did_finish(URL const& url, Core::Socket const* socket) auto& connection = *connection_it; if (connection->request_queue.is_empty()) { + // Immediately mark the connection as finished, as new jobs will never be run if they are queued + // before the deferred_invoke() below runs otherwise. + connection->has_started = false; + connection->socket->set_notifications_enabled(false); + Core::deferred_invoke([&connection, &cache_entry = *it->value, key = it->key, &cache] { - connection->socket->set_notifications_enabled(false); - connection->has_started = false; + if (connection->has_started) + return; + connection->current_url = {}; connection->job_data = {}; connection->removal_timer->on_timeout = [ptr = connection.ptr(), &cache_entry, key = move(key), &cache]() mutable { diff --git a/Userland/Services/RequestServer/ConnectionCache.h b/Userland/Services/RequestServer/ConnectionCache.h index 2cbd354606..564b287731 100644 --- a/Userland/Services/RequestServer/ConnectionCache.h +++ b/Userland/Services/RequestServer/ConnectionCache.h @@ -241,13 +241,15 @@ decltype(auto) get_or_create_connection(auto& cache, URL const& url, auto& job, return ReturnType { nullptr }; } dbgln_if(REQUESTSERVER_DEBUG, "Immediately start request for url {} in {} - {}", url, &connection, connection.socket); - connection.has_started = true; - connection.removal_timer->stop(); - connection.timer.start(); - connection.current_url = url; - connection.job_data = decltype(connection.job_data)::create(job); - connection.socket->set_notifications_enabled(true); - connection.job_data.start(*connection.socket); + Core::deferred_invoke([&connection, url, &job] { + connection.has_started = true; + connection.removal_timer->stop(); + connection.timer.start(); + connection.current_url = url; + connection.job_data = decltype(connection.job_data)::create(job); + connection.socket->set_notifications_enabled(true); + connection.job_data.start(*connection.socket); + }); } else { dbgln_if(REQUESTSERVER_DEBUG, "Enqueue request for URL {} in {} - {}", url, &connection, connection.socket); connection.request_queue.append(decltype(connection.job_data)::create(job));