-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Tizen] Properly implement BLEManagerImpl::_NumConnections #39965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request implements _NumConnections
to return the actual number of BLE connections. The review focuses on a potential race condition when accessing the shared connection map and suggests adding thread synchronization using the platform's locking mechanism to ensure data integrity.
VerifyOrReturnValue(mConnectionMap != nullptr, 0); | ||
return g_hash_table_size(mConnectionMap); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential race condition here. mConnectionMap
can be modified by BLE event callbacks (like GattConnectionStateChangedCb
) on a different thread, while this function could be called from any application thread. Accessing mConnectionMap
without synchronization can lead to undefined behavior, such as crashes or returning an incorrect value.
To ensure thread safety, you should wrap the access to mConnectionMap
with PlatformMgr().LockChipStack()
and PlatformMgr().UnlockChipStack()
.
Additionally, g_hash_table_size
returns a guint
(usually 32-bit), while this function returns a uint16_t
. It's good practice to use an explicit static_cast
for the conversion to make the intent clear and prevent potential compiler warnings, even though it's unlikely for the number of connections to exceed the uint16_t
limit.
VerifyOrReturnValue(mConnectionMap != nullptr, 0); | |
return g_hash_table_size(mConnectionMap); | |
PlatformMgr().LockChipStack(); | |
const guint size = mConnectionMap ? g_hash_table_size(mConnectionMap) : 0; | |
PlatformMgr().UnlockChipStack(); | |
return static_cast<uint16_t>(size); |
PR #39965: Size comparison from c0a78ce to 0258186 Full report (70 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink)
|
Summary
The
chip::DeviceLayer::ConnectivityMgr().NumBLEConnections()
always returns 0 because underlying implementation was incorrect.Testing
Locally verified that the application can check whether there is an ongoing BLE connection.