Merge pull request #17609 from wwmayer/issue_17576
Core: Don't freeze application if lock file already exists
This commit is contained in:
@@ -47,6 +47,7 @@
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QProcessEnvironment>
|
||||
@@ -1114,6 +1115,11 @@ Application::TransactionSignaller::~TransactionSignaller() {
|
||||
}
|
||||
}
|
||||
|
||||
int64_t Application::applicationPid()
|
||||
{
|
||||
return QCoreApplication::applicationPid();
|
||||
}
|
||||
|
||||
std::string Application::getHomePath()
|
||||
{
|
||||
return mConfig["AppHomePath"];
|
||||
|
||||
@@ -399,6 +399,7 @@ public:
|
||||
static std::map<std::string, std::string> &Config(){return mConfig;}
|
||||
static int GetARGC(){return _argc;}
|
||||
static char** GetARGV(){return _argv;}
|
||||
static int64_t applicationPid();
|
||||
//@}
|
||||
|
||||
/** @name Application directories */
|
||||
|
||||
@@ -931,18 +931,21 @@ Document::~Document()
|
||||
std::string Document::getTransientDirectoryName(const std::string& uuid, const std::string& filename) const
|
||||
{
|
||||
// Create a directory name of the form: {ExeName}_Doc_{UUID}_{HASH}_{PID}
|
||||
std::stringstream s;
|
||||
std::stringstream out;
|
||||
QCryptographicHash hash(QCryptographicHash::Sha1);
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6,3,0)
|
||||
hash.addData(filename.c_str(), filename.size());
|
||||
#else
|
||||
hash.addData(QByteArrayView(filename.c_str(), filename.size()));
|
||||
#endif
|
||||
s << App::Application::getUserCachePath() << App::Application::getExecutableName()
|
||||
<< "_Doc_" << uuid
|
||||
<< "_" << hash.result().toHex().left(6).constData()
|
||||
<< "_" << QCoreApplication::applicationPid();
|
||||
return s.str();
|
||||
out << App::Application::getUserCachePath() << App::Application::getExecutableName()
|
||||
<< "_Doc_"
|
||||
<< uuid
|
||||
<< "_"
|
||||
<< hash.result().toHex().left(6).constData()
|
||||
<< "_"
|
||||
<< App::Application::applicationPid();
|
||||
return out.str();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
@@ -2172,46 +2172,51 @@ void setAppNameAndIcon()
|
||||
|
||||
void tryRunEventLoop(GUISingleApplication& mainApp)
|
||||
{
|
||||
std::stringstream s;
|
||||
s << App::Application::getUserCachePath() << App::Application::getExecutableName() << "_"
|
||||
<< QCoreApplication::applicationPid() << ".lock";
|
||||
std::stringstream out;
|
||||
out << App::Application::getUserCachePath()
|
||||
<< App::Application::getExecutableName()
|
||||
<< "_"
|
||||
<< App::Application::applicationPid()
|
||||
<< ".lock";
|
||||
|
||||
// open a lock file with the PID
|
||||
Base::FileInfo fi(s.str());
|
||||
Base::FileInfo fi(out.str());
|
||||
Base::ofstream lock(fi);
|
||||
|
||||
// In case the file_lock cannot be created start FreeCAD without IPC support.
|
||||
#if !defined(FC_OS_WIN32) || (BOOST_VERSION < 107600)
|
||||
std::string filename = s.str();
|
||||
std::string filename = out.str();
|
||||
#else
|
||||
std::wstring filename = fi.toStdWString();
|
||||
#endif
|
||||
std::unique_ptr<boost::interprocess::file_lock> flock;
|
||||
try {
|
||||
flock = std::make_unique<boost::interprocess::file_lock>(filename.c_str());
|
||||
flock->lock();
|
||||
boost::interprocess::file_lock flock(filename.c_str());
|
||||
if (flock.try_lock()) {
|
||||
Base::Console().Log("Init: Executing event loop...\n");
|
||||
QApplication::exec();
|
||||
|
||||
// Qt can't handle exceptions thrown from event handlers, so we need
|
||||
// to manually rethrow SystemExitExceptions.
|
||||
if (mainApp.caughtException) {
|
||||
throw Base::SystemExitException(*mainApp.caughtException.get());
|
||||
}
|
||||
|
||||
// close the lock file, in case of a crash we can see the existing lock file
|
||||
// on the next restart and try to repair the documents, if needed.
|
||||
flock.unlock();
|
||||
lock.close();
|
||||
fi.deleteFile();
|
||||
}
|
||||
else {
|
||||
Base::Console().Warning("Failed to create a file lock for the IPC.\n"
|
||||
"The application will be terminated\n");
|
||||
}
|
||||
}
|
||||
catch (const boost::interprocess::interprocess_exception& e) {
|
||||
QString msg = QString::fromLocal8Bit(e.what());
|
||||
Base::Console().Warning("Failed to create a file lock for the IPC: %s\n",
|
||||
msg.toUtf8().constData());
|
||||
}
|
||||
|
||||
Base::Console().Log("Init: Executing event loop...\n");
|
||||
QApplication::exec();
|
||||
|
||||
// Qt can't handle exceptions thrown from event handlers, so we need
|
||||
// to manually rethrow SystemExitExceptions.
|
||||
if (mainApp.caughtException) {
|
||||
throw Base::SystemExitException(*mainApp.caughtException.get());
|
||||
}
|
||||
|
||||
// close the lock file, in case of a crash we can see the existing lock file
|
||||
// on the next restart and try to repair the documents, if needed.
|
||||
if (flock) {
|
||||
flock->unlock();
|
||||
}
|
||||
lock.close();
|
||||
fi.deleteFile();
|
||||
}
|
||||
|
||||
void runEventLoop(GUISingleApplication& mainApp)
|
||||
|
||||
@@ -651,12 +651,12 @@ void DocumentRecoveryHandler::checkForPreviousCrashes(const std::function<void(Q
|
||||
|
||||
QString exeName = QString::fromStdString(App::Application::getExecutableName());
|
||||
QList<QFileInfo> locks = tmp.entryInfoList();
|
||||
for (QList<QFileInfo>::iterator it = locks.begin(); it != locks.end(); ++it) {
|
||||
QString bn = it->baseName();
|
||||
for (const QFileInfo& it : locks) {
|
||||
QString bn = it.baseName();
|
||||
// ignore the lock file for this instance
|
||||
QString pid = QString::number(QCoreApplication::applicationPid());
|
||||
QString pid = QString::number(App::Application::applicationPid());
|
||||
if (bn.startsWith(exeName) && bn.indexOf(pid) < 0) {
|
||||
QString fn = it->absoluteFilePath();
|
||||
QString fn = it.absoluteFilePath();
|
||||
|
||||
#if !defined(FC_OS_WIN32) || (BOOST_VERSION < 107600)
|
||||
boost::interprocess::file_lock flock(fn.toUtf8());
|
||||
@@ -665,7 +665,7 @@ void DocumentRecoveryHandler::checkForPreviousCrashes(const std::function<void(Q
|
||||
#endif
|
||||
if (flock.try_lock()) {
|
||||
// OK, this file is a leftover from a previous crash
|
||||
QString crashed_pid = bn.mid(exeName.length()+1);
|
||||
QString crashed_pid = bn.mid(exeName.length() + 1);
|
||||
// search for transient directories with this PID
|
||||
QString filter;
|
||||
QTextStream str(&filter);
|
||||
@@ -674,7 +674,10 @@ void DocumentRecoveryHandler::checkForPreviousCrashes(const std::function<void(Q
|
||||
tmp.setFilter(QDir::Dirs);
|
||||
QList<QFileInfo> dirs = tmp.entryInfoList();
|
||||
|
||||
callableFunc(tmp, dirs, it->fileName());
|
||||
callableFunc(tmp, dirs, it.fileName());
|
||||
}
|
||||
else {
|
||||
Base::Console().Log("Failed to lock file %s\n", fn.toUtf8().constData());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user