From 0b96cd651355d5f28779d9864ab3d38c4e5c38f0 Mon Sep 17 00:00:00 2001 From: CatLover01 <160437882+CatLover01@users.noreply.github.com> Date: Fri, 2 Jan 2026 11:46:38 -0500 Subject: [PATCH] fix(android): cancel coroutine scope on React context reload --- .../RNDocumentPickerModule.kt | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/document-picker/android/src/main/java/com/reactnativedocumentpicker/RNDocumentPickerModule.kt b/packages/document-picker/android/src/main/java/com/reactnativedocumentpicker/RNDocumentPickerModule.kt index 75e60748..77d3d2d8 100644 --- a/packages/document-picker/android/src/main/java/com/reactnativedocumentpicker/RNDocumentPickerModule.kt +++ b/packages/document-picker/android/src/main/java/com/reactnativedocumentpicker/RNDocumentPickerModule.kt @@ -21,7 +21,9 @@ import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.WritableMap import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch class RNDocumentPickerModule(reactContext: ReactApplicationContext) : @@ -32,7 +34,18 @@ class RNDocumentPickerModule(reactContext: ReactApplicationContext) : private val pickedFilesUriMap = mutableMapOf() private val metadataGetter = MetadataGetter(pickedFilesUriMap) private val fileOps = FileOperations(pickedFilesUriMap) - private val fileCopyingCoroutine = CoroutineScope(Dispatchers.IO) + private var fileCopyingCoroutine = createCoroutineScope() + + private fun createCoroutineScope(): CoroutineScope { + return CoroutineScope(SupervisorJob() + Dispatchers.IO) + } + + private fun ensureScopeActive(): CoroutineScope { + if (!fileCopyingCoroutine.isActive) { + fileCopyingCoroutine = createCoroutineScope() + } + return fileCopyingCoroutine + } private val activityEventListener: ActivityEventListener = object : BaseActivityEventListener() { @@ -174,7 +187,8 @@ class RNDocumentPickerModule(reactContext: ReactApplicationContext) : "You did not provide the correct options. Expected 'files' and 'destination', got: ${options.toHashMap().keys}" ) } - fileCopyingCoroutine.launch { + val scope = ensureScopeActive() + val job = scope.launch { val results = fileOps.copyFilesToLocalStorage( reactApplicationContext, filesToCopy, @@ -257,7 +271,8 @@ class RNDocumentPickerModule(reactContext: ReactApplicationContext) : } override fun writeDocuments(options: ReadableMap, promise: Promise) { - fileCopyingCoroutine.launch { + val scope = ensureScopeActive() + val job = scope.launch { try { val targetUriString = if (options.hasKey("uri")) options.getString("uri") else null @@ -304,7 +319,8 @@ class RNDocumentPickerModule(reactContext: ReactApplicationContext) : else -> emptyList() } - fileCopyingCoroutine.launch { + val scope = ensureScopeActive() + val job = scope.launch { try { val pickOptions = currentPickOptions require(pickOptions != null)