summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2024-07-26 17:54:16 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2024-07-26 17:54:16 +0000
commitecec0dcaec2f310f53b1d6b70410820de81f4286 (patch)
tree3b72da52a20cb0b144ade42c175927d652453466
parentba12cb52c664beebd9bd8fa12ad27d696ac41fd1 (diff)
parentac689e23cbe17695d516c4cc8c8f16a4389b16cd (diff)
Merge "Fix race condition between service restart and unbind." into main
-rw-r--r--core/tests/coretests/src/android/app/activity/ServiceTest.java15
-rw-r--r--services/core/java/com/android/server/am/ActiveServices.java3
2 files changed, 17 insertions, 1 deletions
diff --git a/core/tests/coretests/src/android/app/activity/ServiceTest.java b/core/tests/coretests/src/android/app/activity/ServiceTest.java
index 3f3d6a3bff34..4e3b2af919e9 100644
--- a/core/tests/coretests/src/android/app/activity/ServiceTest.java
+++ b/core/tests/coretests/src/android/app/activity/ServiceTest.java
@@ -157,6 +157,21 @@ public class ServiceTest extends TestCase {
assertThat(mCurrentConnection.takePid(), is(NOT_STARTED));
}
+ @Test
+ public void testRestart_stickyStartedService_unbindHappenedAfterRestart_restarted() {
+ final int servicePid = startService(Service.START_STICKY);
+ assertThat(servicePid, not(NOT_STARTED));
+ assertThat(bindService(0 /* flags */), is(servicePid));
+
+ final int restartedServicePid = waitForServiceStarted(
+ () -> {
+ Process.killProcess(servicePid);
+ mContext.unbindService(mCurrentConnection);
+ mCurrentConnection = null;
+ });
+ assertThat(restartedServicePid, not(NOT_STARTED));
+ }
+
/** @return The pid of the started service. */
private int startService(int code) {
return waitForServiceStarted(
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 0fa5260a2e89..2e1416b2887b 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -6653,9 +6653,10 @@ public final class ActiveServices {
// If unbound while waiting to start and there is no connection left in this service,
// remove the pending service
- if (s.getConnections().isEmpty()) {
+ if (s.getConnections().isEmpty() && !s.startRequested) {
mPendingServices.remove(s);
mPendingBringups.remove(s);
+ if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Removed pending service: " + s);
}
if (c.hasFlag(Context.BIND_AUTO_CREATE)) {