[Interest] QWebSocketServer - server randomly stops accepting connections

Narolewski Jakub izowiuz at gmail.com
Thu Apr 4 15:13:17 CEST 2019


That's the thing. I already handle connections and disconnections in my
code - including logging relevant information.
When I implemented this I heavily based on the example that you linked to.
WSS communication is single threaded - only way to communicate with the
outside world from different thread is to send custom QEvent onto the
Server instance.
QWebSocketServer does not seem to have the method incomingConnection in
it's public API or maybe I already went mad :P I react to the
QWebSocketServer::newConnection signal.

Last RST from client at 33 second mark is our client-side timeout.
This is the code that handles websocket stuff, I have stripped some parts
that are less important:

// server connects
connect(m_wsServer, &QWebSocketServer::acceptError, this,
[this](QAbstractSocket::SocketError socketError) {
        qCritical() << "QWebSocketServer - acceptError:" << socketError;
});

connect(m_wsServer, &QWebSocketServer::peerVerifyError, this, [this](const
QSslError& error) {
        qCritical() << "QWebSocketServer - peerVerifyError:" << error;
});

connect(m_wsServer, &QWebSocketServer::serverError, this,
[this](QWebSocketProtocol::CloseCode closeCode) {
        qCritical() << "QWebSocketServer - serverError:" << closeCode;
});

connect(m_wsServer, &QWebSocketServer::sslErrors, this, [this](const
QList<QSslError>& errors) {
        qCritical() << "QWebSocketServer - sslErrors:" << errors;
});

connect(m_wsServer, &QWebSocketServer::closed, this, [this]() {
        qCritical() << "QWebSocketServer - closed, serverError():" <<
m_wsServer->errorString();
});

connect(m_wsServer, &QWebSocketServer::newConnection, this, [this]() {
        QWebSocket* clientSocket = m_wsServer->nextPendingConnection();
        qInfo() << "Got connection from client:" <<
clientSocket->peerAddress();

        [ client initialization stuff ]

        connect(clientSocket, &QWebSocket::binaryMessageReceived, this,
[this](const QByteArray& message) {
                auto client = sender()->property("client").value<Client*>();

                [ message deserialization ]
        });

        connect(clientSocket, &QWebSocket::disconnected, this, [this]() {
                auto client = sender()->property("client").value<Client*>();
                qInfo() << client->sayHello() << "is disconnected.";

                client->handleDisconnection();

                [ clinet cleanup stuff ]
        });
});

Client's handleDisconnection() function does:

void i9ms::Client::handleDisconnection()
{
        m_isConnected = false;
        m_websocket->deleteLater();
        m_websocket   = nullptr;
}

Nothing from this lands in system logs. It seems that
QWebSocketServer::newConnection() is just not emited.
As I wrote earlier I have a timerEvent that logs QWebSocketServer's state -
once every ten minutes.
It happily reports that server is listening, has no pending connections and
it's internal 'errorString' is empty - even if it is mute from the outside.

It's hard for me to pinpoint exactly what causes this. 'Time' is my best
guess.
I'm also not sure if quantity of connections plays a role here.

I have a 'fuzzer-bot' that spams hundreds of connections and messages at
the server in short period of time.
Malformed messages, out-of-order client state changes, ugly words - you
name it we have it.
QWebSocketServer handles it well enough for us but then, at some time in
future WHILE being completely idle, server goes silent.
On the other hand I can start the server, connect - disconnect one client,
leave it for a while in idle and same thing happens, leaving me one step
closer to dementia.

On Wed, 3 Apr 2019, 22:32 Jason H, <jhihn at gmx.com> wrote:

> Addendum,
>
> How much does you code diverge from the example?
> https://doc.qt.io/qt-5/echoserver.html
> Can you hack that to receive your traffic or augment your code to to do
> what it does?
> Note that the QWebSocket server does not take ownership of the QWebSocket,
> you have to track that manually. Using the code they provide, I would also
> log your connected clients. See the slot
>
>  EchoServer::socketDisconnected()
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190404/66a46715/attachment.html>


More information about the Interest mailing list