Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDbus: connection fails but lastError indicates no error
Qt 6.11 is out! See what's new in the release blog

QDbus: connection fails but lastError indicates no error

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 3 Posters 4.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brgl
    wrote on last edited by
    #1

    I have a DBus bus name implementing the ObjectManager interface which is visible in qdbus like this:

    # qdbus --system org.veki.CloudAgent1 /org/veki/CloudAgent1
    <snip>
    signal void org.freedesktop.DBus.ObjectManager.InterfacesAdded(QDBusObjectPath object_path, {D-Bus type "a{sa{sv}}"} interfaces_and_properties)
    signal void org.freedesktop.DBus.ObjectManager.InterfacesRemoved(QDBusObjectPath object_path, QStringList interfaces)
    

    Running the following code:

    auto ret = QDBusConnection::systemBus().connect(
    		"org.veki.CloudAgent1",
    		"/org/veki/CloudAgent1",
    		"org.freedesktop.DBus.ObjectManager",
    		"InterfacesAdded",
    		&receiver,
    		SLOT(InterfacesAded(QDBusObjectPath, InterfaceList)));
    

    where Receiver is declared like this:

    typedef QMap<QString, QVariantMap> InterfaceList;
    
    class Receiver : public QObject
    {
    	Q_OBJECT
    
    public:
    	Receiver(QObject *parent = nullptr);
    	virtual ~Receiver();
    
    private slots:
    	void InterfacesAdded(QDBusObjectPath object_path, InterfaceList interfaces_and_properties);
    };
    

    fails because ret is false but calling QDBusConnection::systemBus().lastError().type() returns QDBusError::NoError.

    What can I do to figure out what's wrong?

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Maybe a typo ?

      You have "InterfacesAded´ in your connect statement while the slot is named "InterfacesAdded" in your class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        brgl
        wrote on last edited by
        #3

        Ha! True that it was a typo. Why does that even build? Anyway, fixing the typo didn't help. Is there any DBus verbose logging I could enable?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Because you are using a string based connection. This does not offer compile time error checking.

          For debugging, you have tips here.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            brgl
            wrote on last edited by
            #5

            I missed that one, thanks. This is what I see with QDBUS_DEBUG=1:

            QDBusConnectionPrivate(0x7fb0001c00) : connected successfully
            QDBusConnectionPrivate(0x7fb0001c00) got message (signal): QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.249") )
            <Here is where the connect() method returns false and no error.>
            QDBusConnectionPrivate(0x7fb0001c00) Disconnected
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you sure the lifetime of your objects are fine ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • B Offline
                B Offline
                brgl
                wrote on last edited by
                #7

                Ok so the issue was spotted in a bigger Qt application, I just started reducing the problem to a couple lines to figure out what's wrong.

                The above example was missing the event loop so I did this:

                int main(int argc, char **argv)
                {
                	QApplication app(argc, argv);
                
                	qInfo() << "Before connection";
                	qInfo() << "Is connected " << QDBusConnection::systemBus().isConnected();
                
                	Receiver receiver;
                
                	qInfo() << "Trying to connect";
                	auto ret = QDBusConnection::systemBus().connect(
                		"org.veki.CloudAgent1",
                		"/org/veki/CloudAgent1",
                		"org.freedesktop.DBus.ObjectManager",
                		"InterfacesAdded",
                		&receiver,
                		SLOT(InterfacesAdded(QDBusObjectPath, InterfaceList)));
                
                	qInfo() << "connect(): " << ret;
                
                	qInfo() << "connect() error: " << QDBusConnection::systemBus().lastError().type();
                
                	return app.exec();
                }
                

                And with QDBUS_DEBUG enabled the output looks like this:

                Before connection
                QDBusConnectionPrivate(0x7f90004f50) : connected successfully
                Is connected  true
                Trying to connect
                QDBusConnectionPrivate(0x7f90004f50) got message (signal): QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.27") )
                connect():  false
                QDBusConnectionPrivate(0x7f90004f50) delivery is suspended
                connect() error:  QDBusError::NoError
                QDBusConnectionPrivate(0x7f90004f50) dequeueing message QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.27") )
                

                This delivery is suspended message is emitted from src/dbus/qdbusintegrator.cpp from this method:

                bool QDBusConnectionPrivate::handleMessage(const QDBusMessage &amsg)
                {
                    if (!ref.loadRelaxed())
                        return false;
                
                    // local message are always delivered, regardless of filtering
                    // or whether the dispatcher is enabled
                    bool isLocal = QDBusMessagePrivate::isLocal(amsg);
                
                    if (!dispatchEnabled && !isLocal) {
                        // queue messages only, we'll handle them later
                        qDBusDebug() << this << "delivery is suspended";
                        pendingMessages << amsg;
                        return amsg.type() == QDBusMessage::MethodCallMessage;
                    }
                

                I need to figure out what this dispatchEnabled field is about.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Are you sure your slot signature is correct ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    brgl
                    wrote on last edited by
                    #9

                    Yes,

                    The slot function is:

                    void Receiver::InterfacesAdded(QDBusObjectPath object_path, QMap<QString, QVariantMap> interfaces_and_properties)
                    

                    The SLOT expansion is:

                    SLOT(InterfacesAdded(QDBusObjectPath, QMap<QString, QVariantMap>)
                    
                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      brgl
                      wrote on last edited by
                      #10

                      I tried using the extended variant of the connect() method:

                      	auto ret = QDBusConnection::systemBus().connect(
                      		"org.veki.CloudAgent1",
                      		"/org/veki/CloudAgent1",
                      		"org.freedesktop.DBus.ObjectManager",
                      		"InterfacesAdded",
                      		"a{sa{sv}}",
                      		&receiver,
                      		SLOT(InterfacesAdded(QDBusObjectPath, QMap<QString, QVariantMap>)));
                      

                      Same (lack of) effect.

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        brgl
                        wrote on last edited by
                        #11

                        This delivery is suspended message and the subsequent dequeueing the message one are part of the normal flow it seems. It dequeues the message once the main loop is running. Still not sure why I can't connect to the signal. I checked with gdbus monitor and a bit of python code using pydbus and the provider of this signal works just fine, I can connect to it and be notified when it's triggered.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Are you using the Qt version of your distribution ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            brgl
                            wrote on last edited by
                            #13

                            I tried this on qt5 & qt6 from the distro (ubuntu 22.04). I tried the same on a yocto based OS with meta-qt5 and meta-qt6 respectively. Qt6 version is 6.4.1. I tried switching to session bus (but that shouldn't matter as gdbus monitor works fine as does pydbus, no issues with dbus config).

                            I created a very minimal example with a pydbus provider that after 5 seconds adds an interface:

                            #!/usr/bin/env python3
                            
                            import pydbus
                            import pydbus_manager
                            
                            from gi.repository import GLib
                            from functools import partial
                            
                            class Provider:
                                """
                                <node>
                                    <interface name='org.dupa.Provider1'>
                                        <property name='Dupa' type='s' access='read'/>
                                    </interface>
                                </node>
                                """
                            
                                @property
                                def Dupa(self):
                                    return "dupa dupa"
                            
                            def add_interface(manager):
                                print("Adding object")
                                manager.register_object("/org/dupa/Provider1/Dupa", Provider(), None);
                                return GLib.SOURCE_REMOVE
                            
                            if __name__ == "__main__":
                                loop = GLib.MainLoop()
                                bus = pydbus.SessionBus()
                                manager = pydbus_manager.Manager(bus, "org.dupa.Provider1")
                            
                                GLib.timeout_add_seconds(5, partial(add_interface, manager))
                            
                                loop.run()
                            

                            And the minimal Qt example doesn't work with it all the same. It seems to me that if connect() returns false but no error is set, then it's a bug in the library?

                            It's probably something stupid but I cannot figure it out.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              To make things simple to test:

                              • Can you provide a requirement.txt for your example script ?
                              • Can you provide a minimal C++ example that uses your provider for testing as well ?
                                That way it will be easier to reproduce your issue in similar conditions.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              N 1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                brgl
                                wrote on last edited by brgl
                                #15

                                Hi @SGaist !

                                Here's a repo with a functional example: https://github.com/brgl/qdbus-signal-example

                                Inside you'll find a requirements.txt file for the python scripts, a functional python signal provider and receiver and a qt6 receiver that doesn't work.

                                For simplicity it uses the session bus. You need to run python-provider/provider.py and then within 5 seconds run the python-receiver/receiver.py script and observe that it correctly receives the signal. You can then run qmake6 and make inside the qt6-receiver directory and do the same for the qt receiver program. It will fail to connect and not receive the signal as well as not indicate any error.

                                Let me know if that's clear and thanks for helping!

                                1 Reply Last reply
                                1
                                • SGaistS SGaist

                                  To make things simple to test:

                                  • Can you provide a requirement.txt for your example script ?
                                  • Can you provide a minimal C++ example that uses your provider for testing as well ?
                                    That way it will be easier to reproduce your issue in similar conditions.
                                  N Offline
                                  N Offline
                                  n1k0
                                  wrote on last edited by
                                  #16

                                  Hi @SGaist, were you able to male any progress on this? Thank you for your help!

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    I am suspecting that there's an issue with the signature of the slot but I haven't been able to pinpoint it.

                                    The D-Bus type is a{sa{sv}} So it's a dictionary with string as key and as value a dictionary with string as key and variant as values. The variant is mapped to QDBusVariant based on D-Bus type system so I would check in that direction.

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      brgl
                                      wrote on last edited by
                                      #18

                                      How would I debug any such issues with signal-to-slot signatures?

                                      1 Reply Last reply
                                      0
                                      • B Offline
                                        B Offline
                                        brgl
                                        wrote on last edited by brgl
                                        #19

                                        https://doc.qt.io/qt-6/qdbustypesystem.html#the-type-system-in-use Does this:

                                        Warning: You may not use any type that is not on the list above, including typedefs to the types listed. This also includes QList<QVariant> and QMap<QString,QVariant>.
                                        

                                        Mean I cannot receive signals passing dicts as arguments?

                                        QDbusViewer is telling me:

                                        Error: Unable to connect to service org.foobar.Provider1, path /org/foobar/Provider1, interface org.freedesktop.DBus.ObjectManager, signal InterfacesAdded
                                        
                                        1 Reply Last reply
                                        0
                                        • B Offline
                                          B Offline
                                          brgl
                                          wrote on last edited by
                                          #20

                                          I've been browsing the KDE Udisks code that uses qt AND connects to the InterfacesAdded signal. I added some bits that were missing from my code:

                                          typedef QMap<QString, QVariantMap> QVariantMapMap;
                                          Q_DECLARE_METATYPE(QVariantMapMap)
                                          

                                          and

                                          qDBusRegisterMetaType<QVariantMapMap>();
                                          

                                          but that didn't fix it.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved