Fix HTTP client shutdown and deadlock on request error

Fix HTTP client effectively shutting down by entering the `ERROR` state when a request cannot be added. Now only the invalid request is aborted immediately. The `ERROR` state is now only entered when a curl function fails in a way where recovery is not possible.

Fix occasional deadlock when HTTP client is shutting down after entering the `ERROR` state, by also immediately aborting new requests when the HTTP client is already in the `ERROR` state.

Remove unused `CHttp::EState::STOPPING`.
This commit is contained in:
Robert Müller 2024-03-04 23:29:01 +01:00 committed by Dennis Felsing
parent 52a2e17b55
commit 891242f9a2
2 changed files with 16 additions and 7 deletions

View file

@ -454,7 +454,7 @@ void CHttp::RunLoop()
if(PollCode != CURLM_OK)
{
Lock.lock();
log_error("http", "Failed multi wait: %s", curl_multi_strerror(PollCode));
log_error("http", "curl_multi_poll failed: %s", curl_multi_strerror(PollCode));
m_State = CHttp::ERROR;
break;
}
@ -463,7 +463,7 @@ void CHttp::RunLoop()
if(PerformCode != CURLM_OK)
{
Lock.lock();
log_error("http", "Failed multi perform: %s", curl_multi_strerror(PerformCode));
log_error("http", "curl_multi_perform failed: %s", curl_multi_strerror(PerformCode));
m_State = CHttp::ERROR;
break;
}
@ -497,23 +497,33 @@ void CHttp::RunLoop()
CURL *pEH = curl_easy_init();
if(!pEH)
{
log_error("http", "curl_easy_init failed");
goto error_init;
}
if(!pRequest->ConfigureHandle(pEH))
goto error_configure;
{
curl_easy_cleanup(pEH);
str_copy(pRequest->m_aErr, "Failed to initialize request");
pRequest->OnCompletionInternal(CURLE_ABORTED_BY_CALLBACK);
NewRequests.pop_front();
continue;
}
if(curl_multi_add_handle(m_pMultiH, pEH) != CURLM_OK)
{
log_error("http", "curl_multi_add_handle failed");
goto error_configure;
}
m_RunningRequests.emplace(pEH, std::move(pRequest));
NewRequests.pop_front();
continue;
error_configure:
curl_easy_cleanup(pEH);
error_init:
log_error("http", "failed to start new request");
Lock.lock();
m_State = CHttp::ERROR;
break;
@ -561,7 +571,7 @@ void CHttp::Run(std::shared_ptr<IHttpRequest> pRequest)
{
std::shared_ptr<CHttpRequest> pRequestImpl = std::static_pointer_cast<CHttpRequest>(pRequest);
std::unique_lock Lock(m_Lock);
if(m_Shutdown)
if(m_Shutdown || m_State == CHttp::ERROR)
{
str_copy(pRequestImpl->m_aErr, "Shutting down");
pRequestImpl->OnCompletionInternal(CURLE_ABORTED_BY_CALLBACK);

View file

@ -248,7 +248,6 @@ class CHttp : public IHttp
{
UNINITIALIZED,
RUNNING,
STOPPING,
ERROR,
};