From 94ec46f82f5813fd5bcdf6c2f562e009dd4eec03 Mon Sep 17 00:00:00 2001 From: Svet Ganov Date: Fri, 8 Jun 2018 15:03:46 -0700 Subject: [PATCH] Handle race between UID state and activty resume There is a race between the UID turning active and activity being resume. The proper fix is very risky, so we temporary add some polling which should hapen pretty rarely anyway as the race is hard to hit. Test: camera works fine cts-tradefed run cts-dev -m CtsCameraTestCases bug:77827041 Change-Id: I1768d00dc08e6e49cec0099f0a29b7889f9affad --- .../camera/libcameraservice/CameraService.cpp | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp index 282871bc44..c41de8286c 100644 --- a/services/camera/libcameraservice/CameraService.cpp +++ b/services/camera/libcameraservice/CameraService.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -2433,6 +2434,8 @@ bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) { return isUidActiveLocked(uid, callingPackage); } +static const int kPollUidActiveTimeoutMillis = 50; + bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) { // Non-app UIDs are considered always active // If activity manager is unreachable, assume everything is active @@ -2452,7 +2455,28 @@ bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPack ActivityManager am; // Okay to access with a lock held as UID changes are dispatched without // a lock and we are a higher level component. - active = am.isUidActive(uid, callingPackage); + int64_t startTimeMillis = 0; + do { + // TODO: Fix this b/109950150! + // Okay this is a hack. There is a race between the UID turning active and + // activity being resumed. The proper fix is very risky, so we temporary add + // some polling which should happen pretty rarely anyway as the race is hard + // to hit. + active = am.isUidActive(uid, callingPackage); + if (active) { + break; + } + if (startTimeMillis <= 0) { + startTimeMillis = uptimeMillis(); + } + int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis; + int64_t remainingTimeMillis = kPollUidActiveTimeoutMillis - ellapsedTimeMillis; + if (remainingTimeMillis <= 0) { + break; + } + usleep(remainingTimeMillis * 1000); + } while (true); + if (active) { // Now that we found out the UID is actually active, cache that mActiveUids.insert(uid);