From 9a6ef1e94c2c7cd937a6af668b33988057042520 Mon Sep 17 00:00:00 2001 From: Wale Ogunwale Date: Tue, 2 Jun 2015 13:41:00 -0700 Subject: [PATCH] Always have a handler for PendingIntents sent in the system process It is possible for deadlocks to occur when sending/processing PendingIntents due to components like AMS holding global service locks when dispatching the broadcast without specifying an handler and the receiver calling back into AMS. We now set a default handler to process the broadcast on if we are in the system process and a handler wasn't specified. Bug: 19502993 Change-Id: Iccde32c6c1df997784155bc41ef39e52ee0f7243 --- core/java/android/app/ActivityThread.java | 4 ++++ core/java/android/app/PendingIntent.java | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 2a98b6c74585d..83cec455918e8 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -1638,6 +1638,10 @@ public final class ActivityThread { return sCurrentActivityThread; } + public static boolean isSystem() { + return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false; + } + public static String currentOpPackageName() { ActivityThread am = currentActivityThread(); return (am != null && am.getApplication() != null) diff --git a/core/java/android/app/PendingIntent.java b/core/java/android/app/PendingIntent.java index 2cfc1fa48f674..031854a6a69aa 100644 --- a/core/java/android/app/PendingIntent.java +++ b/core/java/android/app/PendingIntent.java @@ -25,11 +25,13 @@ import android.content.IIntentReceiver; import android.content.IIntentSender; import android.content.IntentSender; import android.os.Bundle; +import android.os.Looper; import android.os.RemoteException; import android.os.Handler; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; +import android.os.Process; import android.os.UserHandle; import android.util.AndroidException; @@ -206,10 +208,20 @@ public final class PendingIntent implements Parcelable { private int mResultCode; private String mResultData; private Bundle mResultExtras; + private static Handler sDefaultSystemHandler; FinishedDispatcher(PendingIntent pi, OnFinished who, Handler handler) { mPendingIntent = pi; mWho = who; - mHandler = handler; + if (handler == null && ActivityThread.isSystem()) { + // We assign a default handler for the system process to avoid deadlocks when + // processing receivers in various components that hold global service locks. + if (sDefaultSystemHandler == null) { + sDefaultSystemHandler = new Handler(Looper.getMainLooper()); + } + mHandler = sDefaultSystemHandler; + } else { + mHandler = handler; + } } public void performReceive(Intent intent, int resultCode, String data, Bundle extras, boolean serialized, boolean sticky, int sendingUser) {