I have a rather simple application that seems to deadlock under certain conditions when I invoke QEventLoop::exec. The application calls this function in two scenarios:
- when certain data arrives on the socket
- upon a timer event
in both cases it is used in the following context (just an http query, nothing special really):
QNetworkReply::NetworkError HttpGetMessagesStrategy::syncHttp(const QUrl url, QByteArray &dst) const
{
QNetworkRequest request(url);
request.setRawHeader("Cache-Control", "no-cache");
QNetworkAccessManager mgr;
QEventLoop eventLoop;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
QNetworkReply *reply = mgr.get(request);
if (reply == NULL) {
return QNetworkReply::UnknownNetworkError;
}
eventLoop.exec();
QNetworkReply::NetworkError error = reply->error();
if (error == QNetworkReply::NoError) {
dst += reply->readAll();
}
delete reply;
return error;
}
Here's what happens when it tries to call it:
...
#56 0x0000003404b57cdc in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib64/libQtCore.so.4
#57 0x0000003404b804a2 in ?? () from /usr/lib64/libQtCore.so.4
#58 0x0000003404b7d928 in ?? () from /usr/lib64/libQtCore.so.4
#59 0x00000033fba38f0e in g_main_context_dispatch () from /lib64/libglib-2.0.so.0
#60 0x00000033fba3c938 in ?? () from /lib64/libglib-2.0.so.0
#61 0x00000033fba3ca3a in g_main_context_iteration () from /lib64/libglib-2.0.so.0
#62 0x0000003404b7d5f3 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib64/libQtCore.so.4
#63 0x0000003404b56722 in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib64/libQtCore.so.4
#64 0x0000003404b569ec in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib64/libQtCore.so.4
#65 0x00007f41c1b4eec7 in HttpGetMessagesStrategy::syncHttp (this=<value optimized out>, url=<value optimized out>, dst=...) at HttpGetMessagesStrategy.cpp:49
#70 0x000000000040e57c in DevicePlugin::timerEvent (this=0x1267390, event=0x7fff902bb5f0) at DevicePlugin.cpp:250
#71 0x0000003404b6698e in QObject::event(QEvent*) () from /usr/lib64/libQtCore.so.4
#72 0x0000003404b57cdc in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib64/libQtCore.so.4
...
As one can see instead of waiting for a response of the HTTP request, it picked up another event and started to process it. As a result I'm getting a huge number of frames (746) in the current thread before my application comes to a stop and then I see lines like this:
#0 0x00000033faa0efe0 in __pause_nocancel () from /lib64/libpthread.so.0
#1 0x00000033faa0917b in __pthread_mutex_lock_full () from /lib64/libpthread.so.0
#2 0x0000003404a702a3 in ?? () from /usr/lib64/libQtCore.so.4
#3 0x0000003404a6cd95 in QMutex::lock() () from /usr/lib64/libQtCore.so.4
#4 0x0000003404b57952 in QCoreApplication::postEvent(QObject*, QEvent*, int) () from /usr/lib64/libQtCore.so.4
#5 0x000000000040e293 in DevicePlugin::destroyConnection (this=0x1267390, c=0x1a85fb0) at DevicePlugin.cpp:194
#6 0x000000000040e5b6 in DevicePlugin::timerEvent (this=0x1267390, event=0x7fff902b9230) at DevicePlugin.cpp:254
#7 0x0000003404b6698e in QObject::event(QEvent*) () from /usr/lib64/libQtCore.so.4
#8 0x0000003404b57cdc in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib64/libQtCore.so.4
...
Could anyone kindly explain me what am I doing wrong here?