From a857e65c53957f87036b7962750bbdece6012b53 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Tue, 5 Sep 2017 16:44:36 -0700 Subject: [PATCH] Fix issue #65359137: Apps with target API 26 can bypass background... ...limit introduced in Oreo by swiping away from the task list There was an edge case where, when restarting a service, if the process wasn't already running we would bring it up directly in the idle state and never go through the mechanism of force stopping services once an app goes idle. The fix for now is pretty targeted, detecting when we have a new uid that is immediately idle, and in that case doing the same thing we do when an existing uid goes idle (stopping any services in it). This means that in this flow you will get a glitch of the service starting and then immediately stopping when idle is applied at that point, but we do end up ultimately in the correct final state. Test: manual Bug: 65359137 Change-Id: I64c29975ad75e7d7390512bf59673d423df16cf2 --- .../java/com/android/server/am/ActivityManagerService.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index ae0910ce716b..dad312505ec9 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -22875,6 +22875,8 @@ public class ActivityManagerService extends IActivityManager.Stub requestPssAllProcsLocked(now, false, mProcessStats.isMemFactorLowered()); } + ArrayList becameIdle = null; + // Update from any uid changes. if (mLocalPowerManager != null) { mLocalPowerManager.startUidChanges(); @@ -22907,6 +22909,10 @@ public class ActivityManagerService extends IActivityManager.Stub } if (uidRec.idle && !uidRec.setIdle) { uidChange = UidRecord.CHANGE_IDLE; + if (becameIdle == null) { + becameIdle = new ArrayList<>(); + } + becameIdle.add(uidRec); } } else { if (uidRec.idle) { @@ -22938,6 +22944,14 @@ public class ActivityManagerService extends IActivityManager.Stub mLocalPowerManager.finishUidChanges(); } + if (becameIdle != null) { + // If we have any new uids that became idle this time, we need to make sure + // they aren't left with running services. + for (int i = becameIdle.size() - 1; i >= 0; i--) { + mServices.stopInBackgroundLocked(becameIdle.get(i).uid); + } + } + if (mProcessStats.shouldWriteNowLocked(now)) { mHandler.post(new Runnable() { @Override public void run() { -- 2.11.0