@@ -9,8 +9,11 @@ import com.facebook.react.ReactPackage
99import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
1010import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
1111import com.facebook.react.defaults.DefaultReactNativeHost
12+ import com.facebook.react.modules.network.OkHttpClientFactory
13+ import com.facebook.react.modules.network.OkHttpClientProvider
1214import com.facebook.react.soloader.OpenSourceMergedSoMapping
1315import com.facebook.soloader.SoLoader
16+ import java.util.concurrent.TimeUnit
1417
1518// expo related packages
1619import android.content.Context
@@ -49,6 +52,39 @@ class MainApplication : Application(), ReactApplication {
4952 override fun onCreate () {
5053 super .onCreate()
5154
55+ // React Native builds its shared OkHttpClient with connect, read and write
56+ // timeouts of 0, which OkHttp reads as "wait forever"
57+ // (OkHttpClientProvider.createClientBuilder). A socket that is accepted and
58+ // then goes silent therefore never produces an error, and the JS promise
59+ // behind it never settles.
60+ //
61+ // connectTimeout is the one that changes behaviour rather than just adding a
62+ // ceiling: OkHttp tries a host's addresses one route at a time, each with its
63+ // own connect timeout, so with 0 a single black-holed address hangs the call
64+ // forever and the remaining addresses are never tried. 10s is well above a
65+ // real handshake even on a poor link, and low enough that a dead route falls
66+ // over to the next one inside the JS deadline (utils/networkTimeout).
67+ //
68+ // read and write are deliberately left alone. They are idle timeouts applied
69+ // to every request on the shared client, so any value low enough to be a
70+ // useful backstop is also low enough to cut short a request that asked for
71+ // longer: a server that accepts an order and then works on it silently would
72+ // be aborted mid-flight, which is the unknown-outcome case the wider
73+ // purchase deadline exists to avoid. The per-request deadline belongs on
74+ // callTimeout, which React Native sets per request from the JS-side timeout
75+ // (NetworkingModule), and every JS caller now carries one.
76+ //
77+ // Must be set before the first client is created, which happens when
78+ // NetworkingModule is built.
79+ OkHttpClientProvider .setOkHttpClientFactory(
80+ OkHttpClientFactory {
81+ // createClientBuilder(context) keeps React Native's own cookie jar
82+ // and its 10MB response cache; only the timeouts change.
83+ OkHttpClientProvider .createClientBuilder(this )
84+ .connectTimeout(10 , TimeUnit .SECONDS )
85+ .build()
86+ }
87+ )
5288
5389 SoLoader .init (this , OpenSourceMergedSoMapping )
5490
0 commit comments