Skip to content

Commit

Permalink
Merge pull request #9336 from m-kuhn/fix-auth-manager-crash-on-exit-r…
Browse files Browse the repository at this point in the history
…elease-3_6

Disconnect any leftover connections when destroying auth manager
  • Loading branch information
m-kuhn committed Mar 4, 2019
2 parents 7302df8 + 3988a93 commit 8aa64cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/core/auth/qgsauthmanager.cpp
Expand Up @@ -140,12 +140,14 @@ QSqlDatabase QgsAuthManager::authDatabaseConnection() const
// triggers a condition in QSqlDatabase which detects the nullptr private thread data and returns an invalid database instead.
// QSqlDatabase::removeDatabase is thread safe, so this is ok to do.
// Right about now is a good time to re-evaluate your selected career ;)
connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName, this ]
QMetaObject::Connection connection = connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName, this ]
{
QMutexLocker locker( mMutex );
QgsDebugMsgLevel( QStringLiteral( "Removing outdated connection to %1 on thread exit" ).arg( connectionName ), 2 );
QSqlDatabase::removeDatabase( connectionName );
mConnectedThreads.remove( QThread::currentThread() );
}, Qt::DirectConnection );

mConnectedThreads.insert( QThread::currentThread(), connection );
}
}
else
Expand Down Expand Up @@ -2961,6 +2963,15 @@ void QgsAuthManager::tryToStartDbErase()

QgsAuthManager::~QgsAuthManager()
{
QMutexLocker locker( mMutex );
QMapIterator<QThread *, QMetaObject::Connection> iterator( mConnectedThreads );
while ( iterator.hasNext() )
{
iterator.next();
iterator.key()->disconnect( iterator.value() );
}
locker.unlock();

if ( !isDisabled() )
{
delete QgsAuthMethodRegistry::instance();
Expand Down
2 changes: 2 additions & 0 deletions src/core/auth/qgsauthmanager.h
Expand Up @@ -900,6 +900,8 @@ class CORE_EXPORT QgsAuthManager : public QObject
//! password helper folder in the wallets
static const QLatin1String AUTH_PASSWORD_HELPER_FOLDER_NAME;

mutable QMap<QThread *, QMetaObject::Connection> mConnectedThreads;

friend class QgsApplication;

};
Expand Down

0 comments on commit 8aa64cf

Please sign in to comment.