Skip to content

Commit 2558577

Browse files
arkqpull[bot]
authored andcommitted
[Linux] Do not reuse cancellable object (#31828)
* [Linux] Do not reuse cancellable object Per documentation for g_cancellable_reset(): > Note that it is generally not a good idea to reuse an existing > cancellable for more operations after it has been cancelled once, as > this function might tempt you to do. The recommended practice is to > drop the reference to a cancellable after cancelling it, and let it > die with the outstanding async operations. * Update cancellable in ChipDeviceScanner
1 parent 202384e commit 2558577

6 files changed

+39
-32
lines changed

src/platform/GLibTypeDeleter.h

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ struct GAutoPtrDeleter<GBytes>
108108
using deleter = GBytesDeleter;
109109
};
110110

111+
template <>
112+
struct GAutoPtrDeleter<GCancellable>
113+
{
114+
using deleter = GObjectDeleter;
115+
};
116+
111117
template <>
112118
struct GAutoPtrDeleter<GDBusConnection>
113119
{

src/platform/Linux/BLEManagerImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ void BLEManagerImpl::DriveBLEState()
571571
// Initializes the Bluez BLE layer if needed.
572572
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kBluezBLELayerInitialized))
573573
{
574-
SuccessOrExit(err = mEndpoint.Init(mAdapterId, mIsCentral, nullptr));
574+
SuccessOrExit(err = mEndpoint.Init(mIsCentral, mAdapterId));
575575
mFlags.Set(Flags::kBluezBLELayerInitialized);
576576
}
577577

src/platform/Linux/bluez/BluezEndpoint.cpp

+15-18
Original file line numberDiff line numberDiff line change
@@ -656,22 +656,14 @@ CHIP_ERROR BluezEndpoint::RegisterGattApplication()
656656
return err;
657657
}
658658

659-
CHIP_ERROR BluezEndpoint::Init(uint32_t aAdapterId, bool aIsCentral, const char * apBleAddr)
659+
CHIP_ERROR BluezEndpoint::Init(bool aIsCentral, uint32_t aAdapterId)
660660
{
661-
CHIP_ERROR err;
661+
VerifyOrReturnError(!mIsInitialized, CHIP_ERROR_INCORRECT_STATE);
662662

663663
mAdapterId = aAdapterId;
664664
mIsCentral = aIsCentral;
665665

666-
if (apBleAddr != nullptr)
667-
mpAdapterAddr = g_strdup(apBleAddr);
668-
669-
if (aIsCentral)
670-
{
671-
mpConnectCancellable = g_cancellable_new();
672-
}
673-
674-
err = PlatformMgrImpl().GLibMatterContextInvokeSync(
666+
CHIP_ERROR err = PlatformMgrImpl().GLibMatterContextInvokeSync(
675667
+[](BluezEndpoint * self) { return self->StartupEndpointBindings(); }, this);
676668
VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "Failed to schedule endpoint initialization"));
677669

@@ -681,6 +673,13 @@ CHIP_ERROR BluezEndpoint::Init(uint32_t aAdapterId, bool aIsCentral, const char
681673
return CHIP_NO_ERROR;
682674
}
683675

676+
CHIP_ERROR BluezEndpoint::Init(bool aIsCentral, const char * apBleAddr)
677+
{
678+
VerifyOrReturnError(!mIsInitialized, CHIP_ERROR_INCORRECT_STATE);
679+
mpAdapterAddr = g_strdup(apBleAddr);
680+
return Init(aIsCentral, mAdapterId);
681+
}
682+
684683
void BluezEndpoint::Shutdown()
685684
{
686685
VerifyOrReturn(mIsInitialized);
@@ -707,8 +706,6 @@ void BluezEndpoint::Shutdown()
707706
g_object_unref(self->mpC2);
708707
if (self->mpC3 != nullptr)
709708
g_object_unref(self->mpC3);
710-
if (self->mpConnectCancellable != nullptr)
711-
g_object_unref(self->mpConnectCancellable);
712709
return CHIP_NO_ERROR;
713710
},
714711
this);
@@ -744,7 +741,7 @@ void BluezEndpoint::ConnectDeviceDone(GObject * aObject, GAsyncResult * aResult,
744741
g_clear_error(&MakeUniquePointerReceiver(error).Get());
745742

746743
bluez_device1_call_disconnect_sync(device, nullptr, &MakeUniquePointerReceiver(error).Get());
747-
bluez_device1_call_connect(device, params->mEndpoint.mpConnectCancellable, ConnectDeviceDone, params);
744+
bluez_device1_call_connect(device, params->mEndpoint.mConnectCancellable.get(), ConnectDeviceDone, params);
748745
return;
749746
}
750747

@@ -760,8 +757,7 @@ void BluezEndpoint::ConnectDeviceDone(GObject * aObject, GAsyncResult * aResult,
760757

761758
CHIP_ERROR BluezEndpoint::ConnectDeviceImpl(ConnectParams * apParams)
762759
{
763-
g_cancellable_reset(apParams->mEndpoint.mpConnectCancellable);
764-
bluez_device1_call_connect(apParams->mpDevice, apParams->mEndpoint.mpConnectCancellable, ConnectDeviceDone, apParams);
760+
bluez_device1_call_connect(apParams->mpDevice, apParams->mEndpoint.mConnectCancellable.get(), ConnectDeviceDone, apParams);
765761
return CHIP_NO_ERROR;
766762
}
767763

@@ -770,6 +766,7 @@ CHIP_ERROR BluezEndpoint::ConnectDevice(BluezDevice1 & aDevice)
770766
auto params = chip::Platform::New<ConnectParams>(*this, &aDevice);
771767
VerifyOrReturnError(params != nullptr, CHIP_ERROR_NO_MEMORY);
772768

769+
mConnectCancellable.reset(g_cancellable_new());
773770
if (PlatformMgrImpl().GLibMatterContextInvokeSync(ConnectDeviceImpl, params) != CHIP_NO_ERROR)
774771
{
775772
ChipLogError(Ble, "Failed to schedule ConnectDeviceImpl() on CHIPoBluez thread");
@@ -782,8 +779,8 @@ CHIP_ERROR BluezEndpoint::ConnectDevice(BluezDevice1 & aDevice)
782779

783780
void BluezEndpoint::CancelConnect()
784781
{
785-
VerifyOrDie(mpConnectCancellable != nullptr);
786-
g_cancellable_cancel(mpConnectCancellable);
782+
g_cancellable_cancel(mConnectCancellable.get());
783+
mConnectCancellable.reset();
787784
}
788785

789786
} // namespace Internal

src/platform/Linux/bluez/BluezEndpoint.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
#include <ble/CHIPBleServiceData.h>
5656
#include <lib/core/CHIPError.h>
57+
#include <platform/GLibTypeDeleter.h>
5758
#include <platform/Linux/dbus/bluez/DbusBluez.h>
5859

5960
#include "BluezConnection.h"
@@ -69,7 +70,8 @@ class BluezEndpoint
6970
BluezEndpoint() = default;
7071
~BluezEndpoint() = default;
7172

72-
CHIP_ERROR Init(uint32_t aAdapterId, bool aIsCentral, const char * apBleAddr);
73+
CHIP_ERROR Init(bool aIsCentral, uint32_t aAdapterId);
74+
CHIP_ERROR Init(bool aIsCentral, const char * apBleAddr);
7375
void Shutdown();
7476

7577
BluezAdapter1 * GetAdapter() const { return mpAdapter; }
@@ -148,8 +150,8 @@ class BluezEndpoint
148150
BluezGattCharacteristic1 * mpC3 = nullptr;
149151

150152
std::unordered_map<std::string, BluezConnection *> mConnMap;
151-
GCancellable * mpConnectCancellable = nullptr;
152-
char * mpPeerDevicePath = nullptr;
153+
GAutoPtr<GCancellable> mConnectCancellable;
154+
char * mpPeerDevicePath = nullptr;
153155

154156
// Allow BluezConnection to access our private members
155157
friend class BluezConnection;

src/platform/Linux/bluez/ChipDeviceScanner.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ CHIP_ERROR ChipDeviceScanner::Init(BluezAdapter1 * adapter, ChipDeviceScannerDel
5858
// Make this function idempotent by shutting down previously initialized state if any.
5959
Shutdown();
6060

61-
mAdapter = BLUEZ_ADAPTER1(g_object_ref(adapter));
62-
mCancellable = g_cancellable_new();
63-
mDelegate = delegate;
61+
mAdapter = BLUEZ_ADAPTER1(g_object_ref(adapter));
62+
mDelegate = delegate;
6463

6564
// Create the D-Bus object manager client object on the glib thread, so that all D-Bus signals
6665
// will be delivered to the glib thread.
@@ -73,7 +72,7 @@ CHIP_ERROR ChipDeviceScanner::Init(BluezAdapter1 * adapter, ChipDeviceScannerDel
7372
self->mManager = g_dbus_object_manager_client_new_for_bus_sync(
7473
G_BUS_TYPE_SYSTEM, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, BLUEZ_INTERFACE, "/",
7574
bluez_object_manager_client_get_proxy_type, nullptr /* unused user data in the Proxy Type Func */,
76-
nullptr /* destroy notify */, self->mCancellable, &MakeUniquePointerReceiver(err).Get());
75+
nullptr /* destroy notify */, nullptr /* cancellable */, &MakeUniquePointerReceiver(err).Get());
7776
VerifyOrReturnError(self->mManager != nullptr, CHIP_ERROR_INTERNAL,
7877
ChipLogError(Ble, "Failed to get D-Bus object manager for device scanning: %s", err->message));
7978
return CHIP_NO_ERROR;
@@ -100,8 +99,6 @@ void ChipDeviceScanner::Shutdown()
10099
g_object_unref(self->mManager);
101100
if (self->mAdapter != nullptr)
102101
g_object_unref(self->mAdapter);
103-
if (self->mCancellable != nullptr)
104-
g_object_unref(self->mCancellable);
105102
return CHIP_NO_ERROR;
106103
},
107104
this);
@@ -115,6 +112,7 @@ CHIP_ERROR ChipDeviceScanner::StartScan(System::Clock::Timeout timeout)
115112
VerifyOrReturnError(mScannerState != ChipDeviceScannerState::SCANNER_SCANNING, CHIP_ERROR_INCORRECT_STATE);
116113
VerifyOrReturnError(mTimerState == ScannerTimerState::TIMER_CANCELED, CHIP_ERROR_INCORRECT_STATE);
117114

115+
mCancellable.reset(g_cancellable_new());
118116
if (PlatformMgrImpl().GLibMatterContextInvokeSync(MainLoopStartScan, this) != CHIP_NO_ERROR)
119117
{
120118
ChipLogError(Ble, "Failed to schedule BLE scan start.");
@@ -191,7 +189,9 @@ CHIP_ERROR ChipDeviceScanner::MainLoopStopScan(ChipDeviceScanner * self)
191189
{
192190
GAutoPtr<GError> error;
193191

194-
g_cancellable_cancel(self->mCancellable); // in case we are currently running a scan
192+
// In case we are currently running a scan
193+
g_cancellable_cancel(self->mCancellable.get());
194+
self->mCancellable.reset();
195195

196196
if (self->mObjectAddedSignal)
197197
{
@@ -303,7 +303,7 @@ CHIP_ERROR ChipDeviceScanner::MainLoopStartScan(ChipDeviceScanner * self)
303303
g_variant_builder_add(&filterBuilder, "{sv}", "Transport", g_variant_new_string("le"));
304304
GVariant * filter = g_variant_builder_end(&filterBuilder);
305305

306-
if (!bluez_adapter1_call_set_discovery_filter_sync(self->mAdapter, filter, self->mCancellable,
306+
if (!bluez_adapter1_call_set_discovery_filter_sync(self->mAdapter, filter, self->mCancellable.get(),
307307
&MakeUniquePointerReceiver(error).Get()))
308308
{
309309
// Not critical: ignore if fails
@@ -312,7 +312,8 @@ CHIP_ERROR ChipDeviceScanner::MainLoopStartScan(ChipDeviceScanner * self)
312312
}
313313

314314
ChipLogProgress(Ble, "BLE initiating scan.");
315-
if (!bluez_adapter1_call_start_discovery_sync(self->mAdapter, self->mCancellable, &MakeUniquePointerReceiver(error).Get()))
315+
if (!bluez_adapter1_call_start_discovery_sync(self->mAdapter, self->mCancellable.get(),
316+
&MakeUniquePointerReceiver(error).Get()))
316317
{
317318
ChipLogError(Ble, "Failed to start discovery: %s", error->message);
318319
return CHIP_ERROR_INTERNAL;

src/platform/Linux/bluez/ChipDeviceScanner.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <ble/CHIPBleServiceData.h>
2525
#include <lib/core/CHIPError.h>
26+
#include <platform/GLibTypeDeleter.h>
2627
#include <platform/Linux/dbus/bluez/DbusBluez.h>
2728
#include <system/SystemLayer.h>
2829

@@ -106,13 +107,13 @@ class ChipDeviceScanner
106107

107108
GDBusObjectManager * mManager = nullptr;
108109
BluezAdapter1 * mAdapter = nullptr;
109-
GCancellable * mCancellable = nullptr;
110110
ChipDeviceScannerDelegate * mDelegate = nullptr;
111111
gulong mObjectAddedSignal = 0;
112112
gulong mInterfaceChangedSignal = 0;
113113
ChipDeviceScannerState mScannerState = ChipDeviceScannerState::SCANNER_UNINITIALIZED;
114114
/// Used to track if timer has already expired and doesn't need to be canceled.
115115
ScannerTimerState mTimerState = ScannerTimerState::TIMER_CANCELED;
116+
GAutoPtr<GCancellable> mCancellable;
116117
};
117118

118119
} // namespace Internal

0 commit comments

Comments
 (0)