From 79b2204af8360a81a8f210a5c2dc097506776c08 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 19 Jun 2025 13:50:42 -0700 Subject: [PATCH] io/socketserver: correctly order startup/teardown across generations Fixes #60 --- src/io/socket.cpp | 27 ++++++++++++++++++++------- src/io/socket.hpp | 10 +++++----- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/io/socket.cpp b/src/io/socket.cpp index a102c7b1..2cf9b625 100644 --- a/src/io/socket.cpp +++ b/src/io/socket.cpp @@ -107,7 +107,11 @@ void Socket::flush() { SocketServer::~SocketServer() { this->disableServer(); } -void SocketServer::onPostReload() { +void SocketServer::onReload(QObject* oldInstance) { + if (auto* old = qobject_cast(oldInstance)) { + old->disableServer(); + } + this->postReload = true; if (this->isActivatable()) this->enableServer(); } @@ -152,6 +156,8 @@ bool SocketServer::isActivatable() { void SocketServer::enableServer() { this->disableServer(); + qCDebug(logSocket) << "Enabling socket server" << this << "at" << this->mPath; + this->server = new QLocalServer(this); QObject::connect( this->server, @@ -160,31 +166,38 @@ void SocketServer::enableServer() { &SocketServer::onNewConnection ); + if (QFile::remove(this->mPath)) { + qCWarning(logSocket) << "Deleted existing file at" << this->mPath << "to create socket"; + } + if (!this->server->listen(this->mPath)) { - qWarning() << "could not start socket server at" << this->mPath; + qCWarning(logSocket) << "Could not start socket server at" << this->mPath; this->disableServer(); } this->activeTarget = false; + this->activePath = this->mPath; emit this->activeStatusChanged(); } void SocketServer::disableServer() { auto wasActive = this->server != nullptr; - if (this->server != nullptr) { + if (wasActive) { + qCDebug(logSocket) << "Disabling socket server" << this << "at" << this->activePath; for (auto* socket: this->mSockets) { socket->deleteLater(); } this->mSockets.clear(); + this->server->close(); this->server->deleteLater(); this->server = nullptr; - } - if (this->mPath != nullptr) { - if (QFile::exists(this->mPath) && !QFile::remove(this->mPath)) { - qWarning() << "failed to delete socket file at" << this->mPath; + if (!this->activePath.isEmpty()) { + if (QFile::exists(this->activePath) && !QFile::remove(this->activePath)) { + qWarning() << "Failed to delete socket file at" << this->activePath; + } } } diff --git a/src/io/socket.hpp b/src/io/socket.hpp index c710dbdc..64605f86 100644 --- a/src/io/socket.hpp +++ b/src/io/socket.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -90,9 +91,7 @@ private: /// } /// } /// ``` -class SocketServer - : public QObject - , public PostReloadHook { +class SocketServer: public Reloadable { Q_OBJECT; /// If the socket server is currently active. Defaults to false. /// @@ -115,11 +114,11 @@ class SocketServer QML_ELEMENT; public: - explicit SocketServer(QObject* parent = nullptr): QObject(parent) {} + explicit SocketServer(QObject* parent = nullptr): Reloadable(parent) {} ~SocketServer() override; Q_DISABLE_COPY_MOVE(SocketServer); - void onPostReload() override; + void onReload(QObject* oldInstance) override; [[nodiscard]] bool isActive() const; void setActive(bool active); @@ -149,4 +148,5 @@ private: bool activeTarget = false; bool postReload = false; QString mPath; + QString activePath; };