Skip to content

Commit e75b934

Browse files
committed
Fix stuck tasks when proxied task using a proxy progress task
completes before the proxy task has been started by task manager Fixes #21589, fixes #19761 (cherry picked from commit b28227f)
1 parent 32081b6 commit e75b934

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

src/core/qgsproxyprogresstask.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,20 @@ QgsProxyProgressTask::QgsProxyProgressTask( const QString &description )
2424

2525
void QgsProxyProgressTask::finalize( bool result )
2626
{
27+
QMutexLocker lock( &mNotFinishedMutex );
28+
mAlreadyFinished = true;
29+
2730
mResult = result;
2831
mNotFinishedWaitCondition.wakeAll();
2932
}
3033

3134
bool QgsProxyProgressTask::run()
3235
{
3336
mNotFinishedMutex.lock();
34-
mNotFinishedWaitCondition.wait( &mNotFinishedMutex );
37+
if ( !mAlreadyFinished )
38+
{
39+
mNotFinishedWaitCondition.wait( &mNotFinishedMutex );
40+
}
3541
mNotFinishedMutex.unlock();
3642

3743
return mResult;

src/core/qgsproxyprogresstask.h

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class CORE_EXPORT QgsProxyProgressTask : public QgsTask
6464

6565
QWaitCondition mNotFinishedWaitCondition;
6666
QMutex mNotFinishedMutex;
67+
bool mAlreadyFinished = false;
6768
bool mResult = true;
6869

6970
};

tests/src/core/testqgstaskmanager.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsproject.h"
2020
#include "qgsvectorlayer.h"
2121
#include "qgsapplication.h"
22+
#include "qgsproxyprogresstask.h"
2223
#include <QObject>
2324
#include "qgstest.h"
2425

@@ -270,6 +271,9 @@ class TestQgsTaskManager : public QObject
270271
void managerWithSubTasks2();
271272
void managerWithSubTasks3();
272273
void cancelBeforeStart();
274+
void proxyTask();
275+
void proxyTask2();
276+
void scopedProxyTask();
273277
};
274278

275279
void TestQgsTaskManager::initTestCase()
@@ -1374,5 +1378,75 @@ void TestQgsTaskManager::cancelBeforeStart()
13741378
flushEvents();
13751379
}
13761380

1381+
void TestQgsTaskManager::proxyTask()
1382+
{
1383+
if ( QgsTest::isTravis() )
1384+
QSKIP( "This test is disabled on Travis CI environment" );
1385+
1386+
QgsProxyProgressTask *proxyTask = new QgsProxyProgressTask( QString() );
1387+
1388+
// finalize before task gets a chance to start
1389+
QgsTaskManager manager;
1390+
proxyTask->finalize( false );
1391+
QPointer< QgsTask > p( proxyTask );
1392+
1393+
manager.addTask( proxyTask );
1394+
1395+
// should all be ok, no deadlock...
1396+
while ( p )
1397+
{
1398+
QCoreApplication::processEvents();
1399+
}
1400+
flushEvents();
1401+
}
1402+
1403+
void TestQgsTaskManager::proxyTask2()
1404+
{
1405+
if ( QgsTest::isTravis() )
1406+
QSKIP( "This test is disabled on Travis CI environment" );
1407+
1408+
QgsProxyProgressTask *proxyTask = new QgsProxyProgressTask( QString() );
1409+
1410+
// finalize before task gets a chance to start
1411+
QgsTaskManager manager;
1412+
QPointer< QgsTask > p( proxyTask );
1413+
manager.addTask( proxyTask );
1414+
1415+
// should all be ok, no deadlock...
1416+
while ( proxyTask->status() != QgsTask::Running )
1417+
{
1418+
QCoreApplication::processEvents();
1419+
}
1420+
proxyTask->finalize( false );
1421+
while ( p )
1422+
{
1423+
QCoreApplication::processEvents();
1424+
}
1425+
1426+
flushEvents();
1427+
}
1428+
1429+
void TestQgsTaskManager::scopedProxyTask()
1430+
{
1431+
if ( QgsTest::isTravis() )
1432+
QSKIP( "This test is disabled on Travis CI environment" );
1433+
1434+
{
1435+
// task finishes before it can start
1436+
QgsScopedProxyProgressTask task{ QString() };
1437+
}
1438+
1439+
// should all be ok, no deadlock...
1440+
while ( QgsApplication::taskManager()->countActiveTasks() == 0 )
1441+
{
1442+
QCoreApplication::processEvents();
1443+
}
1444+
while ( QgsApplication::taskManager()->countActiveTasks() > 0 )
1445+
{
1446+
QCoreApplication::processEvents();
1447+
}
1448+
flushEvents();
1449+
}
1450+
13771451
QGSTEST_MAIN( TestQgsTaskManager )
13781452
#include "testqgstaskmanager.moc"

0 commit comments

Comments
 (0)