Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HTTPRequest
bool InvokeDelayAction();
bool WaitingDelayAction() const { return m_timeRequestComplete != -1; }
#endif
void Threaded_SetComplete(CURLcode result);
void Threaded_SetComplete(CURLcode result, bool bInvokeCallback = true);

// mainly used for downloads
std::vector<uint8_t> GetBuffer() { return m_vecBuffer; }
Expand Down Expand Up @@ -97,4 +97,4 @@ class HTTPRequest
std::function<void(bool bSuccess, int statusCode, std::string strBody, HTTPRequest* pReq)> m_completionCallback = nullptr;

std::function<void(size_t bytesReceived)> m_progressCallback = nullptr;
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ void HTTPManager::Shutdown()
HTTPRequest* pRequest = *it;
if (pRequest != nullptr && pRequest->EasyHandleMatches(pCurlHandle))
{
pRequest->Threaded_SetComplete(m->data.result);
// During shutdown, don't invoke callbacks to avoid use-after-free
pRequest->Threaded_SetComplete(m->data.result, false);
delete pRequest;
m_vecRequestsInFlight.erase(it);
break;
Expand Down Expand Up @@ -226,7 +227,8 @@ void HTTPManager::Tick()
#if defined(ARTIFICIAL_DELAY_HTTP_REQUESTS)
pRequest->SetWaitingDelay(m->data.result);
#else
pRequest->Threaded_SetComplete(m->data.result);
// During normal operation (Tick), invoke callbacks
pRequest->Threaded_SetComplete(m->data.result, true);
vecItemsToRemove.push_back(pRequest);
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ bool HTTPRequest::InvokeDelayAction()
int64_t currTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::utc_clock::now().time_since_epoch()).count();
if (currTime - m_timeRequestComplete > 2000)
{
Threaded_SetComplete(m_pendingCURLCode);
// During normal operation (Tick), invoke callbacks
Threaded_SetComplete(m_pendingCURLCode, true);
return true;
}
}
Expand All @@ -133,7 +134,7 @@ bool HTTPRequest::InvokeDelayAction()

#endif

void HTTPRequest::Threaded_SetComplete(CURLcode result)
void HTTPRequest::Threaded_SetComplete(CURLcode result, bool bInvokeCallback)
{
// store response code
curl_easy_getinfo(m_pCURL, CURLINFO_RESPONSE_CODE, &m_responseCode);
Expand Down Expand Up @@ -171,8 +172,17 @@ void HTTPRequest::Threaded_SetComplete(CURLcode result)

NetworkLog(ELogVerbosity::LOG_RELEASE, "[%p|%s] Response was %d - %s!", this, strURIRedacted.c_str(), m_responseCode, strResponse.c_str());

// trigger callback
InvokeCallbackIfComplete();
// trigger callback if requested, otherwise clear it to avoid use-after-free during shutdown
if (bInvokeCallback)
{
InvokeCallbackIfComplete();
}
else
{
// Clear the callback to prevent potential use-after-free in std::function destructor
m_completionCallback = nullptr;
m_progressCallback = nullptr;
}
}

void HTTPRequest::PlatformStartRequest()
Expand Down
Loading