Skip to content

Fix race in MockWebServer's Http2SocketHandler - #9879

Merged
yschimke merged 1 commit into
lysine-dev:mainfrom
FletchMcKee:fix-Http2SocketHandler-race
Nov 22, 2026
Merged

yschimke merged 1 commit into
lysine-dev:mainfrom
FletchMcKee:fix-Http2SocketHandler-race

Conversation

@FletchMcKee

@FletchMcKee FletchMcKee commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9369 and #9673

Altogether this should fix these 15 flaky test cases:

// RouteFailureTest
http2OneBadHostOneGoodRetryOnConnectionFailureFastFallback
http2OneBadHostOneGoodRetryOnConnectionFailure
// FastFallbackTest
preferCallConnectionOverDeferredConnection
// Both HttpOverHttp2Test and HttpOverHttp2Test_HTTP_2
recoverFromConnectionNoNewStreamsOnFollowUp
recoverFromMultipleRefusedStreamsRequiresNewConnection
recoverFromOneCancelRequiresNewConnection
recoverFromOneInternalErrorRequiresNewConnection
connectionWithTwoRefusedStreamsIsNotPooled
recoverFromRefusedStreamWhenAnotherRouteExists

You can reproduce these test failures in main by adding a thread sleep before dispatchBookkeepingRequest:

if (handleSocketEffect(peek.onRequestStart, socket, stream)) {
  Thread.sleep(100)
  dispatchBookkeepingRequest(
    connectionIndex = connectionIndex,
    exchangeIndex = nextExchangeIndex.getAndIncrement(),
    socket = socket,
    requestLine = DEFAULT_REQUEST_LINE_HTTP_2,
  )
  return
}

This was flaky because the bookkeeping dispatch usually beat the client's retry.

I think the race may have been introduced in #8870 as it switched the order of closing the stream and dispatching.

@yschimke

Copy link
Copy Markdown
Collaborator

JDK 8 error is unrelated


gradle/actions: Writing build results to /home/runner/work/_temp/.gradle-actions/build-results/__run-1795346995557.json


FAILURE: Build failed with an exception.
31 problems were found storing the configuration cache.

- Task `:android-test-app:lintAnalyzeDebug` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
* What went wrong:
- Task `:android-test:extractDebugAnnotations` of type `com.android.build.gradle.tasks.ExtractAnnotations`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
Error while saving task graph
- Task `:logging-interceptor:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
> Configuration cache state could not be cached: field `__classpath__` of `com.android.build.gradle.internal.lint.LintTool_Decorated` bean found in field `__lintTool__` of task `:okhttp-dnsoverhttps:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
- Task `:mockwebserver3-junit4:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
   > Could not resolve all files for configuration ':okhttp-dnsoverhttps:androidLintTool'.
- Task `:mockwebserver3-junit5:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
      > Could not find groovy-4.0.29.jar (org.apache.groovy:groovy:4.0.29).
- Task `:mockwebserver3:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
        Searched in the following locations:
- Task `:mockwebserver:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
            https://repo.maven.apache.org/maven2/org/apache/groovy/groovy/4.0.29/groovy-4.0.29.jar
- Task `:native-image-tests:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
      > Could not find kotlin-reflect-2.2.10.jar (org.jetbrains.kotlin:kotlin-reflect:2.2.10).
- Task `:okcurl:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
        Searched in the following locations:
- Task `:okhttp-brotli:compileKotlin` of type `org.jetbrains.kotlin.gradle.tasks.KotlinCompile`: error writing value of type 'org.gradle.api.internal.provider.DefaultProvider'
            https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-reflect/2.2.10/kotlin-reflect-2.2.10.jar
- Task `:okhttp-coroutines:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
      > Could not find kotlin-stdlib-2.2.10.jar (org.jetbrains.kotlin:kotlin-stdlib:2.2.10).
- Task `:okhttp-dnsoverhttps:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
        Searched in the following locations:
- Task `:okhttp-hpacktests:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
            https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/2.2.10/kotlin-stdlib-2.2.10.jar
- Task `:okhttp-idna-mapping-table:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
      > Could not find httpclient-4.5.6.jar (org.apache.httpcomponents:httpclient:4.5.6).
- Task `:okhttp-java-net-cookiejar:lintAnalyzeJvmMain` of type `com.android.build.gradle.internal.lint.AndroidLintAnalysisTask`: error writing value of type 'org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection'
        Searched in the following locations:
plus 16 more problems. Please see the report for details.
            https://repo.maven.apache.org/maven2/org/apache/httpcomponents/httpclient/4.5.6/httpclient-4.5.6.jar

      > Could not find commons-codec-1.17.1.jar (commons-codec:commons-codec:1.17.1).
See the complete report at file:///home/runner/work/okhttp/okhttp/build/reports/configuration-cache/c8chnoyalzg8297di7kiz3fyf/ad14o7fjbmjt55kt7v3vtdrne/configuration-cache-report.html
        Searched in the following locations:

            https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.17.1/commons-codec-1.17.1.jar
[Incubating] Problems report is available at: file:///home/runner/work/okhttp/okhttp/build/reports/problems/problems-report.html
> Configuration cache state could not be cached: field `provider` of `org.gradle.internal.serialize.codecs.core.ProviderBackedFileCollectionSpec` bean found in field `element` of `java.util.Collections$SingletonList` bean found in field `elements` of `org.gradle.internal.serialize.codecs.core.ResolutionBackedFileCollectionSpec` bean found in field `__classpathSnapshot__` of `org.jetbrains.kotlin.gradle.tasks.KotlinCompile$ClasspathSnapshotProperties_Decorated` bean found in field `__classpathSnapshotProperties__` of task `:okhttp-brotli:compileKotlin` of type `org.jetbrains.kotlin.gradle.tasks.KotlinCompile`: error writing value of type 'org.gradle.api.internal.provider.DefaultProvider'

   > Could not resolve all files for configuration ':okhttp-brotli:compileClasspath'.
      > Could not find dec-0.1.2.jar (org.brotli:dec:0.1.2).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/brotli/dec/0.1.2/dec-0.1.2.jar
> Configuration cache state could not be cached: field `provider` of `org.gradle.internal.serialize.codecs.core.ProviderBackedFileCollectionSpec` bean found in field `element` of `java.util.Collections$SingletonList` bean found in field `elements` of `org.gradle.internal.serialize.codecs.core.ResolutionBackedFileCollectionSpec` bean found in field `__classpathSnapshot__` of `org.jetbrains.kotlin.gradle.tasks.KotlinCompile$ClasspathSnapshotProperties_Decorated` bean found in field `__classpathSnapshotProperties__` of task `:okhttp-zstd:compileKotlin` of type `org.jetbrains.kotlin.gradle.tasks.KotlinCompile`: error writing value of type 'org.gradle.api.internal.provider.DefaultProvider'
   > Could not resolve all files for configuration ':okhttp-zstd:compileClasspath'.
      > Could not find zstd-kmp-okio-jvm-0.4.0.jar (com.squareup.zstd:zstd-kmp-okio-jvm:0.4.0).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/com/squareup/zstd/zstd-kmp-okio-jvm/0.4.0/zstd-kmp-okio-jvm-0.4.0.jar
> Configuration cache state could not be cached: field `provider` of `org.gradle.internal.serialize.codecs.core.ProviderBackedFileCollectionSpec` bean found in field `element` of `java.util.Collections$SingletonList` bean found in field `elements` of `org.gradle.internal.serialize.codecs.core.ResolutionBackedFileCollectionSpec` bean found in field `__classpathSnapshot__` of `org.jetbrains.kotlin.gradle.tasks.KotlinCompile$ClasspathSnapshotProperties_Decorated` bean found in field `__classpathSnapshotProperties__` of task `:okhttp-testing-support:compileKotlin` of type `org.jetbrains.kotlin.gradle.tasks.KotlinCompile`: error writing value of type 'org.gradle.api.internal.provider.DefaultProvider'
   > Could not resolve all files for configuration ':okhttp-testing-support:compileClasspath'.
      > Could not find bcpkix-jdk15to18-1.87.jar (org.bouncycastle:bcpkix-jdk15to18:1.87).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/bouncycastle/bcpkix-jdk15to18/1.87/bcpkix-jdk15to18-1.87.jar
      > Could not find junit-jupiter-params-5.13.4.jar (org.junit.jupiter:junit-jupiter-params:5.13.4).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-params/5.13.4/junit-jupiter-params-5.13.4.jar
      > Could not find junit-jupiter-engine-5.13.4.jar (org.junit.jupiter:junit-jupiter-engine:5.13.4).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.13.4/junit-jupiter-engine-5.13.4.jar
      > Could not find AmazonCorrettoCryptoProvider-2.5.0-linux-x86_64.jar (software.amazon.cryptools:AmazonCorrettoCryptoProvider:2.5.0).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/software/amazon/cryptools/AmazonCorrettoCryptoProvider/2.5.0/AmazonCorrettoCryptoProvider-2.5.0-linux-x86_64.jar
      > Could not find hamcrest-library-3.0.jar (org.hamcrest:hamcrest-library:3.0).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-library/3.0/hamcrest-library-3.0.jar
      > Could not find hamcrest-3.0.jar (org.hamcrest:hamcrest:3.0).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest/3.0/hamcrest-3.0.jar
> Configuration cache state could not be cached: field `provider` of `org.gradle.internal.serialize.codecs.core.ProviderBackedFileCollectionSpec` bean found in field `element` of `java.util.Collections$SingletonList` bean found in field `elements` of `org.gradle.internal.serialize.codecs.core.ResolutionBackedFileCollectionSpec` bean found in field `__classpathSnapshot__` of `org.jetbrains.kotlin.gradle.tasks.KotlinCompile$ClasspathSnapshotProperties_Decorated` bean found in field `__classpathSnapshotProperties__` of task `:samples:simple-client:compileKotlin` of type `org.jetbrains.kotlin.gradle.tasks.KotlinCompile`: error writing value of type 'org.gradle.api.internal.provider.DefaultProvider'
   > Could not resolve all files for configuration ':samples:simple-client:compileClasspath'.
      > Could not find kotlin-stdlib-jdk8-1.8.21.jar (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.21).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.21/kotlin-stdlib-jdk8-1.8.21.jar
      > Could not find kotlin-stdlib-jdk7-1.8.21.jar (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.21).
        Searched in the following locations:
            https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.21/kotlin-stdlib-jdk7-1.8.21.jar
> There are 122 more failures with identical causes.

socket = socket,
requestLine = DEFAULT_REQUEST_LINE_HTTP_2,
)
handleSocketEffect(peek.onRequestStart, socket, stream)

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.

ok, so handling here makes sure we have already called dispatcher.dispatch(request) so we can't peek that event twice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, adding logs + the thread sleep would always show the retry peeking the same event before dispatching in those test cases.

Also I can look into some of those configuration cache errors.

@yschimke
yschimke merged commit b2af318 into lysine-dev:main Nov 22, 2026
38 of 41 checks passed
@FletchMcKee
FletchMcKee deleted the fix-Http2SocketHandler-race branch November 22, 2026 14:15
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.

FastFallbackTest.preferCallConnectionOverDeferredConnection flaky

2 participants