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

QNetworkAccessManager leaks open sockets after destruction

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.15.6
    • Network
    • None
    • Linux/X11

    Description

      I'm using several QNetworkAccessManager in the app. A single QNetworkAccessManager instance per thread. Threads are spawned and destroyed per request. So QNetworkAccessManager too.

      It was noticed that accessing some URLs cause open socket (states ESTABLISHED, then CLOSE_WAIT) left in system. After a while the app reaches limit of open file descriptors and crash with `GLib-ERROR **: Creating pipes for GWakeup: Too many open files`. The leaked sockets could be seen in terminal while the app is running by command `sudo lsof -c myapp`. 

      Only specific URLs cause that problem. These are FTP urls. 2 of 3 ftp URLs I tested cause the problem. I guess that related to "active/passive" ftp server mode.   

      I sugest this URL to reproduce the problem: `ftp://ftp.kiam1.rssi.ru/pub/gps/solar/solarinex.txt`   

      The code to reproduce is following:

      #test.h

      ```

      #ifndef TEST_H
      #define TEST_H

      #include <QObject>
      #include <QThread>
      #include <QNetworkAccessManager>
      #include <QNetworkReply>
      #include <QDebug>

      class QMyThread: public QThread
      {
          Q_OBJECT
      public:
          explicit QMyThread(QObject* parent = nullptr): QThread(parent) {}
          ~QMyThread(){qDebug() << "thread destroy";}
          void run() override
          {
              qDebug() << "thread start";
              QNetworkRequest request(QUrl("ftp://ftp.kiam1.rssi.ru/pub/gps/solar/solarinex.txt"));

              QNetworkAccessManager nam;
              nam.setTransferTimeout();

              QNetworkReply *response = nam.get(request);

              connect(response, &QNetworkReply::errorOccurred, this, [=](QNetworkReply::NetworkError err)

      {             qDebug() << QString("error %1").arg(err);         }

      );

              connect(response, &QNetworkReply::redirected, this, [=](const QUrl &url)

      {             qDebug() << QString("redir %1").arg(url.toString());         }

      );

              connect(response, &QNetworkReply::finished, this, [=]() {
                  if (response->error() != QNetworkReply::NoError)

      {                 qDebug() << response->errorString();             }

      else

      {                 response->readAll();             }

                  quit();
              });

              exec();

              if (response)

      {             qDebug() << "response deleted";             response->deleteLater();         }

              qDebug() << "thread stop";
          }
      };

      #endif // TEST_H

      ```

       

      #main.cpp

      ```

      #include <QCoreApplication>
      #include "test.h"

      int main(int argc, char *argv[])

      {     QCoreApplication a(argc, argv);     QMyThread* thread = new QMyThread;     QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);     thread->start();     return a.exec(); }

      ```

      #app output:

      ```

      thread start
      response deleted
      thread stop
      thread destroy

      ```

      app open sockets left after "thread destroy":

      ` sudo lsof -c untitled`

       

      Expected behaviour:

      No any open TCP sockets to ftp addresses after QNetworkAccessManager is destroyed.

      Attachments

        1. test_src.tar.gz
          1 kB
          Alexander Trufanov
        2. image-2023-09-01-14-56-53-965.png
          20 kB
          Alexander Trufanov
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            manordheim Mårten Nordheim
            truf Alexander Trufanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes