Skip to content

Commit

Permalink
[Fabric-Sync] Fix deadlock when removing device (project-chip#36706)
Browse files Browse the repository at this point in the history
* [Fabric-Sync] Fix deadlock when removing device

* Add assertChipStackLockedByCurrentThread just in case
  • Loading branch information
arkq authored Dec 3, 2024
1 parent c4632a6 commit 9a1d48c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions examples/fabric-sync/bridge/src/BridgedDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,13 @@ std::optional<uint16_t> BridgedDeviceManager::AddDeviceEndpoint(std::unique_ptr<

int BridgedDeviceManager::RemoveDeviceEndpoint(BridgedDevice * dev)
{
assertChipStackLockedByCurrentThread();

uint8_t index = 0;
while (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT)
{
if (mDevices[index].get() == dev)
{
DeviceLayer::StackLock lock;
// Silence complaints about unused ep when progress logging
// disabled.
[[maybe_unused]] EndpointId ep = emberAfClearDynamicEndpoint(index);
Expand All @@ -266,6 +267,8 @@ int BridgedDeviceManager::RemoveDeviceEndpoint(BridgedDevice * dev)

BridgedDevice * BridgedDeviceManager::GetDevice(chip::EndpointId endpointId) const
{
assertChipStackLockedByCurrentThread();

for (uint8_t index = 0; index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; ++index)
{
if (mDevices[index] && mDevices[index]->GetEndpointId() == endpointId)
Expand Down Expand Up @@ -304,6 +307,8 @@ std::string BridgedDeviceManager::GenerateUniqueId()

BridgedDevice * BridgedDeviceManager::GetDeviceByUniqueId(const std::string & id)
{
assertChipStackLockedByCurrentThread();

for (uint8_t index = 0; index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; ++index)
{
if (mDevices[index] && mDevices[index]->GetBridgedAttributes().uniqueId == id)
Expand All @@ -316,6 +321,8 @@ BridgedDevice * BridgedDeviceManager::GetDeviceByUniqueId(const std::string & id

BridgedDevice * BridgedDeviceManager::GetDeviceByScopedNodeId(chip::ScopedNodeId scopedNodeId) const
{
assertChipStackLockedByCurrentThread();

for (uint8_t index = 0; index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; ++index)
{
if (mDevices[index] && mDevices[index]->GetScopedNodeId() == scopedNodeId)
Expand All @@ -328,11 +335,12 @@ BridgedDevice * BridgedDeviceManager::GetDeviceByScopedNodeId(chip::ScopedNodeId

std::optional<uint16_t> BridgedDeviceManager::RemoveDeviceByScopedNodeId(chip::ScopedNodeId scopedNodeId)
{
assertChipStackLockedByCurrentThread();

for (uint16_t index = 0; index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; ++index)
{
if (mDevices[index] && mDevices[index]->GetScopedNodeId() == scopedNodeId)
{
DeviceLayer::StackLock lock;
EndpointId ep = emberAfClearDynamicEndpoint(index);
mDevices[index] = nullptr;
ChipLogProgress(NotSpecified, "Removed device with Id=[%d:0x" ChipLogFormatX64 "] from dynamic endpoint %d (index=%d)",
Expand Down

0 comments on commit 9a1d48c

Please sign in to comment.