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

Connection to D-Bus signals silently breaks if D-Bus was used before creating QCoreApplication

XMLWordPrintable

    • Linux/Wayland
    • 7ea2bbe25 (dev), f597fa5ce (6.10), 3ca91b4f0 (6.9), 6a0e95dc2 (tqtc/lts-6.8)

      If D-Bus is used (e.g. by synchronously calling some method) before creating Q(Core/Gui)Application, connecting to D-Bus signals (done after application is created) does nothing. Calling methods meanwhile continues to work.

      Sample code:

      #include <QCoreApplication>
      #include <QDBusConnection>
      #include <QDBusInterface>
      #include <QDebug>
      
      void ping() {
        QDBusInterface iface("org.freedesktop.DBus", "/",
                             "org.freedesktop.DBus.Peer");
        auto reply = iface.call("Ping");
        qInfo() << "ping" << reply;
      }
      
      class Receiver : public QObject {
        Q_OBJECT
      public slots:
        void received(const QDBusMessage &message) {
          qInfo() << "received signal" << message;
        }
      };
      
      int main(int argc, char *argv[]) {
        ping();
      
        QCoreApplication a(argc, argv);
      
        ping();
      
        auto receiver = new Receiver();
        QDBusConnection::sessionBus().connect({}, "/", "org.foo.bar", "Fooed",
                                              receiver, SLOT(received(QDBusMessage)));
      
        return a.exec();
      }
      
      #include "main.moc"
      

      Start this program, then run `dbus-send --session --type=signal / org.foo.bar.Fooed` in the terminal to send the signal. You will notice that "received signal" is not printed. If you remove first ping() call, then it works.

      It would be nice to at least have a runtime warning message in this case.

      Debugging revealed the issue is caused by the fact that QDBusConnectionPrivate::enableDispatchDelayed here is called with nullptr argument (which is probably an error since the comment says that "qApp was checked in the caller") which makes QDBusConnectionPrivate::enableDispatchDelayed not call QDBusConnectionPrivate::setDispatchEnabled.

        For Gerrit Dashboard: QTBUG-135928
        # Subject Branch Project Status CR V

            thiago Thiago Macieira
            equeim Alexey
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes