From bc6025f39ef930ed343aee2bf2b0deadd93b520b Mon Sep 17 00:00:00 2001 From: Dorian Mazur <46839236+DorianMazur@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:24:03 +0300 Subject: [PATCH] fix: cancel coroutine on invalidate (#661) --- .../java/com/oblador/keychain/KeychainModule.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/oblador/keychain/KeychainModule.kt b/android/src/main/java/com/oblador/keychain/KeychainModule.kt index 3842259a..452fd0da 100644 --- a/android/src/main/java/com/oblador/keychain/KeychainModule.kt +++ b/android/src/main/java/com/oblador/keychain/KeychainModule.kt @@ -26,6 +26,8 @@ import com.oblador.keychain.exceptions.EmptyParameterException import com.oblador.keychain.exceptions.KeyStoreAccessException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.cancel +import kotlinx.coroutines.isActive import java.util.concurrent.TimeUnit import kotlinx.coroutines.launch @@ -127,7 +129,7 @@ class KeychainModule(reactContext: ReactApplicationContext) : private val prefsStorage: PrefsStorage /** Launches a coroutine to perform non-blocking UI operations */ - private val mainCoroutineScope = CoroutineScope(Dispatchers.Default) + private val coroutineScope = CoroutineScope(Dispatchers.Default) // endregion // region Initialization @@ -171,6 +173,13 @@ class KeychainModule(reactContext: ReactApplicationContext) : override fun getName(): String { return KEYCHAIN_MODULE } + + override fun invalidate() { + super.invalidate() + if (coroutineScope.isActive) { + coroutineScope.cancel("$KEYCHAIN_MODULE has been destroyed.") + } + } /** {@inheritDoc} */ override fun getConstants(): Map { @@ -190,7 +199,7 @@ class KeychainModule(reactContext: ReactApplicationContext) : options: ReadableMap?, promise: Promise ) { - mainCoroutineScope.launch { + coroutineScope.launch { try { throwIfEmptyLoginPassword(username, password) val level = getSecurityLevelOrDefault(options) @@ -246,7 +255,7 @@ class KeychainModule(reactContext: ReactApplicationContext) : } private fun getGenericPassword(alias: String, options: ReadableMap?, promise: Promise) { - mainCoroutineScope.launch { + coroutineScope.launch { try { val resultSet = prefsStorage.getEncryptedEntry(alias) if (resultSet == null) {