network: saturate huge Retry-After delays instead of wrapping
- Commit
cef13ee58f63ef15702321e3975187bd38d3987f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-02 01:13 UTC (4 days ago)
- Parents
1c6f8df
Symptom: a Retry-After delay-seconds value above Long.MAX_VALUE / 1000 (a hostile or broken server, or a far-future throttling instruction) wrapped negative when multiplied by 1000, so the retry loop read it as no server delay named and retried immediately against a server that had just asked it to back off. Fix: saturate at Long.MAX_VALUE. Retry callers cap the result at their policy maximum, so saturation is safe and the throttling instruction is honoured as the longest wait the policy allows. Verification: new RetryAfterOverflowTest failed the saturation case before the fix and passes after, alongside the existing RetryAfterTest and NetworkRetryTest; the full :library:network:testDebugUnitTest, :library:network:lint and :library:network:lintVitalRelease suites are green.
Diff
library/network/src/main/java/com/vayunmathur/library/network/RetryAfter.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/RetryAfter.kt b/library/network/src/main/java/com/vayunmathur/library/network/RetryAfter.ktindex fd77ba86a4..946351258a 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/RetryAfter.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/RetryAfter.kt@@ -37,7 +37,16 @@ object RetryAfter { fun parseMs(header: String?, nowMs: Long = System.currentTimeMillis()): Long { val value = header?.trim()?.takeIf { it.isNotEmpty() } ?: return 0L value.toLongOrNull()?.let { seconds ->- return if (seconds >= 0) seconds * MILLIS_PER_SECOND else 0L+ if (seconds < 0) return 0L+ // Saturate rather than wrap: seconds * 1000 overflows past+ // Long.MAX_VALUE / 1000, and a wrapped negative delay would be+ // read as "no server delay named" by the retry loop. Callers+ // cap the result at their policy maximum anyway.+ return if (seconds > Long.MAX_VALUE / MILLIS_PER_SECOND) {+ Long.MAX_VALUE+ } else {+ seconds * MILLIS_PER_SECOND+ } } val dateMs = parseHttpDateMs(value) ?: return 0L return (dateMs - nowMs).coerceAtLeast(0L)
library/network/src/test/java/com/vayunmathur/library/network/RetryAfterOverflowTest.kt
diff --git a/library/network/src/test/java/com/vayunmathur/library/network/RetryAfterOverflowTest.kt b/library/network/src/test/java/com/vayunmathur/library/network/RetryAfterOverflowTest.ktnew file mode 100644index 0000000000..a91ffaf82d--- /dev/null+++ b/library/network/src/test/java/com/vayunmathur/library/network/RetryAfterOverflowTest.kt@@ -0,0 +1,32 @@+package com.vayunmathur.library.network++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/**+ * A syntactically valid but absurd `Retry-After` delay-seconds value must+ * saturate instead of wrapping: `seconds * 1000` overflows Long for+ * seconds above Long.MAX_VALUE / 1000, and a wrapped negative delay is+ * read by retry callers as "no server delay named" (or worse, passed to+ * a sleeper), silently dropping the server's throttling instruction.+ */+class RetryAfterOverflowTest {++ @Test+ fun hugeDelaySecondsSaturateInsteadOfWrapping() {+ val parsed = RetryAfter.parseMs("9223372036854776", nowMs = 0L)+ assertEquals(Long.MAX_VALUE, parsed)+ }++ @Test+ fun largestNonOverflowingDelayIsExact() {+ // Long.MAX_VALUE / 1000 = 9223372036854775, times 1000 still fits.+ assertEquals(9_223_372_036_854_775_000L, RetryAfter.parseMs("9223372036854775", nowMs = 0L))+ }++ @Test+ fun hugeDelayIsNeverNegative() {+ assertTrue(RetryAfter.parseMs("18446744073709551615", nowMs = 0L) >= 0L)+ }+}