fix: avoid kotlin.time.Duration in CacheControl to fix Kotlin 2.3 compat#9397
Open
faraz152 wants to merge 1 commit intosquare:masterfrom
Open
fix: avoid kotlin.time.Duration in CacheControl to fix Kotlin 2.3 compat#9397faraz152 wants to merge 1 commit intosquare:masterfrom
faraz152 wants to merge 1 commit intosquare:masterfrom
Conversation
Replace `Int.MAX_VALUE.seconds` with `maxStale(Int.MAX_VALUE, TimeUnit.SECONDS)` in `commonForceCache()`. The `.seconds` extension goes through internal stdlib methods (`Duration.Companion.fromRawValue`) whose mangled names changed in Kotlin 2.3, causing NoSuchMethodError at runtime when OkHttp (compiled with Kotlin 2.2.x) runs against Kotlin 2.3 stdlib. Using the TimeUnit-based overload avoids the kotlin.time.Duration code path entirely, making OkHttp binary-compatible across Kotlin 2.2.x and 2.3.x. Fixes square#9347
Collaborator
|
Is there a YouTrack issue which details the breaking change? Something published should still be stable. |
Author
|
I couldn't find an existing YouTrack issue for this specific breakage (
But neither covers the 2.2→2.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #9347 —
NoSuchMethodError: Duration.Companion.fromRawValue-UwyO8pcwhen OkHttp 5.x (compiled with Kotlin 2.2.x) runs against Kotlin 2.3 stdlib.Root cause
commonForceCache()in-CacheControlCommon.ktusesInt.MAX_VALUE.seconds, which callskotlin.time.Duration.Companion.seconds→toDuration→durationOfNanos→Duration.Companion.fromRawValue. ThefromRawValuemethod is@PublishedApi internalin stdlib, and its mangled name changed between Kotlin 2.2.x and 2.3.x. When Gradle resolves a single stdlib (2.3.x wins), the OkHttp bytecode referencing the old mangled name crashes.Fix
Replace:
With:
This avoids the
kotlin.time.Durationcode path entirely. TheTimeUnit-basedmaxStaleoverload is OkHttp's own public API — no stdlib internals involved.Changes
-CacheControlCommon.kt: Replacekotlin.time.Duration.Companion.secondsimport withjava.util.concurrent.TimeUnit, changemaxStalecall to useTimeUnitoverloadTesting
CacheControlTestandCacheControlJvmTestcoverFORCE_CACHEbehavior — the value ofmaxStaleSecondsis unchanged (Int.MAX_VALUE)