From a523a0b21b66f4114598a340ea7346d24d6ea0e5 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Thu, 31 Oct 2019 09:06:09 -0400 Subject: make sure all the data is sent before disconnecting don't fail if waitForReadyRead() returns false but there's data available --- src/singleinstance.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/singleinstance.cpp') diff --git a/src/singleinstance.cpp b/src/singleinstance.cpp index 7abf3879..aa62d40a 100644 --- a/src/singleinstance.cpp +++ b/src/singleinstance.cpp @@ -20,6 +20,7 @@ along with Mod Organizer. If not, see . #include "singleinstance.h" #include "utility.h" #include +#include #include static const char s_Key[] = "mo-43d1a3ad-eeb0-4818-97c9-eda5216c29b5"; @@ -93,17 +94,33 @@ void SingleInstance::sendMessage(const QString &message) } socket.disconnectFromServer(); + socket.waitForDisconnected(); } void SingleInstance::receiveMessage() { QLocalSocket *socket = m_Server.nextPendingConnection(); - if (!socket->waitForReadyRead(s_Timeout)) { - reportError(tr("failed to receive data from secondary instance: %1").arg(socket->errorString())); + if (!socket) { return; } + if (!socket->waitForReadyRead(s_Timeout)) { + // check if there are bytes available; if so, it probably means the data was + // already received by the time waitForReadyRead() was called and the + // connection has been closed + const auto av = socket->bytesAvailable(); + + if (av <= 0) { + MOBase::log::error( + "failed to receive data from secondary instance: {}", + socket->errorString()); + + reportError(tr("failed to receive data from secondary instance: %1").arg(socket->errorString())); + return; + } + } + QString message = QString::fromUtf8(socket->readAll().constData()); emit messageSent(message); socket->disconnectFromServer(); -- cgit v1.3.1