Avoid running RegisteredServicesCache tasks on main thread

Since RegisteredServicesCache tasks from SyncManager and AccountManager
run on the main thread of SystemServer, It could cause draining
the main thread and end up with ANR when receiving a lot of broadcasts
or notifying changes through a listener.

This CL changes a few things to avoid running it on main thread.
1. RegisteredServicesCache uses BackgroundThread by default for
receiving broadcasts and a listener, instead of main thread.
2. AccountManager uses its own handler for a listener.

Bug: 171907687
Test: atest SyncManagerTest CtsSyncManagerTest AccountManagerServiceTest
Change-Id: I838c15b797b46fd58bb3c6ce9ee444a4737775d8
This commit is contained in:
Jeongsik Mun
2021-01-05 16:51:28 +09:00
committed by Todd Kennedy
parent 9995d6ca51
commit 8f35d505ba
2 changed files with 8 additions and 5 deletions

View File

@@ -42,6 +42,7 @@ import android.util.Xml;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.ArrayUtils;
import libcore.io.IoUtils;
@@ -161,18 +162,20 @@ public abstract class RegisteredServicesCache<V> {
intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package");
mContext.registerReceiverAsUser(mPackageReceiver, UserHandle.ALL, intentFilter, null, null);
Handler handler = BackgroundThread.getHandler();
mContext.registerReceiverAsUser(
mPackageReceiver, UserHandle.ALL, intentFilter, null, handler);
// Register for events related to sdcard installation.
IntentFilter sdFilter = new IntentFilter();
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
mContext.registerReceiver(mExternalReceiver, sdFilter);
mContext.registerReceiver(mExternalReceiver, sdFilter, null, handler);
// Register for user-related events
IntentFilter userFilter = new IntentFilter();
sdFilter.addAction(Intent.ACTION_USER_REMOVED);
mContext.registerReceiver(mUserRemovedReceiver, userFilter);
mContext.registerReceiver(mUserRemovedReceiver, userFilter, null, handler);
}
private void handlePackageEvent(Intent intent, int userId) {
@@ -265,7 +268,7 @@ public abstract class RegisteredServicesCache<V> {
public void setListener(RegisteredServicesCacheListener<V> listener, Handler handler) {
if (handler == null) {
handler = new Handler(mContext.getMainLooper());
handler = BackgroundThread.getHandler();
}
synchronized (this) {
mHandler = handler;