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

Commit 832543bf authored by Jing Ji's avatar Jing Ji
Browse files

Iterate the pending service bringup list properly

As the pending runnables could alter the list.

Bug: 177523045
Bug: 168292935
Test: atest CtsAppTestCases:ActivityManagerTest
Change-Id: Ia6983b7d0ac7ada13a45dc4a216e45e34af98d98
parent fd763b65
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -1047,9 +1047,11 @@ public final class ActiveServices {

    @GuardedBy("mAm")
    void schedulePendingServiceStartLocked(String packageName, int userId) {
        for (int i = mPendingBringups.size() - 1; i >= 0; i--) {
        int totalPendings = mPendingBringups.size();
        for (int i = totalPendings - 1; i >= 0 && totalPendings > 0;) {
            final ServiceRecord r = mPendingBringups.keyAt(i);
            if (r.userId != userId || !TextUtils.equals(r.packageName, packageName)) {
                i--;
                continue;
            }
            final ArrayList<Runnable> curPendingBringups = mPendingBringups.valueAt(i);
@@ -1057,8 +1059,22 @@ public final class ActiveServices {
                for (int j = curPendingBringups.size() - 1; j >= 0; j--) {
                    curPendingBringups.get(j).run();
                }
                curPendingBringups.clear();
            }
            // Now, how many remaining ones we have after calling into above runnables
            final int curTotalPendings = mPendingBringups.size();
            // Don't call removeAt() here, as it could have been removed already by above runnables
            mPendingBringups.remove(r);
            if (totalPendings != curTotalPendings) {
                // Okay, within the above Runnable.run(), the mPendingBringups is altered.
                // Restart the loop, it won't call into those finished runnables
                // since we've cleared the curPendingBringups above.
                totalPendings = mPendingBringups.size();
                i = totalPendings - 1;
            } else {
                totalPendings = mPendingBringups.size();
                i--;
            }
            mPendingBringups.removeAt(i);
        }
    }