Skip to content

Commit

Permalink
Merge pull request #558 from bitmovin/PRN-145/fix-potential-infinite-…
Browse files Browse the repository at this point in the history
…stall

Use concurrent hash map to avoid potential infinite stall
  • Loading branch information
strangesource authored Nov 14, 2024
2 parents 0cfe9c5 + 2d6efae commit 0735f55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixed

- Error where setting `PlaybackConfig.isAutoplayEnabled = true` causes the player view creation to fail on Android
- Potential infinite stall when using `NetworkConfig.preprocessHttpRequest` and `NetworkConfig.preprocessHttpResponse` on Android

## [0.31.0] - 2024-11-07

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -25,8 +27,9 @@ class NetworkModule(context: ReactApplicationContext) : BitmovinBaseModule(conte
* In-memory mapping from `nativeId`s to `NetworkConfig` instances.
*/
private val networkConfigs: Registry<NetworkConfig> = mutableMapOf()
private val preprocessHttpRequestCompleters: MutableMap<String, Completer<HttpRequest>> = mutableMapOf()
private val preprocessHttpResponseCompleters: MutableMap<String, Completer<HttpResponse>> = mutableMapOf()
private val preprocessHttpRequestCompleters = ConcurrentHashMap<String, Completer<HttpRequest>>()
private val preprocessHttpResponseCompleters = ConcurrentHashMap<String, Completer<HttpResponse>>()

override fun getName() = MODULE_NAME

fun getConfig(nativeId: NativeId?): NetworkConfig? = nativeId?.let { networkConfigs[it] }
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0735f55

Please sign in to comment.