Skip to content

Commit f4c6404

Browse files
Albert MojirJohan Redestig
authored andcommitted
Bluetooth event loop dispatches dbus data on wakeup
On some occasions when the event loop thread and a binder thread are both waiting for a message from dbus, both threads are not woken up when a message is received. This causes applications not receiving responses or events. This happens because both threads are listening to same socket and both threads are not guaranteed to wake up when there is data to read. To fix this we subscribe to callback to wake the eventloop when an message is added to incoming queue. To reproduce the issue: 1. Activate BT 2. Make phone Discoverable 3. Clock is ticking down from 120s 4. At 20s tap the setting again 5. Crash due to keyDispatchingTimedOut 6. Not possible to scan for other devices or making your phone discoverable again 7. Restart necessary Tell tale sign: 07-25 16:37:12.240 E/ActivityManager( 262): ANR in com.android.settings (com.android.settings/.bluetooth.BluetoothSettings) 07-25 16:37:12.240 E/ActivityManager( 262): Reason: keyDispatchingTimedOut Test case to verify this patch: android.bluetooth.BluetoothStressTest#testDiscoverable Change-Id: I7696b5722805e85cd0204ce2597e91594cbe6789
1 parent d144748 commit f4c6404

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

core/jni/android_server_BluetoothEventLoop.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ static void tearDownEventLoop(native_data_t *nat) {
428428
#define EVENT_LOOP_EXIT 1
429429
#define EVENT_LOOP_ADD 2
430430
#define EVENT_LOOP_REMOVE 3
431+
#define EVENT_LOOP_WAKEUP 4
431432

432433
dbus_bool_t dbusAddWatch(DBusWatch *watch, void *data) {
433434
native_data_t *nat = (native_data_t *)data;
@@ -472,6 +473,13 @@ void dbusToggleWatch(DBusWatch *watch, void *data) {
472473
}
473474
}
474475

476+
void dbusWakeup(void *data) {
477+
native_data_t *nat = (native_data_t *)data;
478+
479+
char control = EVENT_LOOP_WAKEUP;
480+
write(nat->controlFdW, &control, sizeof(char));
481+
}
482+
475483
static void handleWatchAdd(native_data_t *nat) {
476484
DBusWatch *watch;
477485
int newFD;
@@ -555,6 +563,7 @@ static void *eventLoopMain(void *ptr) {
555563

556564
dbus_connection_set_watch_functions(nat->conn, dbusAddWatch,
557565
dbusRemoveWatch, dbusToggleWatch, ptr, NULL);
566+
dbus_connection_set_wakeup_main_function(nat->conn, dbusWakeup, ptr, NULL);
558567

559568
nat->running = true;
560569

@@ -590,6 +599,11 @@ static void *eventLoopMain(void *ptr) {
590599
handleWatchRemove(nat);
591600
break;
592601
}
602+
case EVENT_LOOP_WAKEUP:
603+
{
604+
// noop
605+
break;
606+
}
593607
}
594608
}
595609
} else {

0 commit comments

Comments
 (0)