From 1f33110f50f502a8356025471d528a825145ec75 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Tue, 5 Jan 2021 13:10:35 -0800 Subject: [PATCH] Don't cache null in getSystemService() When Context.getSystemService(SERVICE_NAME) returns null, previously, we remembered it and would always return null for the same service. Now we stop doing it and always retry in the next call. Bug: 175626705 Test: boot Change-Id: I458e59fe2efc6278ff27d821f6ef516a48ded443 --- core/java/android/app/SystemServiceRegistry.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index 48d2dfe5cca90..ae1c89468b183 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -1719,7 +1719,7 @@ public final class SystemServiceRegistry { synchronized (cache) { // Return it if we already have a cached instance. T service = (T) cache[mCacheIndex]; - if (service != null || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) { + if (service != null) { ret = service; break; // exit the for (;;) } @@ -1729,7 +1729,9 @@ public final class SystemServiceRegistry { // Grr... if gate is STATE_READY, then this means we initialized the service // once but someone cleared it. // We start over from STATE_UNINITIALIZED. - if (gates[mCacheIndex] == ContextImpl.STATE_READY) { + // Similarly, if the previous attempt returned null, we'll retry again. + if (gates[mCacheIndex] == ContextImpl.STATE_READY + || gates[mCacheIndex] == ContextImpl.STATE_NOT_FOUND) { gates[mCacheIndex] = ContextImpl.STATE_UNINITIALIZED; }