From 8d6a7a10e6b64c6b84fdf560f2840d1c43a49870 Mon Sep 17 00:00:00 2001 From: Alejandro Nijamkin Date: Mon, 31 Oct 2022 09:04:09 -0700 Subject: [PATCH] Blind fix for user switcher instant crash. There is an instant crash stemming from a premature optimization done in the newly refactored code for the user switcher system. I am unable to reproduce this locally, so this is a blind fix. What follows is the rationale behind it. It appears to be a race condition. UserRepositoryImpl#isSimpleUserSwitcher asserts that we have loaded the settings but the settings are only loaded in a background coroutine job that's kicked off when the repository class is instantiated. From the looks of it, the former path is triggered before the latter is. The fix, therefore, is to always make sure that there is a non-null value for user settings. This is achieved in this CL using an expensive runBlocking call at class instantiation time. Delaying that was an optimization that attempted to do better than the original pre-refactor code, where the same approach is taken (settings are accessed on the main thread in a blocking fashion). Fix: 255916597, 254764031 Test: unable to reproduce locally but made sure that, with the new changes, there is no instant crash nor a crash when entering the full-screen user switcher on one of the affected device models - used adb reboot to trigger restarts. Change-Id: I970a416ae7eaf2d2a366b12b3d7c6b98ebc52011 --- .../android/systemui/user/data/repository/UserRepository.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt b/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt index b16dc5403a572..6a2326036ec09 100644 --- a/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt @@ -62,6 +62,7 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext /** @@ -136,7 +137,7 @@ constructor( private val isNewImpl: Boolean get() = !featureFlags.isEnabled(Flags.USER_INTERACTOR_AND_REPO_USE_CONTROLLER) - private val _userSwitcherSettings = MutableStateFlow(null) + private val _userSwitcherSettings = MutableStateFlow(runBlocking { getSettings() }) override val userSwitcherSettings: Flow = _userSwitcherSettings.asStateFlow().filterNotNull() @@ -235,7 +236,7 @@ constructor( } override fun isSimpleUserSwitcher(): Boolean { - return checkNotNull(_userSwitcherSettings.value?.isSimpleUserSwitcher) + return _userSwitcherSettings.value.isSimpleUserSwitcher } private fun observeSelectedUser() {