Details
Description
Using QNetworkReply::setReadBufferSize() to enable throttling and using a timer to read() sometimes causes the download to fail, depending on filesize and the value passed to setReadBufferSize(). The last chunk of the download never completes: readyRead() signal is never emitted, bytesAvailable() is zero. QNetworkReply remains 'running', 'not finished' indefinitely, no errors are reported.
Easy to reproduce. Possibly related to QTBUG-6276.
#include <QHBoxLayout> #include <QLabel> #include <QWidget> #include <QApplication> #include <QtNetwork> class MyWidget: public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = 0); private: QNetworkReply *m_reply; QTimer m_dlTimer; protected slots: void replyFinished(QNetworkReply*); void onReadAndReschedule(); }; MyWidget::MyWidget(QWidget *parent) : QWidget(parent), m_reply(NULL), m_dlTimer(this) { setWindowTitle("Support Tester"); QHBoxLayout* layout = new QHBoxLayout; layout->addWidget(new QLabel("Notice that the download will not complete if it is at" \ "least ~1 MB and throttling is set to ~65000 or lower.")); setLayout(layout); QNetworkAccessManager* nam = new QNetworkAccessManager(this); connect(nam, SIGNAL(finished(QNetworkReply*)), SLOT(replyFinished(QNetworkReply*))); // Download a ~800k file QNetworkRequest netRequest(QUrl("http://get.qt.nokia.com/qtjambi/source/qtjambi-eclipse-integration-linux64-4.4.3_01.tar.gz")); m_reply = nam->get(netRequest); // Set the throttle, try a lower value if the bug doesn't repro with this one. m_reply->setReadBufferSize(36000); connect(&m_dlTimer, SIGNAL(timeout()), SLOT(onReadAndReschedule())); m_dlTimer.setSingleShot(true); m_dlTimer.start(0); } void MyWidget::replyFinished(QNetworkReply*) { qDebug("Finished successfully!"); m_dlTimer.stop(); } void MyWidget::onReadAndReschedule() { const qint64 bytesReceived = m_reply->bytesAvailable(); if (bytesReceived) { QByteArray data = m_reply->read(bytesReceived); const int millisecDelay = static_cast<int>(bytesReceived * 1000 / m_reply->readBufferSize()); qDebug() << "bytesReceived: " << bytesReceived << "Rescheduling in" << millisecDelay << "milliseconds."; m_dlTimer.start(millisecDelay); } else { // BUG: The QNetworkReply has stopped responding near the end of the download. // The last chunk of data is never returned, and the reply never enters an error state. qDebug() << "NO DATA! Error:" << m_reply->error() << "Finished:" << m_reply->isFinished() << "Running:" << m_reply->isRunning(); // Retry m_dlTimer.start(200); } } #include "main.moc" int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget *myWin = new MyWidget(); myWin->show(); return app.exec(); }
Attachments
Issue Links
- relates to
-
QTBUG-6276 QNetworkReply Throttling issue
- Closed