diff options
author | 2024-07-05 12:43:20 +0000 | |
---|---|---|
committer | 2024-07-12 02:38:41 +0000 | |
commit | 7daf9b1cceb18d91a417e4397d1e3746763a8c2e (patch) | |
tree | b91e9797ab0cc4c48f0ba0108d8a71936fa80400 | |
parent | b54b9b5251377e9a5ce31390c59d56cc82556841 (diff) |
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
-rw-r--r-- | keystore/java/android/security/KeyChain.java | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/keystore/java/android/security/KeyChain.java b/keystore/java/android/security/KeyChain.java index 4b367e0e5b4b..f9fd3694835e 100644 --- a/keystore/java/android/security/KeyChain.java +++ b/keystore/java/android/security/KeyChain.java @@ -66,6 +66,7 @@ import java.util.Collection; 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 final class KeyChain { */ 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 @@ public final class KeyChain { 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); |