Skip to content

Commit

Permalink
Prevent full application freeze when writing keychainchunks to keycha…
Browse files Browse the repository at this point in the history
…in on certain systems (e.g. macOS)

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
  • Loading branch information
claucambra committed Feb 7, 2023
1 parent 5aa8002 commit 6f72ce0
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/libsync/creds/keychainchunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* for more details.
*/

#include "QtConcurrent/qtconcurrentrun.h"
#include "account.h"
#include "keychainchunk.h"
#include "theme.h"
Expand Down Expand Up @@ -212,16 +213,21 @@ void WriteJob::slotWriteJobDone(QKeychain::Job *incomingJob)
_account->id()
) : keyWithIndex;

auto job = new QKeychain::WritePasswordJob(_serviceName, this);
#if defined(KEYCHAINCHUNK_ENABLE_INSECURE_FALLBACK)
addSettingsToJob(_account, job);
#endif
job->setInsecureFallback(_insecureFallback);
connect(job, &QKeychain::Job::finished, this, &KeychainChunk::WriteJob::slotWriteJobDone);
// only add the key's (sub)"index" after the first element, to stay compatible with older versions and non-Windows
job->setKey(kck);
job->setBinaryData(chunk);
job->start();
// On certain OSes (e.g. macOS) writing to the keychain blocks the main thread.
// Since QtKeychain does not (yet) run this operation on a separate thread,
// we run this with QtConcurrent to prevent a full application freeze.
QtConcurrent::run([kck, chunk, this] {
auto job = new QKeychain::WritePasswordJob(_serviceName, this);
#if defined(KEYCHAINCHUNK_ENABLE_INSECURE_FALLBACK)
addSettingsToJob(_account, job);
#endif
job->setInsecureFallback(_insecureFallback);
connect(job, &QKeychain::Job::finished, this, &KeychainChunk::WriteJob::slotWriteJobDone);
// only add the key's (sub)"index" after the first element, to stay compatible with older versions and non-Windows
job->setKey(kck);
job->setBinaryData(chunk);
job->start();
});

chunk.clear();
} else {
Expand Down

0 comments on commit 6f72ce0

Please sign in to comment.