From 17bb873156593f00be82a4831e7d3279f2823671 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Mon, 24 May 2021 10:00:23 -0600 Subject: [PATCH] CloseGuard for more Bluetooth components. We've seen evidence of IBluetoothProfileServiceConnection and IBluetoothStateChangeCallback references being leaked, so attempt to unregister them when an object is finalized without closing. Bug: 189091551 Test: manual Change-Id: I23792d48d94578acd7fc7a5164a95171801ee721 --- core/java/android/bluetooth/BluetoothHeadset.java | 12 ++++++++++++ .../android/bluetooth/BluetoothProfileConnector.java | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/java/android/bluetooth/BluetoothHeadset.java b/core/java/android/bluetooth/BluetoothHeadset.java index 3bf517c046f35..5be8326949904 100644 --- a/core/java/android/bluetooth/BluetoothHeadset.java +++ b/core/java/android/bluetooth/BluetoothHeadset.java @@ -39,6 +39,7 @@ import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.os.RemoteException; +import android.util.CloseGuard; import android.util.Log; import java.util.ArrayList; @@ -338,6 +339,8 @@ public final class BluetoothHeadset implements BluetoothProfile { private static final int MESSAGE_HEADSET_SERVICE_CONNECTED = 100; private static final int MESSAGE_HEADSET_SERVICE_DISCONNECTED = 101; + private final CloseGuard mCloseGuard = new CloseGuard(); + private Context mContext; private ServiceListener mServiceListener; private volatile IBluetoothHeadset mService; @@ -385,6 +388,7 @@ public final class BluetoothHeadset implements BluetoothProfile { } doBind(); + mCloseGuard.open("close"); } private boolean doBind() { @@ -438,6 +442,14 @@ public final class BluetoothHeadset implements BluetoothProfile { } mServiceListener = null; doUnbind(); + mCloseGuard.close(); + } + + /** {@hide} */ + @Override + protected void finalize() throws Throwable { + mCloseGuard.warnIfOpen(); + close(); } /** diff --git a/core/java/android/bluetooth/BluetoothProfileConnector.java b/core/java/android/bluetooth/BluetoothProfileConnector.java index beff841f2fc57..a254291f57dbc 100644 --- a/core/java/android/bluetooth/BluetoothProfileConnector.java +++ b/core/java/android/bluetooth/BluetoothProfileConnector.java @@ -26,6 +26,7 @@ import android.os.Build; import android.os.IBinder; import android.os.RemoteException; import android.os.UserHandle; +import android.util.CloseGuard; import android.util.Log; /** @@ -36,6 +37,7 @@ import android.util.Log; */ @SuppressLint("AndroidFrameworkBluetoothPermission") public abstract class BluetoothProfileConnector { + private final CloseGuard mCloseGuard = new CloseGuard(); private final int mProfileId; private BluetoothProfile.ServiceListener mServiceListener; private final BluetoothProfile mProfileProxy; @@ -82,11 +84,19 @@ public abstract class BluetoothProfileConnector { mServiceName = serviceName; } + /** {@hide} */ + @Override + public void finalize() { + mCloseGuard.warnIfOpen(); + doUnbind(); + } + @SuppressLint("AndroidFrameworkRequiresPermission") private boolean doBind() { synchronized (mConnection) { if (mService == null) { logDebug("Binding service..."); + mCloseGuard.open("doUnbind"); try { Intent intent = new Intent(mServiceName); ComponentName comp = intent.resolveSystemService( @@ -110,6 +120,7 @@ public abstract class BluetoothProfileConnector { synchronized (mConnection) { if (mService != null) { logDebug("Unbinding service..."); + mCloseGuard.close(); try { mContext.unbindService(mConnection); } catch (IllegalArgumentException ie) {