From 304f1717f10944cc604d6641aef5c8cf2bb8e1b5 Mon Sep 17 00:00:00 2001 From: Jesse Zamora Date: Thu, 10 Jul 2025 17:02:04 -0400 Subject: [PATCH] Use Int64.random instead of .randomElement in HTTPConnectionPool.calculateBackoff Motivation: On 32-bit systems, using .randomElement on a range larger than what can fit in Int32 causes a crash. After only 26 or 27 retries of a request using HTTPClient, the calculateBackoff method would run into this and crash consistently on an armv7 (32-bit) device. Modifications: A one-line fix to opt to using Int64.random on the same jitterRange instead of .randomElement, which works as expected without crashing on 32-bit systems. Result: The HTTPClient now works as expected and can perform as many retries as needed without crashing. --- .../State Machine/HTTPConnectionPool+Backoff.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+Backoff.swift b/Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+Backoff.swift index 71d8f15f1..e2ef564a5 100644 --- a/Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+Backoff.swift +++ b/Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+Backoff.swift @@ -61,7 +61,7 @@ extension HTTPConnectionPool { // Calculate a 3% jitter range let jitterRange = (backoff.nanoseconds / 100) * 3 // Pick a random element from the range +/- jitter range. - let jitter: TimeAmount = .nanoseconds((-jitterRange...jitterRange).randomElement()!) + let jitter: TimeAmount = .nanoseconds(Int64.random(in: -jitterRange...jitterRange)) let jitteredBackoff = backoff + jitter return jitteredBackoff }