Skip to content

Avoid race in duplex pipe for streaming calls - #3572

Open
oldergod wants to merge 5 commits into
masterfrom
bquenaudon.2026-04-16.lateflushes
Open

oldergod wants to merge 5 commits into
masterfrom
bquenaudon.2026-04-16.lateflushes

Conversation

@oldergod

@oldergod oldergod commented Apr 16, 2026 •

Copy link
Copy Markdown
Member

Fixes #3370.

Server streaming calls used a duplex request body.
A background coroutine on Dispatchers.IO wrote the request and the HTTP/2 END_STREAM flag after OkHttp started the call.
Many gRPC servers wait for the client's half-close before they stream responses.
This race could delay the responses by about 5 minutes.

RealGrpcServerStreamingCall now sends a request body that writes the single message and closes the stream in writeTo().
OkHttp calls writeTo() before it reads the response, so the server gets the complete request and END_STREAM first.
The body stays duplex, so OkHttp still stops callTimeout after the response headers.
Long server streams keep working with a finite OkHttpClient.callTimeout, like before.

Changes:

  • GrpcMethod has new requestStreaming and responseStreaming parameters.
    Both default to false.
  • The Kotlin generator sets these parameters for streaming methods.
  • GrpcClient.newServerStreamingCall() returns RealGrpcServerStreamingCall.
  • GrpcClient.newStreamingCall() returns GrpcServerStreamingCallStreamingAdapter when responseStreaming is true and requestStreaming is false.
  • Generated clients use newStreamingCall() for all streaming methods when explicitStreamingCalls is disabled.
    These clients get the fix after you generate them again.
  • GrpcServerStreamingCallStreamingAdapter starts the call when the request arrives.
    OkHttp's thread sends the responses, so the caller's scope only reads the request.
  • The adapter's blocking path does not use coroutines.
    It starts the call when the caller writes the request, so the caller can read responses before it closes the MessageSink.
  • GrpcStreamingCallServerStreamingAdapter keeps the old delegation for the GrpcServerStreamingCall { ... } test doubles in GrpcCalls.kt.
  • readFromResponseBodyCallback() and BlockingMessageSource take an onResponseMetadata callback.
    Streaming and server streaming calls share them.

Release note: Server streaming gRPC calls now send the complete request and half-close before they read responses.
This prevents long delays on servers that wait for the half-close.
The OkHttpClient.callTimeout behavior does not change.
GrpcMethod has new requestStreaming and responseStreaming parameters, and the Kotlin generator sets them.
Generate clients again to get the fix for GrpcStreamingCall server streaming methods.

@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch 3 times, most recently from ba9575f to c11403d Compare April 16, 2026 10:09
@oldergod

Copy link
Copy Markdown
Member Author

Does it look legit to you @swankjesse ?

@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch 4 times, most recently from b6279d3 to 55fdcba Compare May 1, 2026 13:32
@oldergod
oldergod marked this pull request as ready for review May 1, 2026 13:41
@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch from 55fdcba to bb12bfc Compare May 6, 2026 10:49
@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch from bb12bfc to 7720d10 Compare May 14, 2026 15:26
@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch 2 times, most recently from b4cfb31 to d441c68 Compare November 2, 2026 09:57
The legacy adapter now starts the call when the request arrives.
OkHttp's thread sends the responses, so the caller's scope only reads the request.
A request channel closed with a cause now cancels the call.
A canceled MessageSink now cancels the call.
The blocking path no longer uses coroutines.
It sends the request when the MessageSink closes.

Add tests for routing, blocking, cancel, empty request, error, and clone paths.
@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch from d441c68 to 63b0ffd Compare November 9, 2026 17:36
Canceling the legacy adapter before the request arrives now fails the pending response stream.
This also applies when the call was canceled before it executed.
The blocking path now starts the call on the first write.
Callers can read responses before they close the MessageSink.
GrpcStreamingCall.cancel() promises an IOException.
The legacy adapter now uses one when the call is canceled before the request arrives.
Server streaming calls now send a duplex request body that writes the single message and closes the stream.
OkHttp still writes the request and END_STREAM before it reads the response.
OkHttp stops the call timeout after the response headers only for duplex requests.
With this body, long server streams no longer fail when OkHttpClient.callTimeout expires.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GrpcStreamingCall client buffers packets and flushes only after timeout — not receiving events in real-time

1 participant