Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b524e253 authored by Varun Shah's avatar Varun Shah
Browse files

Gate the FGS timeout ANR behavior.

When this new flag is enabled, allow time-limited foreground services to
start even after they hit the timeout and don't throw an ANR either if
the service isn't stopped after the grace period.

Bug: 339315145
Test: atest CtsFgsTimeoutTestCases
Flag: android.app.gate_fgs_timeout_anr_behavior
Change-Id: I1bbe5447ca2f0450ac709b14f6577bacd9cf4bad
parent 80f02617
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -50,3 +50,13 @@ flag {
         purpose: PURPOSE_BUGFIX
     }
}

flag {
     namespace: "backstage_power"
     name: "gate_fgs_timeout_anr_behavior"
     description: "Gate the new behavior where an ANR is thrown once an FGS times out."
     bug: "339315145"
     metadata {
         purpose: PURPOSE_BUGFIX
     }
}
+18 −3
Original line number Diff line number Diff line
@@ -2454,10 +2454,19 @@ public final class ActiveServices {
                                } else if (lastTimeOutAt > 0) {
                                    // Time limit was exhausted within the past 24 hours and the app
                                    // has not been in the TOP state since then, throw an exception.
                                    throw new ForegroundServiceStartNotAllowedException("Time limit"
                                            + " already exhausted for foreground service type "
                                    final String exceptionMsg = "Time limit already exhausted for"
                                            + " foreground service type "
                                            + ServiceInfo.foregroundServiceTypeToLabel(
                                                            foregroundServiceType));
                                                    foregroundServiceType);
                                    if (!android.app.Flags.gateFgsTimeoutAnrBehavior()) {
                                        throw new ForegroundServiceStartNotAllowedException(
                                                    exceptionMsg);
                                    } else {
                                        // Only throw an exception above while the new ANR behavior
                                        // is not gated, otherwise, reset the limit temporarily.
                                        Slog.wtf(TAG, exceptionMsg);
                                        fgsTypeInfo.reset();
                                    }
                                }
                            }
                        } else {
@@ -3943,6 +3952,12 @@ public final class ActiveServices {
                + ServiceInfo.foregroundServiceTypeToLabel(fgsType)
                + " did not stop within its timeout: " + sr.getComponentName();

        if (android.app.Flags.gateFgsTimeoutAnrBehavior()) {
            // Log a WTF instead of throwing an ANR while the new behavior is gated.
            Slog.wtf(TAG, reason);
            return;
        }

        final TimeoutRecord tr = TimeoutRecord.forFgsTimeout(reason);
        tr.mLatencyTracker.waitingOnAMSLockStarted();
        synchronized (mAm) {