'Network transferred finished but QNetworkReply read data very slow

I am using QNetworkAccessManager to communicate with my RESTful APIserver, one of my API need to fetch a file from server by GET, commonly the file size is about 3MB. Most of time it works very well and very fast, but sometimes it may take very long time and end with RemoteHostClosedError.

My environment:

  • Qt 5.5 MinGW
  • Windows 10 64bit
  • API server run on the same computer

Here is my code to send GET request for the file:

    QUrl url(QString("http://%1:%2/audio/channels")
             .arg(m_ip)
             .arg(m_port));
    QNetworkRequest request(url);
    QNetworkReply *reply = m_http->get(request);
    // Use lamdba to handle fetched file
    connect(reply, &QNetworkReply::finished, [this, reply]{
        // Handle fetched file
    });

When the slow issue occurred, I did wireshark capture, but I found the network transfer is finished very fast and all data is transferred, here is a snapshot of Wireshark capture: Wireshark capture

I also add slot to signal QNetworkReply::downloadProgress, then I found QNetworkReply was always trying to download the data, but very slow at about 5KB/s. Here is the test code:

connect(reply, &QNetworkReply::downloadProgress, [this, reply](qint64 recved, qint64 total){
        qDebug().noquote() << QDateTime::currentDateTimeUtc()
                           << " Downloading from " << m_ip << ":" << m_port
                           << ": " << recved << "/" << total;
    });

Here is part of log output:

QDateTime(2022-04-20 09:06:32.870 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1144846 / 3070158
QDateTime(2022-04-20 09:06:32.969 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1145501 / 3070158
QDateTime(2022-04-20 09:06:33.070 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1146160 / 3070158
QDateTime(2022-04-20 09:06:33.170 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1146837 / 3070158
QDateTime(2022-04-20 09:06:33.271 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1147504 / 3070158
QDateTime(2022-04-20 09:06:33.371 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1148132 / 3070158
QDateTime(2022-04-20 09:06:33.470 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1148751 / 3070158
QDateTime(2022-04-20 09:06:33.571 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1149403 / 3070158
QDateTime(2022-04-20 09:06:33.671 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1150056 / 3070158
QDateTime(2022-04-20 09:06:33.771 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1150729 / 3070158
QDateTime(2022-04-20 09:06:33.871 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1151360 / 3070158
QDateTime(2022-04-20 09:06:33.971 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1152040 / 3070158
QDateTime(2022-04-20 09:06:34.071 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1152707 / 3070158
QDateTime(2022-04-20 09:06:34.171 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1153354 / 3070158
QDateTime(2022-04-20 09:06:34.272 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1153973 / 3070158
QDateTime(2022-04-20 09:06:34.371 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1154618 / 3070158
QDateTime(2022-04-20 09:06:34.471 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1155259 / 3070158
QDateTime(2022-04-20 09:06:34.572 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1155936 / 3070158
QDateTime(2022-04-20 09:06:34.673 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1156592 / 3070158
QDateTime(2022-04-20 09:06:34.772 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1157242 / 3070158
QDateTime(2022-04-20 09:06:34.873 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1157808 / 3070158
QDateTime(2022-04-20 09:06:34.973 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1158460 / 3070158
QDateTime(2022-04-20 09:06:35.073 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1159104 / 3070158
QDateTime(2022-04-20 09:06:35.173 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1159735 / 3070158
QDateTime(2022-04-20 09:06:35.273 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1160407 / 3070158
QDateTime(2022-04-20 09:06:35.373 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1161049 / 3070158
QDateTime(2022-04-20 09:06:35.474 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1161669 / 3070158
QDateTime(2022-04-20 09:06:35.573 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1162266 / 3070158
QDateTime(2022-04-20 09:06:35.673 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1162852 / 3070158
QDateTime(2022-04-20 09:06:35.682 UTC Qt::TimeSpec(UTC))  Downloading from  127.0.0.1 : 8800 :  1162901 / 3070158

Based on my tests, I think it is the reason of QNetworkReply data reading process, it is too slow, but the actual network transferred is finished very fast, after about 3 minutes, server close the connection, then QNetworkReply get the error RemoteHostClosedError, finally cause failed request.

I am confused why network transferred finished, which means the data is already downloaded, but QNetworkReply still take so long to process these data.

Really appreciate if anyone can help me with this, Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source