Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cancel coroutine on invalidate #661

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions android/src/main/java/com/oblador/keychain/KeychainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String, Any> {
Expand All @@ -190,7 +199,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
options: ReadableMap?,
promise: Promise
) {
mainCoroutineScope.launch {
coroutineScope.launch {
try {
throwIfEmptyLoginPassword(username, password)
val level = getSecurityLevelOrDefault(options)
Expand Down Expand Up @@ -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) {
Expand Down