OSDN Git Service

BatteryStats: Don't schedule work when shutting down
authorAdam Lesinski <adamlesinski@google.com>
Wed, 30 Aug 2017 02:30:31 +0000 (19:30 -0700)
committerAdam Lesinski <adamlesinski@google.com>
Wed, 30 Aug 2017 16:58:59 +0000 (16:58 +0000)
Prevent more work from being queued when BatteryStats is
shutting down.

Bug: 64901294
Test: manual
Change-Id: Ifc74a4405b40c949e440d6d65fdd13ec40920429

services/core/java/com/android/server/am/BatteryExternalStatsWorker.java

index eb84adc..118484b 100644 (file)
@@ -38,6 +38,7 @@ import com.android.internal.os.BatteryStatsImpl;
 
 import libcore.util.EmptyArray;
 
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -116,6 +117,10 @@ class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStatsSync {
     }
 
     public synchronized Future<?> scheduleWrite() {
+        if (mExecutorService.isShutdown()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("worker shutdown"));
+        }
+
         scheduleSyncLocked("write", UPDATE_ALL);
         // Since we use a single threaded executor, we can assume the next scheduled task's
         // Future finishes after the sync.
@@ -127,7 +132,9 @@ class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStatsSync {
      * within the task, never wait on the resulting Future. This will result in a deadlock.
      */
     public synchronized void scheduleRunnable(Runnable runnable) {
-        mExecutorService.submit(runnable);
+        if (!mExecutorService.isShutdown()) {
+            mExecutorService.submit(runnable);
+        }
     }
 
     public void shutdown() {
@@ -135,6 +142,10 @@ class BatteryExternalStatsWorker implements BatteryStatsImpl.ExternalStatsSync {
     }
 
     private Future<?> scheduleSyncLocked(String reason, int flags) {
+        if (mExecutorService.isShutdown()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("worker shutdown"));
+        }
+
         if (mCurrentFuture == null) {
             mUpdateFlags = flags;
             mCurrentReason = reason;