From 2d412aeab50cc1b8f153d924eabe4839b2a1c998 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Wed, 1 Sep 2021 14:16:45 +0200 Subject: [PATCH] Make a reaper usable for processes from non-main thread The reap() method needs to be called from the same thread as the passed process lives in. In case we are passing a process that lives in a non-main thread, we move it into the main thread and schedule a call to reap it again, this time from the main thread. In addition, make sure that Internal::d is accessed only from the main thread. Change-Id: I32c836584b5da3050f9254e53cee352c79e536b9 Reviewed-by: Qt CI Bot Reviewed-by: Eike Ziller --- src/plugins/coreplugin/reaper.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/plugins/coreplugin/reaper.cpp b/src/plugins/coreplugin/reaper.cpp index fa8408abb17..1932d60dba8 100644 --- a/src/plugins/coreplugin/reaper.cpp +++ b/src/plugins/coreplugin/reaper.cpp @@ -157,6 +157,19 @@ void reap(QtcProcess *process, int timeoutMs) if (!process) return; + QTC_ASSERT(QThread::currentThread() == process->thread(), return); + + // Neither can move object with a parent into a different thread + // nor reaping the process with a parent makes any sense. + process->setParent(nullptr); + if (process->thread() != qApp->thread()) { + process->moveToThread(qApp->thread()); + QMetaObject::invokeMethod(process, [process, timeoutMs] { + reap(process, timeoutMs); + }); // will be queued + return; + } + QTC_ASSERT(Internal::d, return); new Internal::ProcessReaper(process, timeoutMs);