Skip to content
Merged
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 @@ -1062,17 +1062,42 @@ std::string NGMP_OnlineServicesManager::GetPatcherDirectoryPath()

void WebSocket::Shutdown()
{
// Return immediately if already shut down to prevent double-shutdown
// (e.g., NGMP_OnlineServicesManager::Shutdown() calls this before releasing the shared_ptr,
// and then the shared_ptr destructor also calls Shutdown() via ~WebSocket())
if (m_bShuttingDown)
{
return;
}

NetworkLog(ELogVerbosity::LOG_RELEASE, "[WebSocket] Shutdown initiated");

// Signal that we're shutting down
m_bShuttingDown = true;

// Disconnect from the websocket
// Disconnect from the websocket (handles the connected case)
Disconnect();

// Free headers
// Clean up curl easy handle if still active (e.g., mid-connection, not yet fully connected)
// Disconnect() returns early when m_bConnected is false, so m_pCurlWS may still be alive here
if (m_pCurlWS != nullptr)
{
if (m_pMulti != nullptr)
{
curl_multi_remove_handle(m_pMulti, m_pCurlWS);
}
curl_easy_cleanup(m_pCurlWS);
m_pCurlWS = nullptr;
}

// Clean up multi handle
if (m_pMulti != nullptr)
{
curl_multi_cleanup(m_pMulti);
m_pMulti = nullptr;
}

// Free headers (may already be freed by Disconnect, but check anyway)
if (m_pHeaders != nullptr)
{
curl_slist_free_all(m_pHeaders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ WebSocket::WebSocket()
WebSocket::~WebSocket()
{
Shutdown();
// Only call Shutdown if it has not been initiated already.
// NGMP_OnlineServicesManager::Shutdown() calls Shutdown() before releasing the shared_ptr,
// so calling it again from the destructor would redundantly block for another 100ms sleep
// and attempt to free already-released curl resources.
if (!m_bShuttingDown)
{
Shutdown();
}




if (m_pHeaders != nullptr)
{
Expand Down Expand Up @@ -196,6 +207,14 @@ void WebSocket::Disconnect()
curl_slist_free_all(m_pHeaders);
m_pHeaders = nullptr;
}


// Remove from multi handle before cleanup (required by libcurl)
if (m_pMulti != nullptr)
{
curl_multi_remove_handle(m_pMulti, m_pCurlWS);
}


// cleanup
curl_easy_cleanup(m_pCurlWS);
Expand Down Expand Up @@ -1424,4 +1443,5 @@ void NGMP_OnlineServices_RoomsInterface::OnRosterUpdated(std::unordered_map<uint
m_RosterNeedsRefreshCallback();
}
}


Expand Down
Loading