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 309b1c8 commit 505e365
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
16 changes: 15 additions & 1 deletion src/providers/wfs/qgswfsfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,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
14 changes: 8 additions & 6 deletions src/providers/wfs/qgswfsfeatureiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <QProgressDialog>
#include <QPushButton>
#include <QMutex>
#include <QWaitCondition>

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

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

signals:
/** Emitted when the thread is ready */
void ready();
//! Starts thread and wait for it to be started
void startAndWait();

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

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

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

0 comments on commit 505e365

Please sign in to comment.