Set timeout for binding service to avoid thread blocking
In the case of catching AssertionError, such as in Bug: 351757602, this fix can prevent android.bg thread blockingļ¼In other cases where AssertionError is not catched, this fix can expose the problem as early as possible instead of waiting for the thread to block
Bug: 351757602
Test: Manual
Change-Id: Id2a2a960b28c77532e05c88ce9267762610f9119
diff --git a/keystore/java/android/security/KeyChain.java b/keystore/java/android/security/KeyChain.java
index 4b367e0..f9fd369 100644
--- a/keystore/java/android/security/KeyChain.java
+++ b/keystore/java/android/security/KeyChain.java
@@ -66,6 +66,7 @@
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.security.auth.x500.X500Principal;
@@ -376,6 +377,8 @@
*/
public static final int KEY_ATTESTATION_FAILURE = 4;
+ private static final int BIND_KEY_CHAIN_SERVICE_TIMEOUT_MS = 30 * 1000;
+
/**
* Used by DPC or delegated app in
* {@link android.app.admin.DeviceAdminReceiver#onChoosePrivateKeyAlias} or
@@ -1120,7 +1123,10 @@
context.unbindService(keyChainServiceConnection);
throw new AssertionError("could not bind to KeyChainService");
}
- countDownLatch.await();
+ if (!countDownLatch.await(BIND_KEY_CHAIN_SERVICE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
+ context.unbindService(keyChainServiceConnection);
+ throw new AssertionError("binding to KeyChainService timeout");
+ }
IKeyChainService service = keyChainService.get();
if (service != null) {
return new KeyChainConnection(context, keyChainServiceConnection, service);