Merge pull request #7964 from Techjar/fix-netplayindex-segfault

UICommon/NetPlayIndex: Fix random segfaults after quitting NetPlay
This commit is contained in:
spycrab 2019-04-09 12:45:58 +02:00 committed by GitHub
commit 746849f891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 13 deletions

View File

@ -4,6 +4,7 @@
#include "UICommon/NetPlayIndex.h" #include "UICommon/NetPlayIndex.h"
#include <chrono>
#include <numeric> #include <numeric>
#include <string> #include <string>
@ -124,7 +125,7 @@ NetPlayIndex::List(const std::map<std::string, std::string>& filters)
void NetPlayIndex::NotificationLoop() void NetPlayIndex::NotificationLoop()
{ {
while (m_running.IsSet()) while (!m_session_thread_exit_event.WaitFor(std::chrono::seconds(5)))
{ {
Common::HttpRequest request; Common::HttpRequest request;
auto response = request.Get( auto response = request.Get(
@ -141,7 +142,6 @@ void NetPlayIndex::NotificationLoop()
if (!json) if (!json)
{ {
m_last_error = "BAD_JSON"; m_last_error = "BAD_JSON";
m_running.Set(false);
return; return;
} }
@ -150,18 +150,13 @@ void NetPlayIndex::NotificationLoop()
if (status != "OK") if (status != "OK")
{ {
m_last_error = std::move(status); m_last_error = std::move(status);
m_running.Set(false);
return; return;
} }
Common::SleepCurrentThread(1000 * 5);
} }
} }
bool NetPlayIndex::Add(NetPlaySession session) bool NetPlayIndex::Add(NetPlaySession session)
{ {
m_running.Set(true);
Common::HttpRequest request; Common::HttpRequest request;
auto response = request.Get(Config::Get(Config::NETPLAY_INDEX_URL) + auto response = request.Get(Config::Get(Config::NETPLAY_INDEX_URL) +
"/v0/session/add?name=" + request.EscapeComponent(session.name) + "/v0/session/add?name=" + request.EscapeComponent(session.name) +
@ -204,8 +199,6 @@ bool NetPlayIndex::Add(NetPlaySession session)
m_session_thread = std::thread([this] { NotificationLoop(); }); m_session_thread = std::thread([this] { NotificationLoop(); });
m_session_thread.detach();
return true; return true;
} }
@ -229,7 +222,7 @@ void NetPlayIndex::Remove()
if (m_secret.empty()) if (m_secret.empty())
return; return;
m_running.Set(false); m_session_thread_exit_event.Set();
if (m_session_thread.joinable()) if (m_session_thread.joinable())
m_session_thread.join(); m_session_thread.join();

View File

@ -11,7 +11,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "Common/Flag.h" #include "Common/Event.h"
struct NetPlaySession struct NetPlaySession
{ {
@ -55,8 +55,6 @@ public:
private: private:
void NotificationLoop(); void NotificationLoop();
Common::Flag m_running;
std::string m_secret; std::string m_secret;
std::string m_game; std::string m_game;
int m_player_count = 0; int m_player_count = 0;
@ -64,4 +62,6 @@ private:
std::string m_last_error; std::string m_last_error;
std::thread m_session_thread; std::thread m_session_thread;
Common::Event m_session_thread_exit_event;
}; };