From 4948b2774b8c3443d753a1e7d1f51c8daefab23c Mon Sep 17 00:00:00 2001 From: Terence Hampson Date: Thu, 11 May 2023 11:15:40 -0400 Subject: [PATCH] Fix linux BLE deadload (#26418) Make is so ChipDeviceScanner::StartScan is only called while in the Matter Context --- src/controller/python/chip/ble/LinuxImpl.cpp | 6 +++++- src/platform/Linux/bluez/ChipDeviceScanner.cpp | 3 +-- src/platform/Linux/bluez/ChipDeviceScanner.h | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/controller/python/chip/ble/LinuxImpl.cpp b/src/controller/python/chip/ble/LinuxImpl.cpp index 2aa869ed1a530f..41e589e3c62944 100644 --- a/src/controller/python/chip/ble/LinuxImpl.cpp +++ b/src/controller/python/chip/ble/LinuxImpl.cpp @@ -130,7 +130,11 @@ extern "C" void * pychip_ble_start_scanning(PyObject * context, void * adapter, return nullptr; } - if (scanner->StartScan(chip::System::Clock::Milliseconds32(timeoutMs)) != CHIP_NO_ERROR) + CHIP_ERROR err = CHIP_NO_ERROR; + chip::DeviceLayer::PlatformMgr().LockChipStack(); + err = scanner->StartScan(chip::System::Clock::Milliseconds32(timeoutMs)); + chip::DeviceLayer::PlatformMgr().UnlockChipStack(); + if (err != CHIP_NO_ERROR) { return nullptr; } diff --git a/src/platform/Linux/bluez/ChipDeviceScanner.cpp b/src/platform/Linux/bluez/ChipDeviceScanner.cpp index e20bcf8c4200cd..344eedcce1a191 100644 --- a/src/platform/Linux/bluez/ChipDeviceScanner.cpp +++ b/src/platform/Linux/bluez/ChipDeviceScanner.cpp @@ -141,6 +141,7 @@ std::unique_ptr ChipDeviceScanner::Create(BluezAdapter1 * ada CHIP_ERROR ChipDeviceScanner::StartScan(System::Clock::Timeout timeout) { + assertChipStackLockedByCurrentThread(); ReturnErrorCodeIf(mIsScanning, CHIP_ERROR_INCORRECT_STATE); mIsScanning = true; // optimistic, to allow all callbacks to check this @@ -157,9 +158,7 @@ CHIP_ERROR ChipDeviceScanner::StartScan(System::Clock::Timeout timeout) return CHIP_ERROR_INTERNAL; } - DeviceLayer::PlatformMgr().LockChipStack(); CHIP_ERROR err = chip::DeviceLayer::SystemLayer().StartTimer(timeout, TimerExpiredCallback, static_cast(this)); - DeviceLayer::PlatformMgr().UnlockChipStack(); if (err != CHIP_NO_ERROR) { diff --git a/src/platform/Linux/bluez/ChipDeviceScanner.h b/src/platform/Linux/bluez/ChipDeviceScanner.h index 4dbb244c8772da..2c8c9763b2d6b4 100644 --- a/src/platform/Linux/bluez/ChipDeviceScanner.h +++ b/src/platform/Linux/bluez/ChipDeviceScanner.h @@ -66,6 +66,9 @@ class ChipDeviceScanner ~ChipDeviceScanner(); /// Initiate a scan for devices, with the given timeout + /// + /// This method must be called while in the Matter context (from the Matter event + /// loop, or while holding the Matter stack lock). CHIP_ERROR StartScan(System::Clock::Timeout timeout); /// Stop any currently running scan