@@ -345,8 +345,18 @@ static bool DrainPendingJniException(JEnv& env, std::string& outClassName, std::
345345}
346346
347347static bool PerformHttpFetchOnceSync (const std::string& url, const std::string& canonicalKey,
348- std::string& out, std::string& contentType, int & status,
349- bool & bustApplied);
348+ int timeoutMs, std::string& out, std::string& contentType,
349+ int & status, bool & bustApplied);
350+
351+ // Each module-fetch attempt's connect timeout and read timeout, for both
352+ // transports, matching iOS. Debug builds talk to dev servers that transform
353+ // cold modules on demand (measured at 5-9 s); release builds fetch remote
354+ // modules that should fail fast.
355+ static int ModuleFetchTimeoutMs () {
356+ return IsDebuggable () ? 30000 : 5000 ;
357+ }
358+
359+ static constexpr std::chrono::milliseconds kModuleFetchRetryDelay {120 };
350360
351361static std::string ApplyCacheBustNonce (const std::string& url, const std::string& canonicalKey,
352362 bool * outBustRequested) {
@@ -595,16 +605,17 @@ bool HttpFetchModule(const std::string& url, const std::string& canonicalKey,
595605 std::string contentType;
596606 int status = 0 ;
597607 bool bustApplied = false ;
598- bool transportOk =
599- PerformHttpFetchOnceSync (url, canonicalKey, body, contentType, status, bustApplied);
608+ const int timeoutMs = ModuleFetchTimeoutMs ();
609+ bool transportOk = PerformHttpFetchOnceSync (url, canonicalKey, timeoutMs, body, contentType,
610+ status, bustApplied);
600611 if (!transportOk) {
601612 // One retry, and only for a transport error: an HTTP status is an
602613 // answer, not a failure to communicate, so asking again would just
603614 // repeat it.
604615 TNS_DEBUG (Esm, " [http-loader] retrying %s after initial fetch error" , url.c_str ());
605- usleep ( 120 * 1000 );
606- transportOk =
607- PerformHttpFetchOnceSync (url, canonicalKey, body, contentType, status, bustApplied);
616+ std::this_thread::sleep_for ( kModuleFetchRetryDelay );
617+ transportOk = PerformHttpFetchOnceSync (url, canonicalKey, timeoutMs, body, contentType,
618+ status, bustApplied);
608619 }
609620
610621 ClassifyModuleResponse (url, transportOk, status, contentType, body, result);
@@ -645,13 +656,14 @@ bool HttpFetchModule(const std::string& url, const std::string& canonicalKey,
645656// tells a status-bearing answer (an empty 404) apart from a transport failure,
646657// and past the InputStream close. The wrappers stay on the setup calls, whose
647658// failures have no per-stage verdict and are caught below.
659+ // `timeoutMs` must be positive: HttpURLConnection reads 0 as no timeout.
648660static bool PerformHttpFetchOnceSync (const std::string& url, const std::string& canonicalKey,
649- std::string& out, std::string& contentType, int & status ,
650- bool & bustApplied) {
661+ int timeoutMs, std::string& out, std::string& contentType,
662+ int & status, bool & bustApplied) {
651663 out.clear ();
652664 contentType.clear ();
653665 status = 0 ;
654- TNS_DEBUG (Esm, " [http-esm][fetch][enter] url=%s" , url.c_str ());
666+ TNS_DEBUG (Esm, " [http-esm][fetch][enter] url=%s timeoutMs=%d " , url.c_str (), timeoutMs );
655667
656668 const std::string fetchUrl = ApplyCacheBustNonce (url, canonicalKey, &bustApplied);
657669
@@ -718,8 +730,8 @@ static bool PerformHttpFetchOnceSync(const std::string& url, const std::string&
718730 jmethodID setUseCaches = env.GetMethodID (clsConn, " setUseCaches" , " (Z)V" );
719731 jmethodID setReqProp =
720732 env.GetMethodID (clsConn, " setRequestProperty" , " (Ljava/lang/String;Ljava/lang/String;)V" );
721- env.CallVoidMethod (conn, setConnectTimeout, 15000 );
722- env.CallVoidMethod (conn, setReadTimeout, 15000 );
733+ env.CallVoidMethod (conn, setConnectTimeout, timeoutMs );
734+ env.CallVoidMethod (conn, setReadTimeout, timeoutMs );
723735 if (setDoInput) {
724736 env.CallVoidMethod (conn, setDoInput, JNI_TRUE );
725737 }
@@ -895,9 +907,27 @@ namespace {
895907struct ModuleFetchJob {
896908 std::string url;
897909 std::string canonicalKey;
910+ ModuleFetchDeadline deadline;
898911 std::function<void (ModuleFetchResult)> completion;
899912};
900913
914+ // The timeout for an attempt starting now, or nullopt when `deadline` has
915+ // already passed.
916+ std::optional<int > AttemptTimeoutMs (const ModuleFetchDeadline& deadline) {
917+ const int policyMs = ModuleFetchTimeoutMs ();
918+ if (!deadline) return policyMs;
919+ const auto now = std::chrono::steady_clock::now ();
920+ if (now >= *deadline) return std::nullopt ;
921+ // Rounded up: a sub-millisecond remainder must not become 0, which
922+ // HttpURLConnection reads as no timeout at all.
923+ const auto remainingMs = std::chrono::ceil<std::chrono::milliseconds>(*deadline - now).count ();
924+ return remainingMs < policyMs ? static_cast <int >(remainingMs) : policyMs;
925+ }
926+
927+ bool RetryFitsDeadline (const ModuleFetchDeadline& deadline) {
928+ return !deadline || *deadline - std::chrono::steady_clock::now () > kModuleFetchRetryDelay ;
929+ }
930+
901931// See the cap's rationale in HttpLoader.h. 16 matches iOS's
902932// HTTPMaximumConnectionsPerHost.
903933constexpr size_t kMaxConcurrentModuleFetches = 16 ;
@@ -921,22 +951,36 @@ void RunModuleFetchJob(const ModuleFetchJob& job) {
921951 int status = 0 ;
922952 const auto start = std::chrono::steady_clock::now ();
923953 bool bustApplied = false ;
924- bool transportOk = PerformHttpFetchOnceSync (job.url , job.canonicalKey , body, contentType,
925- status, bustApplied);
926- if (!transportOk) {
954+ bool transportOk = false ;
955+ std::optional<int > timeoutMs = AttemptTimeoutMs (job.deadline );
956+ if (timeoutMs) {
957+ transportOk = PerformHttpFetchOnceSync (job.url , job.canonicalKey , *timeoutMs, body,
958+ contentType, status, bustApplied);
927959 // Transport error → one retry, the same single-retry policy the
928- // sync path applies.
929- TNS_DEBUG (Esm, " [http-loader][fetch-async] retrying %s after transport error" ,
930- job.url .c_str ());
931- usleep (120 * 1000 );
932- transportOk = PerformHttpFetchOnceSync (job.url , job.canonicalKey , body, contentType,
933- status, bustApplied);
960+ // sync path applies, while the deadline can still fit the delay.
961+ if (!transportOk && !RetryFitsDeadline (job.deadline )) {
962+ TNS_DEBUG (Esm, " [http-loader][fetch-async] no budget left to retry %s" ,
963+ job.url .c_str ());
964+ } else if (!transportOk) {
965+ TNS_DEBUG (Esm, " [http-loader][fetch-async] retrying %s after transport error" ,
966+ job.url .c_str ());
967+ std::this_thread::sleep_for (kModuleFetchRetryDelay );
968+ timeoutMs = AttemptTimeoutMs (job.deadline );
969+ if (timeoutMs) {
970+ transportOk = PerformHttpFetchOnceSync (job.url , job.canonicalKey , *timeoutMs,
971+ body, contentType, status, bustApplied);
972+ }
973+ }
934974 }
935975
936- ClassifyModuleResponse (job.url , transportOk, status, contentType, body, result);
937-
938- if (result.ok && bustApplied) {
939- ClearCacheBustForUrl (job.canonicalKey );
976+ if (timeoutMs) {
977+ ClassifyModuleResponse (job.url , transportOk, status, contentType, body, result);
978+ if (result.ok && bustApplied) {
979+ ClearCacheBustForUrl (job.canonicalKey );
980+ }
981+ } else {
982+ result.failureReason =
983+ " HTTP import failed: " + job.url + " (module graph fetch deadline exceeded)" ;
940984 }
941985
942986 if (!result.ok ) {
@@ -1018,6 +1062,7 @@ void ModuleFetchThreadMain(ModuleFetchJob job) {
10181062} // namespace
10191063
10201064void FetchModuleBodyAsync (const std::string& url, const std::string& canonicalKey,
1065+ ModuleFetchDeadline fetchDeadline,
10211066 std::function<void (ModuleFetchResult result)> completion) {
10221067 // Security gate: single point of enforcement, same as HttpFetchModule.
10231068 if (!IsRemoteUrlAllowed (url)) {
@@ -1030,7 +1075,7 @@ void FetchModuleBodyAsync(const std::string& url, const std::string& canonicalKe
10301075 return ;
10311076 }
10321077
1033- ModuleFetchJob job{url, canonicalKey, std::move (completion)};
1078+ ModuleFetchJob job{url, canonicalKey, fetchDeadline, std::move (completion)};
10341079 {
10351080 std::lock_guard<std::mutex> lock (g_fetchQueueMutex);
10361081 if (g_fetchThreadCount >= kMaxConcurrentModuleFetches ) {
0 commit comments