From 1536cc27b9ccfec2f1c456b37cea8858bb325775 Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Mon, 18 Sep 2017 16:43:30 -0700 Subject: [PATCH] Mark uid as non-idle/FG-entitled when launching for fg service During app launch the app is considered background unless there's an established reason for it to be otherwise, but the two-stage "start foreground service" sequence means that there was now a race condition between giving the app the opportunity to make the service launch in the foreground and the "hey this uid isn't foreground, we should shut things down. Now, during service launch we explicitly mark the uid as foreground- equivalent when the startForegroundService() sequence is underway. The install-then-launch sequence isn't strictly essential to demonstrate the race; but it happened to wind up inducing some broadcast traffic (and therefore an OOM-adjust sequence) at a conveniently inopportune time very early during process bringup for the fg service. Fixes: 65706471 Test: adb install -r ~/Downloads/sampleForegroundService.apk && adb shell am startforegroundservice -a myActionName android.support.mediacompat.foregroundservice Change-Id: I6f3b6f8fb808e9d830fe108c577047b1938452fc --- .../core/java/com/android/server/am/ActiveServices.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index e0cde72ffff28..90ad8a5d01311 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -2162,6 +2162,15 @@ public final class ActiveServices { } } + if (r.fgRequired) { + if (DEBUG_FOREGROUND_SERVICE) { + Slog.v(TAG, "Whitelisting " + UserHandle.formatUid(r.appInfo.uid) + + " for fg-service launch"); + } + mAm.tempWhitelistUidLocked(r.appInfo.uid, + SERVICE_START_FOREGROUND_TIMEOUT, "fg-service-launch"); + } + if (!mPendingServices.contains(r)) { mPendingServices.add(r); }