Skip to content

Commit 69dab87

Browse files
committed
[auth] don't share config db connection across threads
(fixes #20054)
1 parent e71e135 commit 69dab87

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/core/auth/qgsauthmanager.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "keychain.h"
4242

4343
// QGIS includes
44+
#include "qgsapplication.h"
4445
#include "qgsauthcertutils.h"
4546
#include "qgsauthcrypto.h"
4647
#include "qgsauthmethod.h"
@@ -111,15 +112,28 @@ QSqlDatabase QgsAuthManager::authDatabaseConnection() const
111112
if ( isDisabled() )
112113
return authdb;
113114

114-
QString connectionname = QStringLiteral( "authentication.configs" );
115-
if ( !QSqlDatabase::contains( connectionname ) )
115+
QString connectionName = QStringLiteral( "authentication.configs" );
116+
// Sharing the same connection between threads is not allowed.
117+
// We use a dedicated connection for each thread requiring access to the database,
118+
// using the thread address as connection name.
119+
const QString threadAddress = QStringLiteral( ":0x%1" ).arg( reinterpret_cast< quintptr >( QThread::currentThreadId() ), 16 );
120+
connectionName += threadAddress;
121+
if ( !QSqlDatabase::contains( connectionName ) )
116122
{
117-
authdb = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), connectionname );
123+
authdb = QSqlDatabase::addDatabase( QStringLiteral( "QSQLITE" ), connectionName );
118124
authdb.setDatabaseName( authenticationDatabasePath() );
125+
// for background threads, remove database when current thread finishes
126+
if ( QThread::currentThread() != QgsApplication::instance()->thread() )
127+
{
128+
connect( QThread::currentThread(), &QThread::finished, QThread::currentThread(), [connectionName]
129+
{
130+
QSqlDatabase::removeDatabase( connectionName );
131+
} );
132+
}
119133
}
120134
else
121135
{
122-
authdb = QSqlDatabase::database( connectionname );
136+
authdb = QSqlDatabase::database( connectionName );
123137
}
124138
if ( !authdb.isOpen() )
125139
{

0 commit comments

Comments
 (0)