From d781ff68f1b4eefebac0ddf3372f1c33c5153e4a Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Thu, 9 Jul 2020 20:13:30 +0000 Subject: [PATCH] Don't cache suspend_control service Caching a reference to the service can lead to a DeadObject if if the service was restarted. Additially we attempt to wait for the service if it is not immediately available. Bug: 160741383 Test: atest FrameworksCoreTests:KernelWakelockReaderTest Change-Id: If54d644a66bed8a0da6a2f5b34168cad8f954187 --- .../internal/os/KernelWakelockReader.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/core/java/com/android/internal/os/KernelWakelockReader.java b/core/java/com/android/internal/os/KernelWakelockReader.java index cffb0ad9fdb99..3d35d2fbaa826 100644 --- a/core/java/com/android/internal/os/KernelWakelockReader.java +++ b/core/java/com/android/internal/os/KernelWakelockReader.java @@ -152,20 +152,33 @@ public class KernelWakelockReader { } } + /** + * Attempt to wait for suspend_control service if not immediately available. + */ + private ISuspendControlService waitForSuspendControlService() throws ServiceNotFoundException { + final String name = "suspend_control"; + final int numRetries = 5; + for (int i = 0; i < numRetries; i++) { + mSuspendControlService = ISuspendControlService.Stub.asInterface( + ServiceManager.getService(name)); + if (mSuspendControlService != null) { + return mSuspendControlService; + } + } + throw new ServiceNotFoundException(name); + } + /** * On success, returns the updated stats from SystemSupend, else returns null. */ private KernelWakelockStats getWakelockStatsFromSystemSuspend( final KernelWakelockStats staleStats) { WakeLockInfo[] wlStats = null; - if (mSuspendControlService == null) { - try { - mSuspendControlService = ISuspendControlService.Stub.asInterface( - ServiceManager.getServiceOrThrow("suspend_control")); - } catch (ServiceNotFoundException e) { - Slog.wtf(TAG, "Required service suspend_control not available", e); - return null; - } + try { + mSuspendControlService = waitForSuspendControlService(); + } catch (ServiceNotFoundException e) { + Slog.wtf(TAG, "Required service suspend_control not available", e); + return null; } try {