From e00f2445d223f512aa3a19b82d6eef207f6b14b3 Mon Sep 17 00:00:00 2001 From: troZee <12766071+troZee@users.noreply.github.com> Date: Sun, 10 Sep 2023 00:45:49 -0700 Subject: [PATCH] fix: network call timeout (#38953) Summary: This PR is a continuation of this https://github.com/facebook/react-native/pull/29471. All context you can find here: https://github.com/axios/axios/issues/2073#issuecomment-1050917487 https://github.com/axios/axios/issues/2073#issuecomment-1674817612 ## Changelog: [ANDROID] [FIXED] - change `connectTimeout` to `callTimeout` in OKHttp client Pull Request resolved: https://github.com/facebook/react-native/pull/38953 Test Plan: - Throttle internet connection (You can use GSM one) ``` import axios from "axios"; axios.get('https://jsonplaceholder.typicode.com/users', { timeout: 5000 }) .then(response => { console.log(response.data); }) .catch(error => { if (error.code === 'ECONNABORTED') { console.log('Request timed out'); } else { console.log(error.message); } }); ``` **Expected result** `Request timed out` should be visible in a console **Videos** Before https://github.com/facebook/react-native/assets/12766071/03a3d666-6a27-4d67-bc68-6bcf6e908e19 After https://github.com/facebook/react-native/assets/12766071/2650d02d-640c-43fe-b1e8-baeac78870f2 **Example URL** Branch: https://github.com/troZee/react-native/tree/fix/network-timeout-example Commit: https://github.com/troZee/react-native/commit/e255e1a423143c4bd4f3e37656a56ba2ecbbf3e2 Reviewed By: mdvacca Differential Revision: D48414884 Pulled By: NickGerleman fbshipit-source-id: 86324601f7459c154b5fc879f8b773df4901f6e0 --- .../com/facebook/react/modules/network/NetworkingModule.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java index 22eb4ea1bb8..4b23bdde76c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java @@ -343,8 +343,8 @@ public final class NetworkingModule extends NativeNetworkingAndroidSpec { // client and set the timeout explicitly on the clone. This is cheap as everything else is // shared under the hood. // See https://github.com/square/okhttp/wiki/Recipes#per-call-configuration for more information - if (timeout != mClient.connectTimeoutMillis()) { - clientBuilder.connectTimeout(timeout, TimeUnit.MILLISECONDS); + if (timeout != mClient.callTimeoutMillis()) { + clientBuilder.callTimeout(timeout, TimeUnit.MILLISECONDS); } OkHttpClient client = clientBuilder.build();