mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-01-28 22:01:50 +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[]) {
|
||||
QApplication app(argc, argv);
|
||||
|
||||
QSharedMemory sharedMemory;
|
||||
sharedMemory.setKey("TcpServer-Key2");
|
||||
QLocalServer::removeServer("app_server");
|
||||
|
||||
// Check if app is already open
|
||||
if(sharedMemory.create(1) == false)
|
||||
{
|
||||
LOG_INFO("Another instance already running! Opening App Window Instead");
|
||||
QLocalSocket socket;
|
||||
// Connect to the original app, then trigger the reopen signal
|
||||
socket.connectToServer("app_server");
|
||||
if (socket.waitForConnected(500)) {
|
||||
socket.write("reopen");
|
||||
socket.flush();
|
||||
socket.waitForBytesWritten(500);
|
||||
socket.disconnectFromServer();
|
||||
app.exit(); // exit; process already running
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 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());
|
||||
}
|
||||
QFile stale("/tmp/app_server");
|
||||
if (stale.exists())
|
||||
stale.remove();
|
||||
|
||||
QLocalSocket socket_check;
|
||||
socket_check.connectToServer("app_server");
|
||||
|
||||
if (socket_check.waitForConnected(300)) {
|
||||
LOG_INFO("Another instance already running! Reopening window...");
|
||||
|
||||
socket_check.write("reopen");
|
||||
socket_check.flush();
|
||||
socket_check.waitForBytesWritten(200);
|
||||
socket_check.disconnectFromServer();
|
||||
|
||||
return 0;
|
||||
}
|
||||
app.setDesktopFileName("me.kavishdevar.librepods");
|
||||
app.setQuitOnLastWindowClosed(false);
|
||||
@@ -1092,8 +1086,16 @@ int main(int argc, char *argv[]) {
|
||||
});
|
||||
|
||||
QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() {
|
||||
LOG_DEBUG("Application is about to quit. Cleaning up...");
|
||||
sharedMemory.detach();
|
||||
LOG_DEBUG("Application quitting. Cleaning up local server...");
|
||||
|
||||
if (server.isListening()) {
|
||||
server.close();
|
||||
}
|
||||
|
||||
QLocalServer::removeServer("app_server");
|
||||
QFile stale("/tmp/app_server");
|
||||
if (stale.exists())
|
||||
stale.remove();
|
||||
});
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user