From 582910567297370f20af426a30cb900d9eb19e80 Mon Sep 17 00:00:00 2001 From: Lukas Knoch-Girstmair Date: Wed, 13 Nov 2024 10:43:11 +0100 Subject: [PATCH 1/2] Use concurrent hash map to avoid potential infinite stall --- .../bitmovin/player/reactnative/NetworkModule.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/com/bitmovin/player/reactnative/NetworkModule.kt b/android/src/main/java/com/bitmovin/player/reactnative/NetworkModule.kt index 4fdd5a37..4cbc854c 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/NetworkModule.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/NetworkModule.kt @@ -1,5 +1,6 @@ package com.bitmovin.player.reactnative +import android.util.Log import androidx.concurrent.futures.CallbackToFutureAdapter import androidx.concurrent.futures.CallbackToFutureAdapter.Completer import com.bitmovin.player.api.network.HttpRequest @@ -14,6 +15,7 @@ import com.bitmovin.player.reactnative.converter.toJson import com.bitmovin.player.reactnative.converter.toNetworkConfig import com.facebook.react.bridge.* import com.facebook.react.module.annotations.ReactModule +import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Future private const val MODULE_NAME = "NetworkModule" @@ -25,8 +27,9 @@ class NetworkModule(context: ReactApplicationContext) : BitmovinBaseModule(conte * In-memory mapping from `nativeId`s to `NetworkConfig` instances. */ private val networkConfigs: Registry = mutableMapOf() - private val preprocessHttpRequestCompleters: MutableMap> = mutableMapOf() - private val preprocessHttpResponseCompleters: MutableMap> = mutableMapOf() + private val preprocessHttpRequestCompleters = ConcurrentHashMap>() + private val preprocessHttpResponseCompleters = ConcurrentHashMap>() + override fun getName() = MODULE_NAME fun getConfig(nativeId: NativeId?): NetworkConfig? = nativeId?.let { networkConfigs[it] } @@ -94,9 +97,14 @@ class NetworkModule(context: ReactApplicationContext) : BitmovinBaseModule(conte @ReactMethod fun setPreprocessedHttpRequest(requestId: String, request: ReadableMap) { - preprocessHttpRequestCompleters[requestId]?.set(request.toHttpRequest()) - preprocessHttpRequestCompleters.remove(requestId) + val completer = preprocessHttpRequestCompleters.remove(requestId) + if (completer == null) { + Log.e(MODULE_NAME, "Completer is null for requestId: $requestId, this can cause stuck network requests") + return + } + completer.set(request.toHttpRequest()) } + private fun preprocessHttpResponseFromJS( nativeId: NativeId, type: HttpRequestType, From 2cb3b0b0e726141f20e5e1f5a092a037aeab262c Mon Sep 17 00:00:00 2001 From: Lukas Knoch-Girstmair Date: Wed, 13 Nov 2024 11:02:38 +0100 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d058a09b..8f2918c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Fixed + +- Potential infinite stall when using `NetworkConfig.preprocessHttpRequest` and `NetworkConfig.preprocessHttpResponse` on Android + ## [0.31.0] - 2024-11-07 ### Added