From 5b8e4e3abe30c1803e0d42d47054250e2cb4bc82 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 11 Sep 2016 23:36:28 -0700 Subject: [PATCH] Fix deadlock when calling StopConnection() during connection startup --- main.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index e160fc6..7c5d85b 100644 --- a/main.cpp +++ b/main.cpp @@ -58,10 +58,6 @@ void MoonlightInstance::OnConnectionStopped(uint32_t error) { // Unlock the mouse UnlockMouse(); - // Join threads - pthread_join(m_ConnectionThread, NULL); - pthread_join(m_GamepadThread, NULL); - // Notify the JS code that the stream has ended pp::Var response(MSG_STREAM_TERMINATED); PostMessage(response); @@ -80,6 +76,17 @@ void MoonlightInstance::StopConnection() { } void* MoonlightInstance::StopThreadFunc(void* context) { + // We must join the connection thread first, because LiStopConnection must + // not be invoked during LiStartConnection. + pthread_join(g_Instance->m_ConnectionThread, NULL); + + // Not running anymore + g_Instance->m_Running = false; + + // We also need to stop this thread after the connection thread, because it depends + // on being initialized there. + pthread_join(g_Instance->m_GamepadThread, NULL); + // Stop the connection LiStopConnection(); return NULL;