From e1de76d763c2288ded37e7fb1aefc996f21d1b70 Mon Sep 17 00:00:00 2001 From: Borys Jurgiel Date: Thu, 7 Nov 2024 17:35:43 +0100 Subject: [PATCH] Make plugin watchdog considering timestamp of last run. Fixes #59370 --- python/pyplugin_installer/installer.py | 5 ++-- src/app/qgspluginregistry.cpp | 37 ++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/python/pyplugin_installer/installer.py b/python/pyplugin_installer/installer.py index d58db50e0ce..2b25185d05c 100644 --- a/python/pyplugin_installer/installer.py +++ b/python/pyplugin_installer/installer.py @@ -29,7 +29,7 @@ import zipfile from functools import partial from qgis.PyQt import sip -from qgis.PyQt.QtCore import Qt, QObject, QDir, QUrl, QFileInfo, QFile +from qgis.PyQt.QtCore import Qt, QObject, QDateTime, QDir, QUrl, QFileInfo, QFile from qgis.PyQt.QtWidgets import ( QApplication, QDialog, @@ -118,7 +118,8 @@ class QgsPluginInstaller(QObject): self.uninstallPlugin(key, quiet=True) updateAvailablePlugins() if plugin_is_active: - settings.setValue("/PythonPlugins/watchDog/" + key, True) + settings.setValue("/PythonPlugins/watchDog/" + key, + QDateTime.currentDateTime().toSecsSinceEpoch()) loadPlugin(key) startPlugin(key) settings.remove("/PythonPlugins/watchDog/" + key) diff --git a/src/app/qgspluginregistry.cpp b/src/app/qgspluginregistry.cpp index 94c38b90aa2..f3c735dbeb5 100644 --- a/src/app/qgspluginregistry.cpp +++ b/src/app/qgspluginregistry.cpp @@ -505,10 +505,23 @@ void QgsPluginRegistry::restoreSessionPlugins( const QString &pluginDirString ) const QString myFullPath = pluginDirString + '/' + myPluginDir[i]; if ( checkCppPlugin( myFullPath ) ) { - // check if the plugin was active on last session + // check if there is a watchdog timestamp left after last session + bool pluginCrashedPreviously = false; const QString baseName = QFileInfo( myFullPath ).baseName(); - if ( mySettings.value( QStringLiteral( "Plugins/watchDog/%1" ).arg( baseName ) ).isValid() ) + const QVariant lastRun = mySettings.value( QStringLiteral( "Plugins/watchDog/%1" ).arg( baseName ) ); + if ( lastRun.isValid() ) + { + const int delta = QDateTime::currentDateTime().toSecsSinceEpoch() - lastRun.toInt(); + if ( delta > 5 ) + { + // The timestamp is left unremoved and is older than 5 seconds, so it doesn't come + // from a parallelly running instance. + pluginCrashedPreviously = true; + } + } + + if ( pluginCrashedPreviously ) { QToolButton *btnEnablePlugin = new QToolButton(); btnEnablePlugin ->setText( QObject::tr( "Enable Plugin" ) ); @@ -548,7 +561,8 @@ void QgsPluginRegistry::restoreSessionPlugins( const QString &pluginDirString ) } if ( mySettings.value( "/Plugins/" + baseName ).toBool() ) { - mySettings.setValue( QStringLiteral( "Plugins/watchDog/%1" ).arg( baseName ), true ); + mySettings.setValue( QStringLiteral( "Plugins/watchDog/%1" ).arg( baseName ), + QDateTime::currentDateTime().toSecsSinceEpoch() ); loadCppPlugin( myFullPath ); mySettings.remove( QStringLiteral( "/Plugins/watchDog/%1" ).arg( baseName ) ); } @@ -597,8 +611,20 @@ void QgsPluginRegistry::restoreSessionPlugins( const QString &pluginDirString ) } // end - temporary fix for issue #5879, more below + bool pluginCrashedPreviously = false; + const QVariant lastRun = mySettings.value( QStringLiteral( "/PythonPlugins/watchDog/%1" ).arg( packageName ) ); + if ( lastRun.isValid() ) + { + const int delta = QDateTime::currentDateTime().toSecsSinceEpoch() - lastRun.toInt(); + if ( delta > 5 ) + { + // The timestamp is left unremoved and is older than 5 seconds, so it doesn't come + // from a parallelly running instance. + pluginCrashedPreviously = true; + } + } - if ( mySettings.value( "/PythonPlugins/watchDog/" + packageName ).isValid() ) + if ( pluginCrashedPreviously ) { QToolButton *btnEnablePlugin = new QToolButton(); btnEnablePlugin->setText( QObject::tr( "Enable Plugin" ) ); @@ -645,7 +671,8 @@ void QgsPluginRegistry::restoreSessionPlugins( const QString &pluginDirString ) // check if the plugin was active on last session if ( mySettings.value( "/PythonPlugins/" + packageName ).toBool() ) { - mySettings.setValue( "/PythonPlugins/watchDog/" + packageName, true ); + mySettings.setValue( "/PythonPlugins/watchDog/" + packageName, + QDateTime::currentDateTime().toSecsSinceEpoch() ); if ( checkPythonPlugin( packageName ) ) { loadPythonPlugin( packageName );