Skip to content

Commit 410d855

Browse files
committed
Fix some authentication interaction threading issues
1 parent 44e8f60 commit 410d855

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/providers/wfs/qgswfsrequest.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,13 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
128128
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
129129
}
130130

131-
QWaitCondition waitCondition;
131+
QWaitCondition mainThreadWaitCondition;
132+
QMutex mainThreadMutex;
132133

133-
QMutex mutex;
134-
std::function<bool()> downloaderFunction = [ this, request, synchronous, &waitCondition ]()
134+
QWaitCondition downloaderThreadWaitCondition;
135+
QMutex downloaderThreadMutex;
136+
137+
std::function<bool()> downloaderFunction = [ this, request, synchronous, &mainThreadMutex, &mainThreadWaitCondition, &downloaderThreadMutex, &downloaderThreadWaitCondition ]()
135138
{
136139
if ( QThread::currentThread() != QgsApplication::instance()->thread() )
137140
QgsNetworkAccessManager::instance( Qt::DirectConnection );
@@ -145,26 +148,36 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
145148
mErrorCode = QgsWfsRequest::NetworkError;
146149
mErrorMessage = errorMessageFailedAuth();
147150
QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) );
148-
waitCondition.wakeAll();
151+
mainThreadWaitCondition.wakeAll();
149152
success = false;
150153
}
151154
else
152155
{
153156
// We are able to use direct connection here, because we
154-
// * either run on the thread mReply lives in, so DirectConnection is standard and safe anyway (if it is not the main thread)
157+
// * either run on the thread mReply lives in, so DirectConnection is standard and safe anyway
155158
// * or the owner thread of mReply is currently not doing anything because it's blocked in future.waitForFinished() (if it is the main thread)
156159
connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished, Qt::DirectConnection );
157160
connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress, Qt::DirectConnection );
158-
connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::authenticationRequired, this, [&waitCondition]() { waitCondition.wakeAll(); } );
159161

160162
if ( synchronous )
161163
{
164+
connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::authenticationRequired, this, [&mainThreadMutex, &mainThreadWaitCondition, &downloaderThreadMutex, &downloaderThreadWaitCondition]()
165+
{
166+
mainThreadMutex.lock();
167+
mainThreadWaitCondition.wakeAll();
168+
mainThreadMutex.unlock();
169+
170+
downloaderThreadMutex.lock();
171+
downloaderThreadWaitCondition.wait( &downloaderThreadMutex );
172+
downloaderThreadMutex.unlock();
173+
}, Qt::DirectConnection
174+
);
162175
QEventLoop loop;
163176
connect( this, &QgsWfsRequest::downloadFinished, &loop, &QEventLoop::quit, Qt::DirectConnection );
164177
loop.exec();
165178
}
166179
}
167-
waitCondition.wakeAll();
180+
mainThreadWaitCondition.wakeAll();
168181
return success;
169182
};
170183

@@ -176,9 +189,16 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
176189
downloaderThread->start();
177190
while ( !downloaderThread->isFinished() )
178191
{
179-
waitCondition.wait( &mutex );
192+
mainThreadMutex.lock();
193+
mainThreadWaitCondition.wait( &mainThreadMutex );
194+
mainThreadMutex.unlock();
180195
if ( !downloaderThread->isFinished() )
196+
{
181197
QgsApplication::instance()->processEvents();
198+
downloaderThreadMutex.lock();
199+
downloaderThreadWaitCondition.wakeAll();
200+
downloaderThreadMutex.unlock();
201+
}
182202
}
183203

184204
success = downloaderThread->success();
@@ -187,7 +207,6 @@ bool QgsWfsRequest::sendGET( const QUrl &url, bool synchronous, bool forceRefres
187207
{
188208
success = downloaderFunction();
189209
}
190-
mutex.unlock();
191210
return success && mErrorMessage.isEmpty();
192211
}
193212

0 commit comments

Comments
 (0)