mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix thread-safety race in InspectorNetworkHelper (#55579)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55579 InspectorNetworkHelper used a lateinit var with an isInitialized check to lazily create an OkHttpClient. This pattern is not thread-safe — concurrent calls to loadNetworkResource could race on the initialization. Replace with the shared DevSupportHttpClient.websocketClient singleton, which is initialized at class load time and is inherently safe. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D93480252 fbshipit-source-id: fefb144b3b26670954a77aa27c9b7f637290f21b
This commit is contained in:
1 parent
a92c7b1a2d
commit
21d7efaad4
1 file changed
+1
-12
+1
-12
@@ -10,26 +10,15 @@
|
||||
package com.facebook.react.devsupport.inspector
|
||||
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.TimeUnit
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
|
||||
internal object InspectorNetworkHelper {
|
||||
private lateinit var client: OkHttpClient
|
||||
|
||||
@JvmStatic
|
||||
fun loadNetworkResource(url: String, listener: InspectorNetworkRequestListener) {
|
||||
if (!::client.isInitialized) {
|
||||
client =
|
||||
OkHttpClient.Builder()
|
||||
.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.writeTimeout(10, TimeUnit.SECONDS)
|
||||
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
|
||||
.build()
|
||||
}
|
||||
|
||||
val request =
|
||||
try {
|
||||
@@ -40,7 +29,7 @@ internal object InspectorNetworkHelper {
|
||||
}
|
||||
|
||||
// TODO(T196951523): Assign cancel function to listener
|
||||
val call = client.newCall(request)
|
||||
val call = DevSupportHttpClient.httpClient.newCall(request)
|
||||
|
||||
call.enqueue(
|
||||
object : Callback {
|
||||
|
||||
Reference in new issue
Block a user