Avoid crash on stop in ApiServerLocal

It is necessart to wait for QThread to stop before destruction
Avoid closing the socket in ApiLocalConnectionHandler destructor as
this remained as ultimate cause of crash on stop
This commit is contained in:
Gioacchino Mazzurco 2018-01-25 15:18:12 +01:00
parent c1d8d6a52d
commit 8c21bfcd54

View File

@ -16,21 +16,28 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QStringList> #include <QStringList>
#include "ApiServerLocal.h" #include "ApiServerLocal.h"
#include "JsonStream.h" #include "JsonStream.h"
namespace resource_api{ namespace resource_api{
ApiServerLocal::ApiServerLocal(ApiServer* server, const QString &listenPath, QObject *parent) : ApiServerLocal::ApiServerLocal(ApiServer* server,
const QString &listenPath, QObject *parent) :
QObject(parent), serverThread(this), QObject(parent), serverThread(this),
localListener(server, listenPath) // Must have no parent to be movable to other thread // Must have no parent to be movable to other thread
localListener(server, listenPath)
{ {
qRegisterMetaType<QAbstractSocket::SocketState>();
localListener.moveToThread(&serverThread); localListener.moveToThread(&serverThread);
serverThread.start(); serverThread.start();
} }
ApiServerLocal::~ApiServerLocal() { serverThread.quit(); } ApiServerLocal::~ApiServerLocal()
{
serverThread.quit();
serverThread.wait();
}
ApiLocalListener::ApiLocalListener(ApiServer *server, ApiLocalListener::ApiLocalListener(ApiServer *server,
const QString &listenPath, const QString &listenPath,
@ -62,8 +69,12 @@ ApiLocalConnectionHandler::ApiLocalConnectionHandler(
ApiLocalConnectionHandler::~ApiLocalConnectionHandler() ApiLocalConnectionHandler::~ApiLocalConnectionHandler()
{ {
mLocalSocket->close(); /* Any attempt of closing the socket here also deferred method call, causes
delete mLocalSocket; * crash when the core is asked to stop, at least from the JSON API call.
* QMetaObject::invokeMethod(&app, "close", Qt::QueuedConnection)
* mLocalSocket->disconnectFromServer()
* mLocalSocket->close() */
mLocalSocket->deleteLater();
} }
void ApiLocalConnectionHandler::handlePendingRequests() void ApiLocalConnectionHandler::handlePendingRequests()