From b2046ceebf85c505c65ec13559e02f3bdf114700 Mon Sep 17 00:00:00 2001 From: Hannah von Reth Date: Fri, 10 Nov 2023 11:41:44 +0100 Subject: [PATCH] Deleting a running QtKeychain job can cause a crash Fixes: #11358 --- src/libsync/creds/credentialmanager.cpp | 26 +++++++++---------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/libsync/creds/credentialmanager.cpp b/src/libsync/creds/credentialmanager.cpp index eef7abbb05b..52ecb13589b 100644 --- a/src/libsync/creds/credentialmanager.cpp +++ b/src/libsync/creds/credentialmanager.cpp @@ -5,6 +5,7 @@ #include "theme.h" #include "common/asserts.h" +#include "common/chronoelapsedtimer.h" #include #include @@ -71,26 +72,17 @@ QKeychain::Job *CredentialManager::set(const QString &key, const QVariant &data) auto timer = new QTimer(writeJob); timer->setInterval(tiemoutC); - timer->setSingleShot(true); - auto timedOut = std::make_unique(false); - connect(timer, &QTimer::timeout, writeJob, [writeJob, timedOut = timedOut.get()] { - *timedOut = true; - Q_EMIT writeJob->finished(writeJob); - writeJob->deleteLater(); - }); - connect(writeJob, &QKeychain::WritePasswordJob::finished, this, [writeJob, timer, key, timedOut = std::move(timedOut), this] { - timer->stop(); + Utility::ChronoElapsedTimer elapsedTimer; + connect(timer, &QTimer::timeout, writeJob, + [writeJob, elapsedTimer] { qCWarning(lcCredentialsManager) << "set" << writeJob->key() << "has not yet finished." << elapsedTimer.duration(); }); + connect(writeJob, &QKeychain::WritePasswordJob::finished, this, [writeJob, key, elapsedTimer, this] { if (writeJob->error() == QKeychain::NoError) { - if (*timedOut.get()) { - qCInfo(lcCredentialsManager) << "set" << writeJob->key() << "timed out"; - } else { - qCInfo(lcCredentialsManager) << "added" << writeJob->key(); - // just a list, the values don't matter - credentialsList().setValue(key, true); - } + qCInfo(lcCredentialsManager) << "added" << writeJob->key() << "after" << elapsedTimer.duration(); + // just a list, the values don't matter + credentialsList().setValue(key, true); } else { - qCWarning(lcCredentialsManager) << "Failed to set:" << writeJob->key() << writeJob->errorString(); + qCWarning(lcCredentialsManager) << "Failed to set:" << writeJob->key() << writeJob->errorString() << "after" << elapsedTimer.duration(); } }); writeJob->setBinaryData(QCborValue::fromVariant(data).toCbor());