1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:47:35 +00:00

RequestServer: Avoid race condition between cached request end/start

Prior to this commit, it was possible to get a socket stuck in a state
with enqueued entries, waiting for a nonexistent request to finish.
This state could be entered by issuing a request immediately after the
completion of another one, and before deferred_invoke execution in the
event loop.

This commit closes this hole by making sure the socket is never in a
state where it can queue requests without an active job.
This commit is contained in:
Ali Mohammad Pur 2023-12-15 06:34:34 +03:30 committed by Andreas Kling
parent 47b6030347
commit 89877b3f40
2 changed files with 17 additions and 9 deletions

View file

@ -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));