From f9e9dd19ccd16b184409b94d567d220ed717aa1b Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 22 Dec 2022 11:58:17 +0100 Subject: [PATCH] runOnAndroidMainThread(): Don't block QThreadPool::globalInstance() with timout awaiters A waiting task on QThreadPool::globalInstance() will block the worker thread that it was scheduled on, making it unavailable for productive work. That's why one should only put CPU-bound tasks onto QThreadPool::globalInstance(). When blocking nonetheless, use the releaseThread()/reserveThread() trick to avoid deadlocks caused by the pool running out of workers. So, do that here. Task-number: QTBUG-109586 Change-Id: Ia2660c69e1f23b5df0c308576301aac6e05d4725 Reviewed-by: Qt CI Bot Reviewed-by: Assam Boudjelthia (cherry picked from commit c0496013484c35ab9b1a29ffb0f1eb687ef6db78) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/platform/android/qandroidnativeinterface.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/corelib/platform/android/qandroidnativeinterface.cpp b/src/corelib/platform/android/qandroidnativeinterface.cpp index fb6044c1bb4..7e79f112f2d 100644 --- a/src/corelib/platform/android/qandroidnativeinterface.cpp +++ b/src/corelib/platform/android/qandroidnativeinterface.cpp @@ -176,6 +176,14 @@ QFuture QNativeInterface::QAndroidApplication::runOnAndroidMainThread( loop.quit(); }); watcher.setFuture(future); + + // we're going to sleep, make sure we don't block + // QThreadPool::globalInstance(): + + QThreadPool::globalInstance()->releaseThread(); + const auto sg = qScopeGuard([] { + QThreadPool::globalInstance()->reserveThread(); + }); loop.exec(); }); }