[vlc-devel] [PATCH 1/2] qt: medialib: fix race condition

Romain Vimont rom1v at videolabs.io
Mon Nov 9 17:33:57 CET 2020


If cancel() was called before the first lock was acquired in run(), then
cancel() would return immediately and run() would continue as if it was
not canceled (which could cause segfaults).

To avoid the problem, always set the "canceled" flag on cancel, so that
any future execution of run() will return immediately.

Note: we could alternatively track the "finished" state, and wait until
the task is finished on cancel(). However, if run() may not start
immediately (if the thread pool is full), this strategy would cause to
block on cancel() until run() is actually executed, which is
unnecessary.
---
 modules/gui/qt/medialibrary/mlgenre.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/gui/qt/medialibrary/mlgenre.cpp b/modules/gui/qt/medialibrary/mlgenre.cpp
index a1067d52be..d2f13f37f7 100644
--- a/modules/gui/qt/medialibrary/mlgenre.cpp
+++ b/modules/gui/qt/medialibrary/mlgenre.cpp
@@ -185,9 +185,9 @@ public:
     void cancel()
     {
         QMutexLocker lock(&m_taskLock);
+        m_canceled = true;
         if (!m_running)
             return;
-        m_canceled = true;
         m_taskCond.wait(&m_taskLock);
     }
 
-- 
2.29.2



More information about the vlc-devel mailing list