Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c4f28a41 authored by Shibin George's avatar Shibin George
Browse files

Fix deadlock in MediaSessionService during getCurrentUser()

SystemServiceMAnager.stopUser() acquires ActivityManagerService
lock first and then acquires MediaSessionService.mLock during
onStopUser(). MediaSessionService.onSwitchUser(), on the other
hand, acquires mLock first and ActivityManagerService lock next.
This is a potential deadlock situation which can be resolved by
moving getCurrentUser() out of synchronized block. This will
release mLock sooner and avoid the deadlock.

https://code.google.com/p/android/issues/detail?id=214469



Change-Id: Ieb4520d60af6ca38496ca933da0cd9ae096a9408
Signed-off-by: default avatarShibin George <shibing@codeaurora.org>
parent 326e55a3
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -227,8 +227,8 @@ public class MediaSessionService extends SystemService implements Monitor {
    }

    private void updateUser() {
        synchronized (mLock) {
        int userId = ActivityManager.getCurrentUser();
        synchronized (mLock) {
            if (mCurrentUserId != userId) {
                final int oldUserId = mCurrentUserId;
                mCurrentUserId = userId; // do this first
@@ -764,7 +764,7 @@ public class MediaSessionService extends SystemService implements Monitor {
                synchronized (mLock) {
                    // If we don't have a media button receiver to fall back on
                    // include non-playing sessions for dispatching
                    UserRecord ur = mUserRecords.get(ActivityManager.getCurrentUser());
                    UserRecord ur = mUserRecords.get(mCurrentUserId);
                    boolean useNotPlayingSessions = (ur == null) ||
                            (ur.mLastMediaButtonReceiver == null
                                && ur.mRestoredMediaButtonReceiver == null);
@@ -957,8 +957,7 @@ public class MediaSessionService extends SystemService implements Monitor {
                        mKeyEventReceiver);
            } else {
                // Launch the last PendingIntent we had with priority
                int userId = ActivityManager.getCurrentUser();
                UserRecord user = mUserRecords.get(userId);
                UserRecord user = mUserRecords.get(mCurrentUserId);
                if (user.mLastMediaButtonReceiver != null
                        || user.mRestoredMediaButtonReceiver != null) {
                    if (DEBUG) {
@@ -979,7 +978,7 @@ public class MediaSessionService extends SystemService implements Monitor {
                        } else {
                            mediaButtonIntent.setComponent(user.mRestoredMediaButtonReceiver);
                            getContext().sendBroadcastAsUser(mediaButtonIntent,
                                    new UserHandle(userId));
                                    new UserHandle(mCurrentUserId));
                        }
                    } catch (CanceledException e) {
                        Log.i(TAG, "Error sending key event to media button receiver "