Skip to content

Commit 680d5d2

Browse files
committed
Disconnect any leftover connections when destroying auth manager
If one of these connections is triggered after destruction of auth manager, bad things happen because the slot tries to access the mutex which has gone for good along with the auth manager itself.
1 parent 854a904 commit 680d5d2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/core/auth/qgsauthmanager.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@ QSqlDatabase QgsAuthManager::authDatabaseConnection() const
140140
// triggers a condition in QSqlDatabase which detects the nullptr private thread data and returns an invalid database instead.
141141
// QSqlDatabase::removeDatabase is thread safe, so this is ok to do.
142142
// Right about now is a good time to re-evaluate your selected career ;)
143-
connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName, this ]
143+
QMetaObject::Connection connection = connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName, this ]
144144
{
145145
QMutexLocker locker( mMutex );
146-
QgsDebugMsgLevel( QStringLiteral( "Removing outdated connection to %1 on thread exit" ).arg( connectionName ), 2 );
147146
QSqlDatabase::removeDatabase( connectionName );
147+
mConnectedThreads.remove( QThread::currentThread() );
148148
}, Qt::DirectConnection );
149+
150+
mConnectedThreads.insert( QThread::currentThread(), connection );
149151
}
150152
}
151153
else
@@ -2961,6 +2963,15 @@ void QgsAuthManager::tryToStartDbErase()
29612963

29622964
QgsAuthManager::~QgsAuthManager()
29632965
{
2966+
QMutexLocker locker( mMutex );
2967+
QMapIterator<QThread *, QMetaObject::Connection> iterator( mConnectedThreads );
2968+
while ( iterator.hasNext() )
2969+
{
2970+
iterator.next();
2971+
iterator.key()->disconnect( iterator.value() );
2972+
}
2973+
locker.unlock();
2974+
29642975
if ( !isDisabled() )
29652976
{
29662977
delete QgsAuthMethodRegistry::instance();

src/core/auth/qgsauthmanager.h

+2
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@ class CORE_EXPORT QgsAuthManager : public QObject
900900
//! password helper folder in the wallets
901901
static const QLatin1String AUTH_PASSWORD_HELPER_FOLDER_NAME;
902902

903+
mutable QMap<QThread *, QMetaObject::Connection> mConnectedThreads;
904+
903905
friend class QgsApplication;
904906

905907
};

0 commit comments

Comments
 (0)