From 0e0842f3d146f8497932f2c2d34152a3045987be Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Wed, 26 Jul 2023 14:31:59 +0000 Subject: [PATCH] Avoid killing task process after its activity was died Some tests may have the order: 1. removeTask in tearDown Schedule KILL_TASK_PROCESSES_TIMEOUT_MSG to wait for the activity to be destroyed. 2. forceStopPackage in tearDown The process is forced killed, but the scheduled timeout is not cleared. 3. Launch the app again in a short time If the timeout reaches before the activity attached to the new process, the process will be killed so the test is failed due to target activity not detected. Originally the pending-kill state is consumed (clear flag and kill task processes) only when the activity is destroyed. So this change ensures it is done by the general cleanup that is also called when the process died. Fix: 293185893 Fix: 293280011 Test: atest RecentTasksTest#testRemovePackageByName Change-Id: I8cf8d67cc7cd39b6cc8db91498dc901f494ae85e --- .../android/server/wm/ActivityTaskSupervisor.java | 15 +++++++++++++-- .../com/android/server/wm/RecentTasksTest.java | 7 +++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java b/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java index 5553600b403fb..7bb34b056cff1 100644 --- a/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java +++ b/services/core/java/com/android/server/wm/ActivityTaskSupervisor.java @@ -621,6 +621,13 @@ public class ActivityTaskSupervisor implements RecentTasks.Callbacks { mFinishingActivities.remove(r); stopWaitingForActivityVisible(r); + + final Task task = r.getTask(); + if (task != null && task.mKillProcessesOnDestroyed && task.getTopMostActivity() == r) { + // The activity is destroyed or its process is died, so cancel the pending kill. + task.mKillProcessesOnDestroyed = false; + removeTimeoutOfKillProcessesOnDestroyed(task); + } } /** There is no valid launch time, just stop waiting. */ @@ -1901,9 +1908,13 @@ public class ActivityTaskSupervisor implements RecentTasks.Callbacks { killTaskProcessesIfPossible(task); } + private void removeTimeoutOfKillProcessesOnDestroyed(Task task) { + mHandler.removeMessages(KILL_TASK_PROCESSES_TIMEOUT_MSG, task); + } + void killTaskProcessesOnDestroyedIfNeeded(Task task) { if (task == null || !task.mKillProcessesOnDestroyed) return; - mHandler.removeMessages(KILL_TASK_PROCESSES_TIMEOUT_MSG, task); + removeTimeoutOfKillProcessesOnDestroyed(task); killTaskProcessesIfPossible(task); } @@ -2776,7 +2787,7 @@ public class ActivityTaskSupervisor implements RecentTasks.Callbacks { } break; case KILL_TASK_PROCESSES_TIMEOUT_MSG: { final Task task = (Task) msg.obj; - if (task.mKillProcessesOnDestroyed) { + if (task.mKillProcessesOnDestroyed && task.hasActivity()) { Slog.i(TAG, "Destroy timeout of remove-task, attempt to kill " + task); killTaskProcessesIfPossible(task); } diff --git a/services/tests/wmtests/src/com/android/server/wm/RecentTasksTest.java b/services/tests/wmtests/src/com/android/server/wm/RecentTasksTest.java index 7cb58022c0e79..77d5908c63a54 100644 --- a/services/tests/wmtests/src/com/android/server/wm/RecentTasksTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/RecentTasksTest.java @@ -1038,6 +1038,13 @@ public class RecentTasksTest extends WindowTestsBase { top.setState(ActivityRecord.State.DESTROYING, "test"); top.destroyed("test"); assertFalse(task.mKillProcessesOnDestroyed); + + // If the process is died, the state should be cleared. + final Task lastTask = tasks.get(0); + lastTask.addChild(top); + lastTask.mKillProcessesOnDestroyed = true; + top.handleAppDied(); + assertFalse(lastTask.mKillProcessesOnDestroyed); } @Test