From dcd69b994e269331d06287cd97ef81f81e42a02c Mon Sep 17 00:00:00 2001 From: Suprabh Shukla Date: Thu, 6 Mar 2025 01:17:48 -0800 Subject: Release wakelock only when no alarms are in-flight Fix the bug where the alarm wakelock could be released before all alarms completed. Test: atest FrameworksMockingServicesTests:com.android.server.alarm Flag: com.android.server.alarm.acquire_wakelock_before_send Fixes: 400058421 Change-Id: I86210548b9a8fc6050577ca1f7e973b127c47862 --- .../java/com/android/server/alarm/AlarmManagerService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'apex') diff --git a/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java b/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java index 251776e907d8..44e4999ccf44 100644 --- a/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java +++ b/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java @@ -5369,7 +5369,9 @@ public class AlarmManagerService extends SystemService { // to do any wakelock or stats tracking, so we have nothing // left to do here but go on to the next thing. mSendFinishCount++; - if (Flags.acquireWakelockBeforeSend()) { + if (Flags.acquireWakelockBeforeSend() && mBroadcastRefCount == 0) { + // No other alarms are in-flight and this dispatch failed. We will + // acquire the wakelock again before the next dispatch. mWakeLock.release(); } return; @@ -5409,7 +5411,9 @@ public class AlarmManagerService extends SystemService { // stats management to do. It threw before we posted the delayed // timeout message, so we're done here. mListenerFinishCount++; - if (Flags.acquireWakelockBeforeSend()) { + if (Flags.acquireWakelockBeforeSend() && mBroadcastRefCount == 0) { + // No other alarms are in-flight and this dispatch failed. We will + // acquire the wakelock again before the next dispatch. mWakeLock.release(); } return; -- cgit v1.2.3-59-g8ed1b