Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-13431

QNetworkReply: Throttling causes download to fail

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 4.7.2
    • 4.6.3, 4.7.0
    • Network
    • None
    • Windows XP SP3
      Visual Studio 2005
      MinGW 4.4
    • 6bf640a7600132c02742a2581402e04f75b60962, d4cad3c2f3fdbf6a1bda926582fb8ad4b6f08666

    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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              mgoetz Markus Goetz (Inactive)
              treinio Topi Reiniƶ
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes