Skip to content

Commit 416c971

Browse files
onseokmattcreaser
andauthored
fix(storage): Prevent duplicate observer registration in TransferWorkerObserver to mitigate OOM issues (#2988)
Co-authored-by: Matt Creaser <[email protected]>
1 parent d3975dc commit 416c971

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/TransferWorkerObserver.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.amplifyframework.storage.TransferState
2525
import com.amplifyframework.storage.s3.AWSS3StoragePlugin
2626
import com.amplifyframework.storage.s3.TransferOperations
2727
import com.amplifyframework.storage.s3.transfer.worker.BaseTransferWorker
28+
import java.util.concurrent.ConcurrentHashMap
2829
import java.util.concurrent.Executors
2930
import kotlinx.coroutines.CoroutineScope
3031
import kotlinx.coroutines.Dispatchers
@@ -49,6 +50,8 @@ internal class TransferWorkerObserver private constructor(
4950
AWSS3StoragePlugin.AWS_S3_STORAGE_LOG_NAMESPACE.format(this::class.java.simpleName)
5051
)
5152

53+
private val observedTags = ConcurrentHashMap.newKeySet<String>()
54+
5255
init {
5356
attachObserverForPendingTransfer()
5457
}
@@ -195,13 +198,15 @@ internal class TransferWorkerObserver private constructor(
195198

196199
private suspend fun attachObserver(tag: String) {
197200
withContext(Dispatchers.Main) {
201+
if (!observedTags.add(tag)) return@withContext
198202
val liveData = workManager.getWorkInfosByTagLiveData(tag)
199203
liveData.observeForever(this@TransferWorkerObserver)
200204
}
201205
}
202206

203207
private suspend fun removeObserver(tag: String) {
204208
withContext(Dispatchers.Main) {
209+
if (!observedTags.remove(tag)) return@withContext
205210
workManager.getWorkInfosByTagLiveData(tag)
206211
.removeObserver(this@TransferWorkerObserver)
207212
}

0 commit comments

Comments
 (0)