Unregister the tethering internal callback in finalize

Bug: 177265744
Bug: 191798390
Bug: 187972579
Test: atest TetheringServiceTest
Change-Id: Ie7f9535b923db5073a59329ead22546a54e6ef47
This commit is contained in:
markchien
2021-09-29 00:30:20 +08:00
committed by Mark Chien
parent e7b4a505aa
commit 819e19ea2a
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) {