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

QHostInfo::lookupHost calls back from another thread

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • Not Evaluated
    • 5.12.5
    • 5.12.4
    • Network
    • None
    • Linux/X11

    Description

      Since 5.9, this method has been introduced:

      static int QHostInfo::lookupHost(const QString &name, Functor functor)

       

      While this is pretty much the same as the one using old style SLOT syntax, there is one difference which I believe should be considered as a bug:

       

      As the documentation mentions, the internals of this method will be threaded, however, using the signal/slot syntax this would - afaiu - be decoupled again as signals/slots by default use Qt::QueuedConnections when different threads are involved. Using this Functor syntax tho, for example with a lambda will cause the lambda to be called from a different thread and I think that's not obvious.

       

      I only noticed because I wrote such code:

       

      QHostInfo::lookupHost("ufo.local", this, [this](const QHostInfo &host) {
          foreach (const QHostAddress &address, host.addresses()) {
               QNetworkRequest request("http://" + address.toString() + "/info");
               QNetworkReply *reply = m_nam->get(request);
               ...
           }
      });
       
      

       

      And that would not work unless QHostInfo's cache is filled. In that case it will directly emit cache results in the same thread as called, but if it actually does resolving, the callback will come from a different thread and break QNetworkAccessManager.

       

      Now, I know how to get around this behavior, but IMO this behavior might cause quite bad bugs in applications as I would not expect it to behave this way.

      Attachments

        Issue Links

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

          Activity

            People

              tpochep Timur Pocheptsov
              mzanetti2 Michael Zanetti
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes