[vlc-commits] [Git][videolan/vlc][master] qt: use lock file in ModelRecoveryAgent to consider multiple application instances

Steve Lhomme (@robUx4) gitlab at videolan.org
Sun Dec 15 08:34:28 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
0e2d7848 by Fatih Uzunoglu at 2024-12-15T08:11:13+00:00
qt: use lock file in ModelRecoveryAgent to consider multiple application instances

ModelRecoveryAgent helps recovering a model if the application closes abruptly,
this can be a simple crash or power loss. Currently it is only used for the main
playlist.

It works well for that use case, but if a new instance of the application starts
while the old instance still runs, it considers the previous instance to be closed
abruptly. This is a known limitation, and now it should be corrected with this
change.

Note that currently we do not support recovering multiple models of same type. This
means that the instance holding the lock manages recovery, and if you have multiple
instances open with different models, only the lock holder instance's model would
be saved.

There is currently no plan to support recovering multiple models of the same type.
It is questionable if that would be useful in a media player, although it really
makes sense with text editors to not lose data.

- - - - -


1 changed file:

- modules/gui/qt/util/model_recovery_agent.hpp


Changes:

=====================================
modules/gui/qt/util/model_recovery_agent.hpp
=====================================
@@ -27,6 +27,7 @@
 #include <QMessageBox>
 #include <QTemporaryFile>
 #include <QAbstractItemModel>
+#include <QLockFile>
 
 #include <cstdio>
 
@@ -39,6 +40,7 @@ class ModelRecoveryAgent
     QString m_recoveryFileName;
     QTimer m_timer;
     bool m_conditionDismissInitialDirtiness = false;
+    std::unique_ptr<QLockFile> m_lockFile;
 
 public:
     // NOTE: settings and model must outlive the instance of this class.
@@ -56,6 +58,12 @@ public:
             if (!recoveryFileName.isEmpty())
             {
                 m_recoveryFileName = std::move(recoveryFileName);
+                {
+                    m_lockFile = std::make_unique<QLockFile>(m_recoveryFileName + QStringLiteral(".lock"));
+                    m_lockFile->setStaleLockTime(0); // if the process crashed, QLockFile considers the lock stale regardless of the lock time
+                    if (!m_lockFile->tryLock()) // if the older instance is still alive, it would have the lock
+                        throw std::exception(); // Older instance is managing the recovery, don't take over. We don't support recovering multiple models at the moment.
+                }
                 const QFileInfo fileInfo(m_recoveryFileName);
                 if (fileInfo.size() > 0)
                 {
@@ -81,6 +89,14 @@ public:
             if (!temporaryFile.open())
                 throw std::exception();
             m_recoveryFileName = temporaryFile.fileName();
+            {
+                assert(!m_lockFile);
+                m_lockFile = std::make_unique<QLockFile>(m_recoveryFileName + QStringLiteral(".lock"));
+                m_lockFile->setStaleLockTime(0); // if the process crashed, QLockFile considers the lock stale regardless of the lock time
+                assert(!m_lockFile->isLocked()); // the file name is new here, it can not be locked before
+                if (!m_lockFile->tryLock())
+                    throw std::exception();
+            }
             settings->setValue(m_key, m_recoveryFileName);
             settings->sync();
         }



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/0e2d7848bb772a9c4983917556ea07a1cd7598ef

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/0e2d7848bb772a9c4983917556ea07a1cd7598ef
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list