diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2021-05-27 17:44:00 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2021-05-28 14:41:16 +0300 |
commit | 38c74af88c0059eec8ae89e947357488f86126bf (patch) | |
tree | 781adccceea29ef44e9c6bdbd7336e9b4ff96a01 /src | |
parent | 6ce2948519da69b2141988402940e7d03733d16d (diff) |
Fix a mac crash the socket api
On mac SocketApiSocket::writeData can emit disconnected()
This caused a modification of the map containing the sockets during the iteration
and thus a crash.
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/socketapi/socketapi.cpp | 38 | ||||
-rw-r--r-- | src/gui/socketapi/socketapi.h | 7 |
2 files changed, 16 insertions, 29 deletions
diff --git a/src/gui/socketapi/socketapi.cpp b/src/gui/socketapi/socketapi.cpp index 8ca1477c6..3126d0603 100644 --- a/src/gui/socketapi/socketapi.cpp +++ b/src/gui/socketapi/socketapi.cpp @@ -296,15 +296,22 @@ void SocketApi::slotNewConnection() { // Note that on macOS this is not actually a line-based QIODevice, it's a SocketApiSocket which is our // custom message based macOS IPC. - QIODevice *socket = _localServer.nextPendingConnection(); + SocketApiSocket *socket = _localServer.nextPendingConnection(); if (!socket) { return; } qCInfo(lcSocketApi) << "New connection" << socket; - connect(socket, &QIODevice::readyRead, this, &SocketApi::slotReadSocket); - connect(socket, SIGNAL(disconnected()), this, SLOT(onLostConnection())); - connect(socket, &QObject::destroyed, this, &SocketApi::slotSocketDestroyed); + connect(socket, &SocketApiSocket::readyRead, this, &SocketApi::slotReadSocket); + connect(socket, &SocketApiSocket::disconnected, this, [socket] { + qCInfo(lcSocketApi) << "Lost connection " << socket; + // will trigger destroyed in the next execution of the main loop + // a direct removal can cause issues when iterating on _listeners + socket->deleteLater(); + }); + connect(socket, &SocketApiSocket::destroyed, this, [socket, this] { + _listeners.remove(socket); + }); OC_ASSERT(socket->readAll().isEmpty()); auto listener = QSharedPointer<SocketListener>::create(socket); @@ -317,25 +324,9 @@ void SocketApi::slotNewConnection() } } -void SocketApi::onLostConnection() -{ - qCInfo(lcSocketApi) << "Lost connection " << sender(); - sender()->deleteLater(); - - auto socket = qobject_cast<QIODevice *>(sender()); - OC_ASSERT(socket); - _listeners.remove(socket); -} - -void SocketApi::slotSocketDestroyed(QObject *obj) -{ - QIODevice *socket = static_cast<QIODevice *>(obj); - _listeners.remove(socket); -} - void SocketApi::slotReadSocket() { - QIODevice *socket = qobject_cast<QIODevice *>(sender()); + SocketApiSocket *socket = qobject_cast<SocketApiSocket *>(sender()); OC_ENFORCE(socket); // Find the SocketListener @@ -432,10 +423,7 @@ void SocketApi::slotRegisterPath(const QString &alias) Folder *f = FolderMan::instance()->folder(alias); if (f) { - const QString message = buildRegisterPathMessage(removeTrailingSlash(f->path())); - for (const auto &listener : qAsConst(_listeners)) { - listener->sendMessage(message); - } + broadcastMessage(buildRegisterPathMessage(removeTrailingSlash(f->path()))); } _registeredAliases.insert(alias); diff --git a/src/gui/socketapi/socketapi.h b/src/gui/socketapi/socketapi.h index 27b15843e..829ac7e49 100644 --- a/src/gui/socketapi/socketapi.h +++ b/src/gui/socketapi/socketapi.h @@ -26,7 +26,8 @@ #include "socketapisocket_mac.h" #else #include <QLocalServer> -typedef QLocalServer SocketApiServer; +using SocketApiServer = QLocalServer; +using SocketApiSocket = QLocalSocket; #endif class QUrl; @@ -66,8 +67,6 @@ signals: private slots: void slotNewConnection(); - void onLostConnection(); - void slotSocketDestroyed(QObject *obj); void slotReadSocket(); static void copyUrlToClipboard(const QString &link); @@ -157,7 +156,7 @@ private: QString buildRegisterPathMessage(const QString &path); QSet<QString> _registeredAliases; - QMap<QIODevice *, QSharedPointer<SocketListener>> _listeners; + QMap<SocketApiSocket *, QSharedPointer<SocketListener>> _listeners; SocketApiServer _localServer; }; } |