Merge "Unregister the tethering internal callback in finalize" am: f8d1f3d1cd

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1838543

Change-Id: I3523da57b7d0feac11b2cdf62f254e694d5b7269
This commit is contained in:
Treehugger Robot
2021-10-20 08:57:57 +00:00
committed by Automerger Merge Worker
2 changed files with 18 additions and 2 deletions

View File

@@ -290,6 +290,23 @@ public class TetheringManager {
getConnector(c -> c.registerTetheringEventCallback(mCallback, pkgName));
}
/** @hide */
@Override
protected void finalize() throws Throwable {
final String pkgName = mContext.getOpPackageName();
Log.i(TAG, "unregisterTetheringEventCallback:" + pkgName);
// 1. It's generally not recommended to perform long operations in finalize, but while
// unregisterTetheringEventCallback does an IPC, it's a oneway IPC so should not block.
// 2. If the connector is not yet connected, TetheringManager is impossible to finalize
// because the connector polling thread strong reference the TetheringManager object. So
// it's guaranteed that registerTetheringEventCallback was already called before calling
// unregisterTetheringEventCallback in finalize.
if (mConnector == null) Log.wtf(TAG, "null connector in finalize!");
getConnector(c -> c.unregisterTetheringEventCallback(mCallback, pkgName));
super.finalize();
}
private void startPollingForConnector() {
new Thread(() -> {
while (true) {

View File

@@ -577,8 +577,7 @@ public final class TetheringServiceTest {
assertNull("TetheringManager weak reference still not null after " + attempts
+ " attempts", weakTm.get());
// BUG: internal callback do not be unregistered after TetheringManager is GCed.
assertEquals(1, callbacks.size());
assertEquals("Internal callback is not unregistered", 0, callbacks.size());
});
}
}