From c895f8c06282bd3298790113f1305c92d9f268e9 Mon Sep 17 00:00:00 2001
From: andreykproductengine <andreykproductengine@lindenlab.com>
Date: Wed, 4 Jul 2018 16:18:08 +0300
Subject: MAINT-8800 Vivox pointlesly stalls for 10 seconds waiting on shutdown

---
 indra/newview/llappviewer.cpp  | 10 +++++-----
 indra/newview/llvoicevivox.cpp | 28 +++++++++++++++++++++-------
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/indra/newview/llappviewer.cpp b/indra/newview/llappviewer.cpp
index acec68f393..687b76c224 100644
--- a/indra/newview/llappviewer.cpp
+++ b/indra/newview/llappviewer.cpp
@@ -1581,6 +1581,11 @@ bool LLAppViewer::doFrame()
 			saveFinalSnapshot();
 		}
 
+		if (LLVoiceClient::instanceExists())
+		{
+			LLVoiceClient::getInstance()->terminate();
+		}
+
 		delete gServicePump;
 
 		destroyMainloopTimeout();
@@ -1680,11 +1685,6 @@ bool LLAppViewer::cleanup()
     // Give any remaining SLPlugin instances a chance to exit cleanly.
     LLPluginProcessParent::shutdown();
 
-	if (LLVoiceClient::instanceExists())
-	{
-		LLVoiceClient::getInstance()->terminate();
-	}
-
 	disconnectViewer();
 
 	LL_INFOS() << "Viewer disconnected" << LL_ENDL;
diff --git a/indra/newview/llvoicevivox.cpp b/indra/newview/llvoicevivox.cpp
index 6a3242b43f..1676f70b1e 100644
--- a/indra/newview/llvoicevivox.cpp
+++ b/indra/newview/llvoicevivox.cpp
@@ -542,6 +542,10 @@ void LLVivoxVoiceClient::connectorShutdown()
 		
 		writeString(stream.str());
 	}
+	else
+	{
+		mShutdownComplete = true;
+	}
 }
 
 void LLVivoxVoiceClient::userAuthorized(const std::string& user_id, const LLUUID &agentID)
@@ -1046,16 +1050,25 @@ bool LLVivoxVoiceClient::breakVoiceConnection(bool corowait)
         retval = result.has("connector");
     }
     else
-    {   // If we are not doing a corowait then we must sleep until the connector has responded
+    {
+        mRelogRequested = false; //stop the control coro
+        // If we are not doing a corowait then we must sleep until the connector has responded
         // otherwise we may very well close the socket too early.
 #if LL_WINDOWS
-        int count = 0;
-        while (!mShutdownComplete && 10 > count++)
-        {   // Rider: This comes out to a max wait time of 10 seconds.  
-            // The situation that brings us here is a call from ::terminate() 
-            // and so the viewer is attempting to go away.  Don't slow it down 
-            // longer than this.
+        if (!mShutdownComplete)
+        {
+            // The situation that brings us here is a call from ::terminate()
+            // At this point message system is already down so we can't wait for
+            // the message, yet we need to receive "connector shutdown response".
+            // Either wait a bit and emulate it or check gMessageSystem for specific message
             _sleep(1000);
+            if (mConnected)
+            {
+                mConnected = false;
+                LLSD vivoxevent(LLSDMap("connector", LLSD::Boolean(false)));
+                LLEventPumps::instance().post("vivoxClientPump", vivoxevent);
+            }
+            mShutdownComplete = true;
         }
 #endif
     }
@@ -3243,6 +3256,7 @@ void LLVivoxVoiceClient::connectorShutdownResponse(int statusCode, std::string &
 	}
 	
 	mConnected = false;
+	mShutdownComplete = true;
 	
     LLSD vivoxevent(LLSDMap("connector", LLSD::Boolean(false)));
 
-- 
cgit v1.2.3