Ensure SerialExecutor has no more pending tasks.
* We need to ensure this is the case because we might shutdown
SystemAlarmService without actually completing all the tasks.
Test: Existing unit tests pass. Ran integration tests.
Fixes: b/138238197
Change-Id: I72fe2ddb1607995721eae320ce41accad93fee8f
diff --git a/work/workmanager/src/main/java/androidx/work/impl/background/systemalarm/SystemAlarmDispatcher.java b/work/workmanager/src/main/java/androidx/work/impl/background/systemalarm/SystemAlarmDispatcher.java
index 548da36..5ddb288 100644
--- a/work/workmanager/src/main/java/androidx/work/impl/background/systemalarm/SystemAlarmDispatcher.java
+++ b/work/workmanager/src/main/java/androidx/work/impl/background/systemalarm/SystemAlarmDispatcher.java
@@ -32,6 +32,7 @@
import androidx.work.impl.ExecutionListener;
import androidx.work.impl.Processor;
import androidx.work.impl.WorkManagerImpl;
+import androidx.work.impl.utils.SerialExecutor;
import androidx.work.impl.utils.WakeLocks;
import androidx.work.impl.utils.taskexecutor.TaskExecutor;
@@ -221,8 +222,11 @@
}
mCurrentIntent = null;
}
+ SerialExecutor serialExecutor = mTaskExecutor.getBackgroundExecutor();
+ if (!mCommandHandler.hasPendingCommands()
+ && mIntents.isEmpty()
+ && !serialExecutor.hasPendingTasks()) {
- if (!mCommandHandler.hasPendingCommands() && mIntents.isEmpty()) {
// If there are no more intents to process, and the command handler
// has no more pending commands, stop the service.
Logger.get().debug(TAG, "No more commands & intents.");
diff --git a/work/workmanager/src/main/java/androidx/work/impl/utils/SerialExecutor.java b/work/workmanager/src/main/java/androidx/work/impl/utils/SerialExecutor.java
index e48ef4a..982c865 100644
--- a/work/workmanager/src/main/java/androidx/work/impl/utils/SerialExecutor.java
+++ b/work/workmanager/src/main/java/androidx/work/impl/utils/SerialExecutor.java
@@ -57,6 +57,15 @@
}
}
+ /**
+ * @return {@code true} if there are tasks to execute in the queue.
+ */
+ public boolean hasPendingTasks() {
+ synchronized (mLock) {
+ return !mTasks.isEmpty();
+ }
+ }
+
@NonNull
@VisibleForTesting
public Executor getDelegatedExecutor() {