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

Commit d8ef1169 authored by Chris Wailes's avatar Chris Wailes
Browse files

Improved timekeeping logic for USAP Pool refill mechanism.

This patch makes two changes to increase the reliability of the USAP
pool refill mechanism.  The first is to change the storage type of the
result of an expression from int to long.  This will help avoid
potential overflow issues.

The second change mitigates the impact of the non-monotonic system clock
having its value changed.  If the clock goes backwards in time past when
the initial event triggered the refill delay, the delay will now be reset.

Bug: 146614089
Test: Treehugger
Change-Id: Ic6f65de51757803b5ba95975bab3b9322a91c837
Merged-In: Ic6f65de51757803b5ba95975bab3b9322a91c837
parent b28db4ab
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -490,16 +490,23 @@ class ZygoteServer {
            if (mUsapPoolRefillTriggerTimestamp == INVALID_TIMESTAMP) {
                pollTimeoutMs = -1;
            } else {
                int elapsedTimeMs =
                        (int) (System.currentTimeMillis() - mUsapPoolRefillTriggerTimestamp);
                long elapsedTimeMs = System.currentTimeMillis() - mUsapPoolRefillTriggerTimestamp;

                if (elapsedTimeMs >= mUsapPoolRefillDelayMs) {
                    // Normalize the poll timeout value when the time between one poll event and the
                    // next pushes us over the delay value.  This prevents poll receiving a 0
                    // timeout value, which would result in it returning immediately.
                    pollTimeoutMs = -1;

                } else if (elapsedTimeMs <= 0) {
                    // This can occur if the clock used by currentTimeMillis is reset, which is
                    // possible because it is not guaranteed to be monotonic.  Because we can't tell
                    // how far back the clock was set the best way to recover is to simply re-start
                    // the respawn delay countdown.
                    pollTimeoutMs = mUsapPoolRefillDelayMs;

                } else {
                    pollTimeoutMs = mUsapPoolRefillDelayMs - elapsedTimeMs;
                    pollTimeoutMs = (int) (mUsapPoolRefillDelayMs - elapsedTimeMs);
                }
            }