Skip to content

Commit bb3863b

Browse files
committed
Improve thread-safety of VssStoreIdProvider
Changes: - Remove @volatile annotation (redundant with synchronized blocks) - Change cachedStoreIds from var to val (map reference never changes) - Move cache check inside synchronized block for proper thread-safety - MutableMap operations must be synchronized; @volatile only affects reference visibility This ensures all map operations happen under synchronization, preventing potential race conditions when multiple threads access the cache.
1 parent fcb94b9 commit bb3863b

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

app/src/main/java/to/bitkit/data/backup/VssStoreIdProvider.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ import javax.inject.Singleton
1212
class VssStoreIdProvider @Inject constructor(
1313
private val keychain: Keychain,
1414
) {
15-
@Volatile
16-
private var cachedStoreIds: MutableMap<Int, String> = mutableMapOf()
15+
private val cachedStoreIds: MutableMap<Int, String> = mutableMapOf()
1716

1817
fun getVssStoreId(walletIndex: Int = 0): String {
19-
cachedStoreIds[walletIndex]?.let { return it }
20-
21-
return synchronized(this) {
18+
synchronized(this) {
2219
cachedStoreIds[walletIndex]?.let { return it }
2320

2421
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) ?: throw ServiceError.MnemonicNotFound
@@ -32,7 +29,7 @@ class VssStoreIdProvider @Inject constructor(
3229

3330
Logger.info("VSS store id: '$storeId' for walletIndex: $walletIndex", context = TAG)
3431
cachedStoreIds[walletIndex] = storeId
35-
storeId
32+
return storeId
3633
}
3734
}
3835

0 commit comments

Comments
 (0)