kdecore: block only when another process is holding the lock in KLockFile::lock()

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-08 14:12:41 +03:00
parent 2e9adc69c4
commit 0812a98d19
2 changed files with 6 additions and 5 deletions

View file

@ -594,8 +594,10 @@ bool KConfigIniBackend::lock(const KComponentData& componentData)
m_lockfile = new KLockFile(filePath());
}
if (m_lockfile->lock() == KLockFile::LockStale) // attempt to break the lock
if (m_lockfile->lock() == KLockFile::LockStale) {
// attempt to break the lock
m_lockfile->lock(KLockFile::ForceFlag);
}
return m_lockfile->isLocked();
}

View file

@ -68,12 +68,12 @@ KLockFile::LockResult KLockFilePrivate::tryLock()
QFile infofile(QFile::decodeName(m_lockfile));
if (Q_UNLIKELY(!infofile.open(QFile::ReadOnly))) {
kWarning() << infofile.errorString();
return KLockFile::LockError;
return KLockFile::LockFail;
}
const QList<QByteArray> lockinfo = infofile.readAll().split('\t');
if (Q_UNLIKELY(lockinfo.size() != 2)) {
kWarning() << "Invalid lock information";
return KLockFile::LockError;
return KLockFile::LockFail;
}
const qint64 lockpid = lockinfo.at(0).toLongLong();
const QByteArray lockhost = lockinfo.at(1);
@ -104,7 +104,6 @@ KLockFile::LockResult KLockFilePrivate::tryLock()
}
KLockFile::KLockFile(const QString &file)
: d(new KLockFilePrivate())
{
@ -129,7 +128,7 @@ tryagain:
}
result = d->tryLock();
}
if (!(options & KLockFile::NoBlockFlag) && result != KLockFile::LockOK) {
if (!(options & KLockFile::NoBlockFlag) && result == KLockFile::LockFail) {
const int randomtimeout = KRandom::randomMax(KLOCKFILE_TIMEOUT) + KLOCKFILE_TIMEOUT;
kDebug() << "Retrying to lock after" << (randomtimeout + KLOCKFILE_SLEEPTIME);
QCoreApplication::processEvents(QEventLoop::AllEvents, randomtimeout);