Skip to content

fix: defer platform detection until first use#9467

Open
cyphercodes wants to merge 2 commits into
lysine-dev:masterfrom
cyphercodes:fix-jdk9-platform-class-init-9462
Open

fix: defer platform detection until first use#9467
cyphercodes wants to merge 2 commits into
lysine-dev:masterfrom
cyphercodes:fix-jdk9-platform-class-init-9462

Conversation

@cyphercodes

Copy link
Copy Markdown

Summary

  • Defer OkHttp platform detection until Platform.get() is first used.
  • Add an isolated JVM regression test proving Class.forName("okhttp3.internal.platform.Jdk9Platform") no longer trips the superclass initialization cycle.

Fixes #9462

Testing

  • ./gradlew :okhttp:jvmTest --tests okhttp3.internal.platform.Jdk9PlatformInitializationTest --no-daemon --rerun-tasks
  • ./gradlew :okhttp:jvmTest --tests okhttp3.internal.platform.Jdk9PlatformTest --no-daemon --rerun-tasks
  • git diff --check

Note: locally I had to point Gradle's daemon JVM criteria at the installed Ubuntu OpenJDK 21 to run the checks; that generated file was restored before committing.


companion object {
@Volatile private var platform = findPlatform()
@Volatile private var platform = lazy { findPlatform() }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This feels like a confused line now.

var + lazy + volatile.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

perhaps we make get() responsible for setting it if unset?

@cyphercodes

Copy link
Copy Markdown
Author

Updated the deferred platform initialization follow-up:

  • replaced the var + lazy + @Volatile holder with a nullable platform cache
  • made get() initialize and cache the platform only when unset
  • fixed the missing final newline in the new JDK9 initialization test

Verification:

  • ./gradlew --no-daemon --no-configuration-cache -Dorg.gradle.java.installations.auto-download=false -Dorg.gradle.java.installations.paths=/usr/lib/jvm/java-21-openjdk-amd64,/usr/lib/jvm/java-11-openjdk-amd64 :okhttp:spotlessKotlinCheck :okhttp:jvmTest --tests 'okhttp3.internal.platform.Jdk9PlatformInitializationTest'
  • git diff --check

@yschimke
yschimke requested a review from swankjesse July 18, 2026 14:17
@JakeWharton

Copy link
Copy Markdown
Collaborator

Please stop pasting LLM output. If you're not going to bother to write yourself why should we be bothered to read it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Class.forName(Jdk9Platform) throws during warm up

3 participants