diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-11-22 17:45:33 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-11-24 09:49:36 +0100 |
commit | bf69c830419a7cda87d6aa71c962804a946011ea (patch) | |
tree | e858b08df23d10d2ca976c2eb81dea0b9468c066 | |
parent | b0d5e01c3160dc734fa8c4f182a1503abcac5d8f (diff) | |
download | qtconnectivity-bf69c830419a7cda87d6aa71c962804a946011ea.tar.gz |
Fix thread affinity for charListObtained slot
The connection between QWinRTLowEnergyServiceHandler::charListObtained
and a corresponding lambda was established using a 3-argument version
of QObject::connect, which is implying Qt::DirectConnection.
This means that the lambda was actually executed on the same helper
thread that was responsible for reading characteristics.
This is not the intentional behavior, because it could result in a
concurrent access to serviceList. Also registerForValueChanges()
could be called from multiple threads.
This patch uses the 4-argument version of QObject::connect, so that
Qt::AutoConnection is used. It means that the lambda will be executed
on the main thread.
This patch also removes a duplicated call to thread->exit() from the
lambda. The original call is already performed in a method that emits
the signal. It was not an issue with Qt::DirectConnection, but can be
a problem with the fixed behavior.
Task-number: QTBUG-97578
Change-Id: Ia363489024e46e2879a4bd5e56c2ab8cd6606306
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
(cherry picked from commit 4ecc718d418689b9b9b66086ef4d041a21f44327)
-rw-r--r-- | src/bluetooth/qlowenergycontroller_winrt.cpp | 5 | ||||
-rw-r--r-- | src/bluetooth/qlowenergycontroller_winrt_new.cpp | 6 |
2 files changed, 4 insertions, 7 deletions
diff --git a/src/bluetooth/qlowenergycontroller_winrt.cpp b/src/bluetooth/qlowenergycontroller_winrt.cpp index 6a039337..9109102e 100644 --- a/src/bluetooth/qlowenergycontroller_winrt.cpp +++ b/src/bluetooth/qlowenergycontroller_winrt.cpp @@ -644,8 +644,8 @@ void QLowEnergyControllerPrivateWinRT::discoverServiceDetails(const QBluetoothUu connect(thread, &QThread::started, worker, &QWinRTLowEnergyServiceHandler::obtainCharList); connect(thread, &QThread::finished, thread, &QObject::deleteLater); connect(thread, &QThread::finished, worker, &QObject::deleteLater); - connect(worker, &QWinRTLowEnergyServiceHandler::charListObtained, - [this, thread](const QBluetoothUuid &service, QHash<QLowEnergyHandle, QLowEnergyServicePrivate::CharData> charList + connect(worker, &QWinRTLowEnergyServiceHandler::charListObtained, this, + [this](const QBluetoothUuid &service, QHash<QLowEnergyHandle, QLowEnergyServicePrivate::CharData> charList , QVector<QBluetoothUuid> indicateChars , QLowEnergyHandle startHandle, QLowEnergyHandle endHandle) { if (!serviceList.contains(service)) { @@ -668,7 +668,6 @@ void QLowEnergyControllerPrivateWinRT::discoverServiceDetails(const QBluetoothUu Q_ASSERT_SUCCEEDED(hr); pointer->setState(QLowEnergyService::ServiceDiscovered); - thread->exit(0); }); thread->start(); } diff --git a/src/bluetooth/qlowenergycontroller_winrt_new.cpp b/src/bluetooth/qlowenergycontroller_winrt_new.cpp index ed082ba1..5f8d5de5 100644 --- a/src/bluetooth/qlowenergycontroller_winrt_new.cpp +++ b/src/bluetooth/qlowenergycontroller_winrt_new.cpp @@ -1170,8 +1170,8 @@ void QLowEnergyControllerPrivateWinRTNew::discoverServiceDetails(const QBluetoot connect(thread, &QThread::finished, worker, &QObject::deleteLater); connect(worker, &QWinRTLowEnergyServiceHandlerNew::errorOccured, this, &QLowEnergyControllerPrivateWinRTNew::handleServiceHandlerError); - connect(worker, &QWinRTLowEnergyServiceHandlerNew::charListObtained, - [this, reactOnDiscoveryError, thread](const QBluetoothUuid &service, QHash<QLowEnergyHandle, + connect(worker, &QWinRTLowEnergyServiceHandlerNew::charListObtained, this, + [this, reactOnDiscoveryError](const QBluetoothUuid &service, QHash<QLowEnergyHandle, QLowEnergyServicePrivate::CharData> charList, QVector<QBluetoothUuid> indicateChars, QLowEnergyHandle startHandle, QLowEnergyHandle endHandle) { if (!serviceList.contains(service)) { @@ -1194,12 +1194,10 @@ void QLowEnergyControllerPrivateWinRTNew::discoverServiceDetails(const QBluetoot if (FAILED(hr)) { reactOnDiscoveryError(pointer, QStringLiteral("Could not register for value changes in Xaml thread: %1").arg(hr)); - thread->exit(0); return; } pointer->setState(QLowEnergyService::ServiceDiscovered); - thread->exit(0); }); thread->start(); } |