From 0c54aa175ed52c6c30a758bb527987513a308893 Mon Sep 17 00:00:00 2001 From: Yasin Kilicdere Date: Thu, 11 Aug 2022 15:50:29 +0100 Subject: [PATCH] Unregister user switch observer in ActivityManagerShellCommand. ActivityManagerShellCommand does not unregisterUserSwitchObserver after registerUserSwitchObserver, which leads to a leak of unnecessary callbacks. This CL adds unregistering in finally block. Bug: 242176088 Test: Manual test with adb and perfetto. Change-Id: I14d66a00c5dd3f881d393a386a2678835c99a900 --- .../am/ActivityManagerShellCommand.java | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java index 6609d4a91f21a..55471410cfd2c 100644 --- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java +++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java @@ -51,6 +51,7 @@ import android.app.IActivityManager; import android.app.IActivityTaskManager; import android.app.IStopUserCallback; import android.app.IUidObserver; +import android.app.IUserSwitchObserver; import android.app.KeyguardManager; import android.app.ProfilerInfo; import android.app.RemoteServiceException.CrashedByAdbException; @@ -1937,31 +1938,36 @@ final class ActivityManagerShellCommand extends ShellCommand { // Register switch observer. final CountDownLatch switchLatch = new CountDownLatch(1); - mInterface.registerUserSwitchObserver( - new UserSwitchObserver() { - @Override - public void onUserSwitchComplete(int newUserId) { - if (userId == newUserId) { - switchLatch.countDown(); - } - } - }, ActivityManagerShellCommand.class.getName()); - - // Switch. - boolean switched = mInterface.switchUser(userId); - if (!switched) { - // Switching failed, don't wait for the user switch observer. - return false; - } - - // Wait. + final IUserSwitchObserver userSwitchObserver = new UserSwitchObserver() { + @Override + public void onUserSwitchComplete(int newUserId) { + if (userId == newUserId) { + switchLatch.countDown(); + } + } + }; try { - switched = switchLatch.await(USER_OPERATION_TIMEOUT_MS, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - getErrPrintWriter().println("Error: Thread interrupted unexpectedly."); - } + mInterface.registerUserSwitchObserver(userSwitchObserver, + ActivityManagerShellCommand.class.getName()); - return switched; + // Switch. + boolean switched = mInterface.switchUser(userId); + if (!switched) { + // Switching failed, don't wait for the user switch observer. + return false; + } + + // Wait. + try { + switched = switchLatch.await(USER_OPERATION_TIMEOUT_MS, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + getErrPrintWriter().println("Error: Thread interrupted unexpectedly."); + } + + return switched; + } finally { + mInterface.unregisterUserSwitchObserver(userSwitchObserver); + } } int runSwitchUser(PrintWriter pw) throws RemoteException {