Important: Please read the Qt Code of Conduct -

App crash calling QTcpSocket::waitForConnected

  • Hi, I have the following error:
    the next function is called multiple times from diferent threads and after some time, say 20 minutes, crash in socket->waitForConnected(1000). Anybody? Thanks.

    bool Utils::testConnectionToServer(QString host, int port)
        QTcpSocket * socket = new QTcpSocket();
        socket->connectToHost(host, port, QIODevice::ReadOnly);
        if (socket->waitForConnected(1000)) {
            delete socket;
            socket = 0;
            return true;
        } else {
            qDebug() << "### " << socket->errorString() << " ###";
            delete socket;
            socket = 0;
            return false;

  • Lifetime Qt Champion

    Hi and welcome
    Have you checked that new can allocate always ?
    You might fragment memory if calls happens really fast.

    Also , any chance that the threads call it at the SAME time ?
    so following situation could arise ? (when times perfectly)
    QTcpSocket * socket = new QTcpSocket(); << Thread 1 here
    socket->connectToHost(host, port, QIODevice::ReadOnly) ; << Thread 2 here

  • Previously he had a version of the function that used QMutexLocker to avoid just that but also crash in waitForConnected. Specifically, the error displayed is as follows:

    *** buffer overflow detected ***: /home/kiyov/RepoSVN/tss-streamingserver/trunk/StreamingServer/StreamingServer/Server terminated
    ======= Backtrace: =========

  • Lifetime Qt Champion

    it looks like buffer overrun happens inside Qt.
    Have you searched the bug database for issues sounding like this`?

  • @mrjj no, where I can search that bug?

  • Lifetime Qt Champion

  • @mrjj Thanks

  • Lifetime Qt Champion

    Hope you find something.

    Is it always after the same amount of calls?

  • @mrjj I do not know, I have done this test.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I'd change the delete statement by socket->deleteLater() to ensure that you are not deleting the sockets while there's still an event going on the socket.

  • @mrjj No, but it made approximately 1348 calls.

    @SGaist Even changing the delete statement by socket->deleteLeter() the problem persist.

    Apparently all starts in the call to QNativeSocketEnginePrivate::nativeSelect.

    This is the stack trace
    0 __GI_raise raise.c 56 0x7ffff48fd107
    1 __GI_abort abort.c 89 0x7ffff48fe4e8
    2 __libc_message libc_fatal.c 175 0x7ffff493b204
    3 __GI___fortify_fail fortify_fail.c 31 0x7ffff49be4c7
    4 __GI___chk_fail chk_fail.c 28 0x7ffff49bc6e0
    5 __fdelt_chk fdelt_chk.c 25 0x7ffff49be437
    6 QNativeSocketEnginePrivate::nativeSelect qnativesocketengine_unix.cpp 1073 0x7ffff5968e25
    7 QNativeSocketEngine::waitForWrite qnativesocketengine.cpp 947 0x7ffff5966a8d
    8 QAbstractSocket::waitForConnected qabstractsocket.cpp 1814 0x7ffff595f27a
    9 Utils::testConnectionToServer Utils.cpp 106 0x43a099
    10 MyAmqpClient::publishMessage myamqpclient.cpp 206 0x4361d2
    11 Utils::sendMessageToAmqp Utils.cpp 73 0x439bae
    12 StreamingServer::checkSystemResources streamingserver.cpp 1083 0x4152bb
    13 StreamingServer::qt_static_metacall moc_streamingserver.cpp 107 0x459099
    14 QObject::event qobject.cpp 1222 0x7ffff5544e11
    15 QCoreApplication::notifyInternal qcoreapplication.cpp 955 0x7ffff552b71d
    16 sendEvent qcoreapplication.h 231 0x7ffff552efc1
    17 QCoreApplicationPrivate::sendPostedEvents qcoreapplication.cpp 1579 0x7ffff552efc1
    18 QCoreApplication::sendPostedEvents qcoreapplication.cpp 1472 0x7ffff552f453
    19 sendPostedEvents qcoreapplication.h 236 0x7ffff555abd3
    20 postEventSourceDispatch qeventdispatcher_glib.cpp 280 0x7ffff555abd3
    ... <More>

  • Hello everyone,

    sorry for reviving that old thread, but it looks nearly the same as my problem.

    I am using QT 5.6.2 on a headless gentoo. My (QCore) application is using sockets with multiple threads.

    The threads are in a threadpool. Every thread sends its result to the same target after finishing its work (~0.5 to 10 seconds). Each "sends its result" involves a code like this:

    int WorkerThread::sendMessageOverTCPSocket(const QString & result, const QString &ip, const quint16 &port)
    	QTcpSocket socket;
    			ss << m_id <<":timeout while writing data to socket";
    			LOG(ss.str().c_str()); ss.str("");
    		ss << m_id <<":could not connect to socket";
    		LOG(ss.str().c_str()); ss.str("");
      catch (std::exception &e)
    	qFatal("Error(%s) sending results ",e.what());
      catch (...)
    	qFatal("Unknown error occured while trying to send results");
      return 0;

    After about 1100 -1300 threads, the application always terminates. This may take up to a couple of days until it happens "naturally". The one I am currently investigating terminates with SIGABRT.

    No immediate output or log can be found.

    This is a bt from a sample coredump

    #0  0x00007faa868442b8 in raise () from /lib64/
    #1  0x00007faa868456da in abort () from /lib64/
    #2  0x00007faa868803f1 in __libc_message () from /lib64/
    #3  0x00007faa86908b27 in __fortify_fail () from /lib64/
    #4  0x00007faa86906bb0 in __chk_fail () from /lib64/
    #5  0x00007faa86908a9a in __fdelt_warn () from /lib64/
    #6  0x00007faa8a0737a4 in ?? () from /usr/lib64/
    #7  0x00007faa8a070ce2 in ?? () from /usr/lib64/
    #8  0x00007faa8a06a5dc in QAbstractSocket::waitForConnected(int) () from /usr/lib64/

    The line number points to the if(socket.waitForConnected(10203))

    Can someone hint me to the right direction? There seems to be no solution posted to the problem of OP

    Edit: Updated code

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you sure you are cleaning up properly your resources when you're done ?

  • @SGaist
    Thanks for the welcome!

    The resources in use are all local and should get cleaned up, once they are out of scope.
    See the update in my post above.

    I am not sure whether this really is "properly cleaning up". Actually, I assumed the DTOR of the QTCPSocket is enough?
    From the docs:

    [virtual] QTcpSocket::~QTcpSocket()
    Destroys the socket, closing the connection if necessary.
    See also close().

    My current vague suspicion:
    Is the line socket.close(); positioned correctly?

  • Lifetime Qt Champion

    Looks like it.

    How are you handling your thread pool ?

Log in to reply