Skip to content

Commit

Permalink
[WFS provider] Fix deadlock when adding WFS layer to project from Pyt…
Browse files Browse the repository at this point in the history
…hon console (fixes #17087)
  • Loading branch information
rouault committed Nov 9, 2017
1 parent 032ca68 commit 75c41f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/providers/wfs/qgswfsfeatureiterator.cpp
Expand Up @@ -755,11 +755,25 @@ void QgsWFSThreadedFeatureDownloader::stop()
}
}

void QgsWFSThreadedFeatureDownloader::startAndWait()
{
start();

QMutexLocker locker( &mWaitMutex );
while ( !mDownloader )
{
mWaitCond.wait( &mWaitMutex );
}
}

void QgsWFSThreadedFeatureDownloader::run()
{
// We need to construct it in the run() method (i.e. in the new thread)
mDownloader = new QgsWFSFeatureDownloader( mShared );
emit ready();
{
QMutexLocker locker( &mWaitMutex );
mWaitCond.wakeOne();
}
mDownloader->run( true, /* serialize features */
0 /* user max features */ );
}
Expand Down
11 changes: 7 additions & 4 deletions src/providers/wfs/qgswfsfeatureiterator.h
Expand Up @@ -25,6 +25,8 @@
#include <memory>
#include <QProgressDialog>
#include <QPushButton>
#include <QMutex>
#include <QWaitCondition>

class QgsWFSProvider;
class QgsWFSSharedData;
Expand Down Expand Up @@ -168,20 +170,21 @@ class QgsWFSThreadedFeatureDownloader: public QThread
//! Return downloader object
QgsWFSFeatureDownloader *downloader() { return mDownloader; }

//! Starts thread and wait for it to be started
void startAndWait();

//! Stops (synchronously) the download
void stop();

signals:
//! Emitted when the thread is ready
void ready();

protected:
//! Inherited from QThread. Starts the download
void run() override;

private:
QgsWFSSharedData *mShared; //!< Mutable data shared between provider and feature sources
QgsWFSFeatureDownloader *mDownloader = nullptr;
QWaitCondition mWaitCond;
QMutex mWaitMutex;
};

class QgsWFSFeatureSource;
Expand Down
5 changes: 1 addition & 4 deletions src/providers/wfs/qgswfsshareddata.cpp
Expand Up @@ -540,10 +540,7 @@ int QgsWFSSharedData::registerToCache( QgsWFSFeatureIterator *iterator, const Qg
mDownloadFinished = false;
mComputedExtent = QgsRectangle();
mDownloader = new QgsWFSThreadedFeatureDownloader( this );
QEventLoop loop;
connect( mDownloader, &QgsWFSThreadedFeatureDownloader::ready, &loop, &QEventLoop::quit );
mDownloader->start();
loop.exec( QEventLoop::ExcludeUserInputEvents );
mDownloader->startAndWait();
}
if ( mDownloadFinished )
return -1;
Expand Down

0 comments on commit 75c41f5

Please sign in to comment.