You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When a central wants to disconnect from a peripheral with unpair(), it wont work as expected because the bt_conn_set_state() is not a thread-safe function, I think.
First, the function conn_disconnect(), it has two steps, the former will send a disconenct HCI cmd, th later will set connection state, while between them, if the HCI diconnect EVT comes and rx_thread has higher priority, the conn->state may will experience a flow: BT_CONN_CONNECTED -> BT_CONN_DISCONNECT_COMPLETE -> BT_CONN_DISCONNECTED -> BT_CONN_DISCONNECT.
Then, if this thread switch happens in bt_conn_set_state(), we wont get the right state as expected and do bt_conn_ref(conn), would cause a failure when connect to this peripheral next time with err log "Found valid connection in disconnect state".
static int conn_disconnect(struct bt_conn *conn, uint8_t reason)
{
int err;
err = bt_hci_disconnect(conn->handle, reason);
if (err) {
return err;
}
if (conn->state == BT_CONN_CONNECTED) { // <-- the precondition BT_CONN_CONNECTED
bt_conn_set_state(conn, BT_CONN_DISCONNECT);
}
return 0;
}
void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state)
{
bt_conn_state_t old_state;
BT_DBG("%s -> %s", state2str(conn->state), state2str(state));
if (conn->state == state) {
BT_WARN("no transition %s", state2str(state));
return;
}
old_state = conn->state; //<-- get the state again, maybe it has already been modified
conn->state = state;
// so following flow wont work correctly
......
}
To Reproduce
Steps to reproduce the behavior:
make menuconfig and make sure the rx_thread has higher priority than tx_thread,
let central connects to a peripheral,
Here I used a shell cmd like 'bt clear 00:E0:4C:09:E1:CE public' to disconnect.
when reconnect, will have "Connection failed (-22)"
Expected behavior
Maybe the conn->state experience a flow: BT_CONN_CONNECTED -> BT_CONN_DISCONNECT --> BT_CONN_DISCONNECT_COMPLETE -> BT_CONN_DISCONNECTED.
Impact
annoyance: reconnect failed.
Logs and console output NOTE: it`s not a standard zephyr console
we will have OK flow log like this:
briliant-ben
changed the title
Bluetooth: host: bt_unpair failed due to function [bt_conn_set_state] wont work as expected
Bluetooth: host: bt_unpair failed because function [bt_conn_set_state] wont work as expected
Sep 27, 2021
make menuconfig and make sure the rx_thread has higher priority than tx_thread,
We have this:
/* The Bluetooth subsystem requires the Tx thread to execute at higher priority
* than the Rx thread as the Tx thread needs to process the acknowledgements
* before new Rx data is processed. This is a necessity to correctly detect
* transaction violations in ATT and SMP protocols.
*/
BUILD_ASSERT(CONFIG_BT_HCI_TX_PRIO < CONFIG_BT_RX_PRIO);
I haven't studied the zephyr kernel scheduling mechanism yet, if there wont be a thread switching in bt_conn_set_state() (in conn_disconnect), then it wont fail.
Describe the bug
When a central wants to disconnect from a peripheral with unpair(), it wont work as expected because the bt_conn_set_state() is not a thread-safe function, I think.
First, the function conn_disconnect(), it has two steps, the former will send a disconenct HCI cmd, th later will set connection state, while between them, if the HCI diconnect EVT comes and rx_thread has higher priority, the conn->state may will experience a flow: BT_CONN_CONNECTED -> BT_CONN_DISCONNECT_COMPLETE -> BT_CONN_DISCONNECTED -> BT_CONN_DISCONNECT.
Then, if this thread switch happens in bt_conn_set_state(), we wont get the right state as expected and do bt_conn_ref(conn), would cause a failure when connect to this peripheral next time with err log "Found valid connection in disconnect state".
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Maybe the conn->state experience a flow: BT_CONN_CONNECTED -> BT_CONN_DISCONNECT --> BT_CONN_DISCONNECT_COMPLETE -> BT_CONN_DISCONNECTED.
Impact
annoyance: reconnect failed.
Logs and console output
NOTE: it`s not a standard zephyr console
we will have OK flow log like this:
and have FAIL flow log like this:
Environment (please complete the following information):
Not a standard zephyr development environement
Additional context
The text was updated successfully, but these errors were encountered: