From b65da76a9c6380b12dadd012496041d1b7bb08ac Mon Sep 17 00:00:00 2001 From: Liran Binyamin Date: Wed, 21 Jun 2023 15:37:11 -0400 Subject: [PATCH] Use a SupervisorJob in BubbleDataRepository Using a SupervisorJob ensures that if child coroutines fail, the scope remains active and we can launch further coroutines in it. Also renamed the scope field to exclude io from it. Scopes should be tied to lifetime of objects, not to dispatchers Bug: 281083629 Test: Verified that bubbles are still persisted after adding new bubbles and reordering them. Change-Id: I38cc755d5496bfd636d8a4e26f93a26843b0508a --- .../com/android/wm/shell/bubbles/BubbleDataRepository.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleDataRepository.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleDataRepository.kt index e37c785f15f57..942dcd9db54c2 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleDataRepository.kt +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubbleDataRepository.kt @@ -33,6 +33,7 @@ import com.android.wm.shell.common.ShellExecutor import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.launch import kotlinx.coroutines.yield @@ -45,7 +46,7 @@ internal class BubbleDataRepository( private val volatileRepository = BubbleVolatileRepository(launcherApps) private val persistentRepository = BubblePersistentRepository(context) - private val ioScope = CoroutineScope(Dispatchers.IO) + private val coroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var job: Job? = null // For use in Bubble construction. @@ -131,7 +132,7 @@ internal class BubbleDataRepository( */ private fun persistToDisk() { val prev = job - job = ioScope.launch { + job = coroutineScope.launch { // if there was an ongoing disk I/O operation, they can be cancelled prev?.cancelAndJoin() // check for cancellation before disk I/O @@ -148,7 +149,7 @@ internal class BubbleDataRepository( * bubbles. */ @SuppressLint("WrongConstant") - fun loadBubbles(userId: Int, cb: (List) -> Unit) = ioScope.launch { + fun loadBubbles(userId: Int, cb: (List) -> Unit) = coroutineScope.launch { /** * Load BubbleEntity from disk. * e.g.