Close leftover Audio-to-Bluetooth HAL connections before reopening
If the Audio-to-Bluetooth HAL reopens a connection on the Data or
Control channel without closing first the old one, the UIPC select(2)
loop gets confused and could spin with 100% CPU usage.
Now we check for channels that are reopened, and explicitly close
them before reopening.
Also, fix a check whether a file descriptor is valid: 0 is a valid
file descriptor.
Test: A2DP testing with a headset: connect/play/stop
Bug: 35394629
Change-Id: I7ae988ac77f25da56e59e347363e7c473e7f69a9
diff --git a/udrv/ulinux/uipc.cc b/udrv/ulinux/uipc.cc
index c843ea7..bb7f8e5 100644
--- a/udrv/ulinux/uipc.cc
+++ b/udrv/ulinux/uipc.cc
@@ -272,11 +272,19 @@
if (SAFE_FD_ISSET(uipc_main.ch[ch_id].srvfd, &uipc_main.read_set)) {
BTIF_TRACE_EVENT("INCOMING CONNECTION ON CH %d", ch_id);
+ // Close the previous connection
+ if (uipc_main.ch[ch_id].fd != UIPC_DISCONNECTED) {
+ BTIF_TRACE_EVENT("CLOSE CONNECTION (FD %d)", uipc_main.ch[ch_id].fd);
+ close(uipc_main.ch[ch_id].fd);
+ FD_CLR(uipc_main.ch[ch_id].fd, &uipc_main.active_set);
+ uipc_main.ch[ch_id].fd = UIPC_DISCONNECTED;
+ }
+
uipc_main.ch[ch_id].fd = accept_server_socket(uipc_main.ch[ch_id].srvfd);
BTIF_TRACE_EVENT("NEW FD %d", uipc_main.ch[ch_id].fd);
- if ((uipc_main.ch[ch_id].fd > 0) && uipc_main.ch[ch_id].cback) {
+ if ((uipc_main.ch[ch_id].fd >= 0) && uipc_main.ch[ch_id].cback) {
/* if we have a callback we should add this fd to the active set
and notify user with callback event */
BTIF_TRACE_EVENT("ADD FD %d TO ACTIVE SET", uipc_main.ch[ch_id].fd);