Skip to content

Commit 79b2204

Browse files
committed
io/socketserver: correctly order startup/teardown across generations
Fixes #60
1 parent 95d0af8 commit 79b2204

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/io/socket.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ void Socket::flush() {
107107

108108
SocketServer::~SocketServer() { this->disableServer(); }
109109

110-
void SocketServer::onPostReload() {
110+
void SocketServer::onReload(QObject* oldInstance) {
111+
if (auto* old = qobject_cast<SocketServer*>(oldInstance)) {
112+
old->disableServer();
113+
}
114+
111115
this->postReload = true;
112116
if (this->isActivatable()) this->enableServer();
113117
}
@@ -152,6 +156,8 @@ bool SocketServer::isActivatable() {
152156
void SocketServer::enableServer() {
153157
this->disableServer();
154158

159+
qCDebug(logSocket) << "Enabling socket server" << this << "at" << this->mPath;
160+
155161
this->server = new QLocalServer(this);
156162
QObject::connect(
157163
this->server,
@@ -160,31 +166,38 @@ void SocketServer::enableServer() {
160166
&SocketServer::onNewConnection
161167
);
162168

169+
if (QFile::remove(this->mPath)) {
170+
qCWarning(logSocket) << "Deleted existing file at" << this->mPath << "to create socket";
171+
}
172+
163173
if (!this->server->listen(this->mPath)) {
164-
qWarning() << "could not start socket server at" << this->mPath;
174+
qCWarning(logSocket) << "Could not start socket server at" << this->mPath;
165175
this->disableServer();
166176
}
167177

168178
this->activeTarget = false;
179+
this->activePath = this->mPath;
169180
emit this->activeStatusChanged();
170181
}
171182

172183
void SocketServer::disableServer() {
173184
auto wasActive = this->server != nullptr;
174185

175-
if (this->server != nullptr) {
186+
if (wasActive) {
187+
qCDebug(logSocket) << "Disabling socket server" << this << "at" << this->activePath;
176188
for (auto* socket: this->mSockets) {
177189
socket->deleteLater();
178190
}
179191

180192
this->mSockets.clear();
193+
this->server->close();
181194
this->server->deleteLater();
182195
this->server = nullptr;
183-
}
184196

185-
if (this->mPath != nullptr) {
186-
if (QFile::exists(this->mPath) && !QFile::remove(this->mPath)) {
187-
qWarning() << "failed to delete socket file at" << this->mPath;
197+
if (!this->activePath.isEmpty()) {
198+
if (QFile::exists(this->activePath) && !QFile::remove(this->activePath)) {
199+
qWarning() << "Failed to delete socket file at" << this->activePath;
200+
}
188201
}
189202
}
190203

src/io/socket.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <qcontainerfwd.h>
34
#include <qlocalserver.h>
45
#include <qlocalsocket.h>
56
#include <qloggingcategory.h>
@@ -90,9 +91,7 @@ private slots:
9091
/// }
9192
/// }
9293
/// ```
93-
class SocketServer
94-
: public QObject
95-
, public PostReloadHook {
94+
class SocketServer: public Reloadable {
9695
Q_OBJECT;
9796
/// If the socket server is currently active. Defaults to false.
9897
///
@@ -115,11 +114,11 @@ class SocketServer
115114
QML_ELEMENT;
116115

117116
public:
118-
explicit SocketServer(QObject* parent = nullptr): QObject(parent) {}
117+
explicit SocketServer(QObject* parent = nullptr): Reloadable(parent) {}
119118
~SocketServer() override;
120119
Q_DISABLE_COPY_MOVE(SocketServer);
121120

122-
void onPostReload() override;
121+
void onReload(QObject* oldInstance) override;
123122

124123
[[nodiscard]] bool isActive() const;
125124
void setActive(bool active);
@@ -149,4 +148,5 @@ private slots:
149148
bool activeTarget = false;
150149
bool postReload = false;
151150
QString mPath;
151+
QString activePath;
152152
};

0 commit comments

Comments
 (0)