1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:37:35 +00:00

LibCore+RequestServer: Ignore callbacks for cancelled network jobs

Also cancel the jobs when they're destroyed.
This makes sure that jobs whose owners have discarded don't end up
crashing because of a did_fail().
This commit is contained in:
Ali Mohammad Pur 2021-09-19 18:18:19 +04:30 committed by Ali Mohammad Pur
parent 436693c0c9
commit 81a0301d4d
4 changed files with 12 additions and 3 deletions

View file

@ -29,6 +29,9 @@ void NetworkJob::shutdown()
void NetworkJob::did_finish(NonnullRefPtr<NetworkResponse>&& response) void NetworkJob::did_finish(NonnullRefPtr<NetworkResponse>&& response)
{ {
if (is_cancelled())
return;
// NOTE: We protect ourselves here, since the on_finish callback may otherwise // NOTE: We protect ourselves here, since the on_finish callback may otherwise
// trigger destruction of this job somehow. // trigger destruction of this job somehow.
NonnullRefPtr<NetworkJob> protector(*this); NonnullRefPtr<NetworkJob> protector(*this);
@ -42,6 +45,9 @@ void NetworkJob::did_finish(NonnullRefPtr<NetworkResponse>&& response)
void NetworkJob::did_fail(Error error) void NetworkJob::did_fail(Error error)
{ {
if (is_cancelled())
return;
// NOTE: We protect ourselves here, since the on_finish callback may otherwise // NOTE: We protect ourselves here, since the on_finish callback may otherwise
// trigger destruction of this job somehow. // trigger destruction of this job somehow.
NonnullRefPtr<NetworkJob> protector(*this); NonnullRefPtr<NetworkJob> protector(*this);
@ -55,6 +61,9 @@ void NetworkJob::did_fail(Error error)
void NetworkJob::did_progress(Optional<u32> total_size, u32 downloaded) void NetworkJob::did_progress(Optional<u32> total_size, u32 downloaded)
{ {
if (is_cancelled())
return;
// NOTE: We protect ourselves here, since the callback may otherwise // NOTE: We protect ourselves here, since the callback may otherwise
// trigger destruction of this job somehow. // trigger destruction of this job somehow.
NonnullRefPtr<NetworkJob> protector(*this); NonnullRefPtr<NetworkJob> protector(*this);

View file

@ -58,7 +58,7 @@ GeminiRequest::~GeminiRequest()
{ {
m_job->on_finish = nullptr; m_job->on_finish = nullptr;
m_job->on_progress = nullptr; m_job->on_progress = nullptr;
m_job->shutdown(); m_job->cancel();
} }
NonnullOwnPtr<GeminiRequest> GeminiRequest::create_with_job(Badge<GeminiProtocol>, ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream) NonnullOwnPtr<GeminiRequest> GeminiRequest::create_with_job(Badge<GeminiProtocol>, ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)

View file

@ -22,7 +22,7 @@ HttpRequest::~HttpRequest()
{ {
m_job->on_finish = nullptr; m_job->on_finish = nullptr;
m_job->on_progress = nullptr; m_job->on_progress = nullptr;
m_job->shutdown(); m_job->cancel();
} }
NonnullOwnPtr<HttpRequest> HttpRequest::create_with_job(Badge<HttpProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream) NonnullOwnPtr<HttpRequest> HttpRequest::create_with_job(Badge<HttpProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)

View file

@ -27,7 +27,7 @@ HttpsRequest::~HttpsRequest()
{ {
m_job->on_finish = nullptr; m_job->on_finish = nullptr;
m_job->on_progress = nullptr; m_job->on_progress = nullptr;
m_job->shutdown(); m_job->cancel();
} }
NonnullOwnPtr<HttpsRequest> HttpsRequest::create_with_job(Badge<HttpsProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream) NonnullOwnPtr<HttpsRequest> HttpsRequest::create_with_job(Badge<HttpsProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)