Skip to content

MockWebServer can send extra HTTP/2 stream resets before consuming queued responses #9821

Open

Description

@Amir-Golmoradi

Related to #9816

I was investigating HttpOverHttp2Test.recoverFromMultipleRefusedStreamsRequiresNewConnection at 67c4f34e7. The test queues two responses that reset the stream with REFUSED_STREAM, followed by a successful response containing "abc".

In Http2SocketHandler.onStream(), MockWebServer reads dispatcher.peek() and applies onRequestStart before calling dispatchBookkeepingRequest(). For CloseStream, this sends the reset before dispatcher.dispatch(request) consumes the queued response.

Since stream handlers run concurrently, a client can receive the reset and retry while the previous handler has not yet consumed its response. The retry can then observe the same queued response.

I reproduced this with a debugger:

  1. Run recoverFromMultipleRefusedStreamsRequiresNewConnection.
  2. Set a breakpoint on requestQueue.add(request) inside dispatchBookkeepingRequest() in MockWebServer.kt.
  3. Set the breakpoint to suspend only the current thread.
  4. Leave the handler suspended so the client can retry.

The client received two refusals on the original connection, opened another connection, and received a third refusal there. The call then failed with StreamResetException: REFUSED_STREAM, even though only two refusals were queued.

I would expect each scripted refusal to be consumed once, with the third attempt receiving "abc".

The breakpoint deliberately forces a delay between sending the reset and consuming the response. I haven’t reproduced this exact failure without the debugger or established whether it explains the Windows CI failure that prompted the investigation.

Environment: Arch Linux x86_64, Java 21, Gradle 9.8.2.

Activity

  1. added
    bug
    on Oct 27, 2026
  2. Amir-Golmoradi commented on Oct 27, 2026

    @Amir-Golmoradi
    ContributorAuthor

    @JakeWharton , could you take a look at this when you have a chance?

  3. JakeWharton commented on Oct 27, 2026

    @JakeWharton
    Collaborator

    There is no need to mention individual maintainers. We see issues automatically. But generally, no, I don't really review too much here anymore.

  4. Amir-Golmoradi commented on Oct 27, 2026

    @Amir-Golmoradi
    ContributorAuthor

    There is no need to mention individual maintainers. We see issues automatically. But generally, no, I don't really review too much here anymore.

    Noted.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions