From bb146a76d06a9039d55661a3e4fc42176c078643 Mon Sep 17 00:00:00 2001 From: Rob Emery Date: Mon, 22 Jul 2024 21:29:03 +0000 Subject: [PATCH] Bluetooth provider workaround for BlueZ/DBus timeouts Context: https://invent.kde.org/network/kdeconnect-kde/-/merge_requests/600#note_884500 When bluetooth doesn't exist on the machine at all, QTConnectivity tries to communicate with Bluez via dbus and introduces a 30 odd second pause. That's not necessarily a problem in concept, however this blocks the main thread of KDEConnect, which also then blocks the main thread of Plasma on logon and causes tremendous delays and very broken behaviour. For the life of me, I cannot find a way to do "is bluetooth ok" without QTConnect kicking off the dbus call so I think the only option is to thread off the startup of the providers so that pauses don't block the whole process. I've just tested this here and my logon with bluetooth missing went from approx 35 seconds down to about 2. Ready for input/feedback whenever people have time; in my testing at the moment it seems to completely break the behaviour of KDEConnect (i.e. things can't connect), I'm guessing this is something to do with the effect of wrapping everything in the QThread. I'll dig into that next and see if I can figure it out. BUG: 481870 --- core/daemon.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/core/daemon.cpp b/core/daemon.cpp index 4e0ddce65..3ec70bdd7 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -62,6 +62,16 @@ void Daemon::init() { qCDebug(KDECONNECT_CORE) << "Daemon starting"; + // Register on DBus + // This must happen as early as possible in the process startup to ensure + // the absolute minimum amount of blocking on logon/autostart + + qDBusRegisterMetaType>(); + QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kdeconnect")); + QDBusConnection::sessionBus().registerObject(QStringLiteral("/modules/kdeconnect"), this, QDBusConnection::ExportScriptableContents); + + qCDebug(KDECONNECT_CORE) << "DBus registration complete"; + // Load backends if (d->m_testMode) d->m_linkProviders.insert(new LoopbackLinkProvider()); @@ -75,10 +85,7 @@ void Daemon::init() #endif } - // Register on DBus - qDBusRegisterMetaType>(); - QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kdeconnect")); - QDBusConnection::sessionBus().registerObject(QStringLiteral("/modules/kdeconnect"), this, QDBusConnection::ExportScriptableContents); + qCDebug(KDECONNECT_CORE) << "Backends loaded"; // Read remembered paired devices const QStringList &list = KdeConnectConfig::instance().trustedDevices(); @@ -86,12 +93,16 @@ void Daemon::init() addDevice(new Device(this, id)); } + qCDebug(KDECONNECT_CORE) << "Paired devices added"; + // Listen to new devices for (LinkProvider *a : std::as_const(d->m_linkProviders)) { connect(a, &LinkProvider::onConnectionReceived, this, &Daemon::onNewDeviceLink); a->onStart(); } + qCDebug(KDECONNECT_CORE) << "Link providers started"; + NotificationServerInfo::instance().init(); qCDebug(KDECONNECT_CORE) << "Daemon started";