Skip to content

Use Int64.random instead of .randomElement in HTTPConnectionPool.calculateBackoff #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jessezamora
Copy link
Contributor

@jessezamora jessezamora commented Jul 11, 2025

Fixes #847.

Motivation:

On 32-bit systems, using .randomElement on a range larger than what can fit in Int32 (Int) 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.

I tested this on my armv7 board doing the retries, and ran up to several hundred repetitions after a few hours with no crashes as was happening before.

@Lukasa

…ulateBackoff

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.
@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jul 11, 2025
Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really lovely @jessezamora, thank you! ✨

@Lukasa Lukasa enabled auto-merge (squash) July 11, 2025 14:28
@Lukasa Lukasa merged commit 0b6f957 into swift-server:main Jul 11, 2025
25 checks passed
@jessezamora
Copy link
Contributor Author

Thank you!

@jessezamora jessezamora deleted the #847-calculate-backoff-32bit-crash branch July 11, 2025 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTTPConnectionPool.calculateBackoff(failedAttempt:) crashes on 32-bit platforms due to use of randomElement
2 participants