From cda6b2a728e44a781b557ede9ea2d0ef6e919f7e Mon Sep 17 00:00:00 2001 From: Romain LE JEUNE Date: Tue, 11 Jul 2023 09:03:45 +0200 Subject: [PATCH] fix(GODT-2781): try to remove stale lock file before failing in checkSingleInstance. --- internal/frontend/bridge-gui/bridge-gui/main.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/frontend/bridge-gui/bridge-gui/main.cpp b/internal/frontend/bridge-gui/bridge-gui/main.cpp index d28851c7..dc8eca14 100644 --- a/internal/frontend/bridge-gui/bridge-gui/main.cpp +++ b/internal/frontend/bridge-gui/bridge-gui/main.cpp @@ -137,12 +137,21 @@ bool checkSingleInstance(QLockFile &lock) { if (lock.getLockInfo(&pid, &hostname, &appName)) { details = QString("(PID : %1 - Host : %2 - App : %3)").arg(pid).arg(hostname, appName); } - + if (lock.error() == QLockFile::LockFailedError) { + // This happens if a stale lock file exists and another process uses that PID. + // Try removing the stale file, which will fail if a real process is holding a + // file-level lock. A false error is more problematic than not locking properly + // on corner-case systems. + if (lock.removeStaleLockFile() && lock.tryLock()) { + app().log().info("Removed stale lock file"); + app().log().info(QString("lock file created %1").arg(lock.fileName())); + return true; + } + } app().log().error(QString("Instance already exists %1 %2").arg(lock.fileName(), details)); return false; - } else { - app().log().info(QString("lock file created %1").arg(lock.fileName())); } + app().log().info(QString("lock file created %1").arg(lock.fileName())); return true; }