From 3faba7515c757ca3183522bd017c0f76d9c4581c Mon Sep 17 00:00:00 2001
From: Mnikolenko Productengine <mnikolenko@productengine.com>
Date: Mon, 1 Nov 2021 19:38:55 +0200
Subject: [PATCH] SL-16237 FIXED Viewer hangs on login

---
 indra/llcommon/workqueue.h   |  6 ++++++
 indra/llrender/llimagegl.cpp | 17 ++++++++++++++++-
 indra/llrender/llimagegl.h   |  2 ++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/indra/llcommon/workqueue.h b/indra/llcommon/workqueue.h
index b88aef989ae..5ec790da799 100644
--- a/indra/llcommon/workqueue.h
+++ b/indra/llcommon/workqueue.h
@@ -94,6 +94,12 @@ namespace LL
         void postEvery(const std::chrono::duration<Rep, Period>& interval,
                        CALLABLE&& callable);
 
+        template <typename CALLABLE>
+        bool tryPost(CALLABLE&& callable)
+        {
+            return mQueue.tryPush(TimedWork(TimePoint::clock::now(), std::move(callable)));
+        }
+
         /*------------------------- handshake API --------------------------*/
 
         /**
diff --git a/indra/llrender/llimagegl.cpp b/indra/llrender/llimagegl.cpp
index 09b1c71f029..cbc53928824 100644
--- a/indra/llrender/llimagegl.cpp
+++ b/indra/llrender/llimagegl.cpp
@@ -2288,7 +2288,10 @@ bool LLImageGLThread::postCallback(const std::function<void()>& callback)
 {
     try
     {
-        mCallbackQueue.post(callback);
+        if (!mCallbackQueue.tryPost(callback))
+        {
+            mPendingCallbackQ.push(callback);
+        }
     }
     catch (LLThreadSafeQueueInterrupt e)
     {
@@ -2304,6 +2307,18 @@ void LLImageGLThread::executeCallbacks()
     LL_PROFILE_ZONE_SCOPED;
     //executed from main thread
     mCallbackQueue.runPending();
+
+    while (!mPendingCallbackQ.empty())
+    {
+        if (mCallbackQueue.tryPost(mPendingCallbackQ.front()))
+        {
+            mPendingCallbackQ.pop();
+        }
+        else
+        {
+            break;
+        }
+    }
 }
 
 void LLImageGLThread::run()
diff --git a/indra/llrender/llimagegl.h b/indra/llrender/llimagegl.h
index 95b60bd0bdc..8264e4a5f21 100644
--- a/indra/llrender/llimagegl.h
+++ b/indra/llrender/llimagegl.h
@@ -332,6 +332,8 @@ class LLImageGLThread : public LLThread
     void* mContext;
     LLAtomicBool mFinished;
 
+    std::queue<std::function<void()>> mPendingCallbackQ;
+
     static LLImageGLThread* sInstance;
 };
 
-- 
GitLab