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

Commit d46f12bf authored by riddle_hsu's avatar riddle_hsu Committed by Steve Kondik
Browse files

Avoid deadlock between ActivityManagerService and ProcessStatsService.

Root Cause:
Case 1
getStatsOverTime(#483) // lock mWriteLock, wait AMS
async performWriteState(#269) // lock mPendingWriteLock, wait mWriteLock
writeStateLocked(#218) // lock AMS, wait mPendingWriteLock

Case 2
getStatsOverTime(#483) // lock mWriteLock, wait AMS
writeStateSyncLocked,writeStateLocked(#269) // lock AMS, wait mWriteLock

Solution:
Reduce nested lock.

Others also post the same solution:
I437a5cedceb34292a6bd1d9e7610f52b1478e424

Change-Id: Ie9395f3f6359fe59e2282674fcfec9d123e53f25
parent b98529ea
Loading
Loading
Loading
Loading
+16 −16
Original line number Diff line number Diff line
@@ -445,14 +445,14 @@ public final class ProcessStatsService extends IProcessStats.Stub {
        mAm.mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.PACKAGE_USAGE_STATS, null);
        Parcel current = Parcel.obtain();
        mWriteLock.lock();
        try {
        synchronized (mAm) {
            long now = SystemClock.uptimeMillis();
            mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
            mProcessStats.mTimePeriodEndUptime = now;
            mProcessStats.writeToParcel(current, now, 0);
        }
        mWriteLock.lock();
        try {
            if (historic != null) {
                ArrayList<String> files = getCommittedFiles(0, false, true);
                if (files != null) {
@@ -476,8 +476,6 @@ public final class ProcessStatsService extends IProcessStats.Stub {
    public ParcelFileDescriptor getStatsOverTime(long minTime) {
        mAm.mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.PACKAGE_USAGE_STATS, null);
        mWriteLock.lock();
        try {
        Parcel current = Parcel.obtain();
        long curTime;
        synchronized (mAm) {
@@ -488,6 +486,8 @@ public final class ProcessStatsService extends IProcessStats.Stub {
            curTime = mProcessStats.mTimePeriodEndRealtime
                    - mProcessStats.mTimePeriodStartRealtime;
        }
        mWriteLock.lock();
        try {
            if (curTime < minTime) {
                // Need to add in older stats to reach desired time.
                ArrayList<String> files = getCommittedFiles(0, false, true);