mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-02-10 19:52:24 +00:00
linux: fix single-instance logic (#314)
Replaced the previous QSharedMemory-based single-instance logic with a clean QLocalServer approach. The old method left stale shared memory and socket files after crashes, causing false "already running" states and occasional segmentation faults.
This commit is contained in:
@@ -987,30 +987,24 @@ private:
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
QSharedMemory sharedMemory;
|
QLocalServer::removeServer("app_server");
|
||||||
sharedMemory.setKey("TcpServer-Key2");
|
|
||||||
|
|
||||||
// Check if app is already open
|
QFile stale("/tmp/app_server");
|
||||||
if(sharedMemory.create(1) == false)
|
if (stale.exists())
|
||||||
{
|
stale.remove();
|
||||||
LOG_INFO("Another instance already running! Opening App Window Instead");
|
|
||||||
QLocalSocket socket;
|
QLocalSocket socket_check;
|
||||||
// Connect to the original app, then trigger the reopen signal
|
socket_check.connectToServer("app_server");
|
||||||
socket.connectToServer("app_server");
|
|
||||||
if (socket.waitForConnected(500)) {
|
if (socket_check.waitForConnected(300)) {
|
||||||
socket.write("reopen");
|
LOG_INFO("Another instance already running! Reopening window...");
|
||||||
socket.flush();
|
|
||||||
socket.waitForBytesWritten(500);
|
socket_check.write("reopen");
|
||||||
socket.disconnectFromServer();
|
socket_check.flush();
|
||||||
app.exit(); // exit; process already running
|
socket_check.waitForBytesWritten(200);
|
||||||
return 0;
|
socket_check.disconnectFromServer();
|
||||||
}
|
|
||||||
else
|
return 0;
|
||||||
{
|
|
||||||
// Failed connection, log and open the app (assume it's not running)
|
|
||||||
LOG_ERROR("Failed to connect to the original app instance. Assuming it is not running.");
|
|
||||||
LOG_DEBUG("Socket error: " << socket.errorString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
app.setDesktopFileName("me.kavishdevar.librepods");
|
app.setDesktopFileName("me.kavishdevar.librepods");
|
||||||
app.setQuitOnLastWindowClosed(false);
|
app.setQuitOnLastWindowClosed(false);
|
||||||
@@ -1092,8 +1086,16 @@ int main(int argc, char *argv[]) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() {
|
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() {
|
||||||
LOG_DEBUG("Application is about to quit. Cleaning up...");
|
LOG_DEBUG("Application quitting. Cleaning up local server...");
|
||||||
sharedMemory.detach();
|
|
||||||
|
if (server.isListening()) {
|
||||||
|
server.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
QLocalServer::removeServer("app_server");
|
||||||
|
QFile stale("/tmp/app_server");
|
||||||
|
if (stale.exists())
|
||||||
|
stale.remove();
|
||||||
});
|
});
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user