Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa2501b0 authored by Evan Laird's avatar Evan Laird
Browse files

[mobile] wrap the get-or-create block in a `synchronized`

The bug: while the subIdRepositoryCache is using ConcurrentHashMap, the
`get` (which returns null the first time) does not synchronize with the
call to `createRepositoryForSubId`. This means that if multiple getters
happen at the same time, we can end up creating multiple repositories.

The fix: synchronize both functions around the map.

Test: MobileConnectionsRepositoryTest
Test: manually logging creations of the mobile connection repositories
Bug: 405396564
Flag: EXEMPT bugfix
Change-Id: Ibcb6d0aa9b2732d175280910326cfea181001d83
parent fac5dcbe
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -355,10 +355,12 @@ constructor(
        getOrCreateRepoForSubId(subId)

    private fun getOrCreateRepoForSubId(subId: Int) =
        synchronized(subIdRepositoryCache) {
            subIdRepositoryCache[subId]?.get()
                ?: createRepositoryForSubId(subId).also {
                    subIdRepositoryCache[subId] = WeakReference(it)
                }
        }

    override val mobileIsDefault: StateFlow<Boolean> =
        connectivityRepository.defaultConnections