Skip to content

Avoid race in duplex pipe for streaming calls - #3572

Merged
oldergod merged 9 commits into
masterfrom
bquenaudon.2026-04-16.lateflushes
Nov 23, 2026
Merged

oldergod merged 9 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.
Server streaming calls enforce the GrpcClient call timeout and the per-call timeout and deadline, like the other calls.
They also apply the maxInboundMessageSize limit.

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 onResponseMetadata and callTimeout parameters.
    Streaming and server streaming calls share them.
  • When a legacy server streaming call is canceled, readers get an IOException, like with the duplex implementation.
  • GrpcMethod keeps its 3-argument constructor, so the klib ABI does not change for existing callers.

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
@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch from d441c68 to 63b0ffd Compare November 9, 2026 17:36
@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch from fe3b54a to 1e16534 Compare November 23, 2026 15:49
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.
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.
The default arguments replaced the old constructor signature on Kotlin/Native and JS.
Code compiled against the earlier constructor would fail to link.
…hreads

The legacy adapter closed the response channel with a CancellationException.
A consumer coroutine treated that as its own cancellation and ended without an error.
The duplex implementation gave an IOException, and the adapter now does the same.

The adapter can cancel the call on another thread than the one that starts it.
The call state is volatile so that the cancel is not lost.

Test a second request and a closed response stream on the legacy adapter.
Server streaming calls no longer use the duplex implementation, so its tests did not cover them.
Give the dispatcher tests more time on slow machines.
@oldergod
oldergod force-pushed the bquenaudon.2026-04-16.lateflushes branch from 016663c to 3522e76 Compare November 23, 2026 18:35
The OkHttp call can be canceled before its interceptors record it, so the test failed on slow machines.
@oldergod
oldergod merged commit 3b9cb90 into master Nov 23, 2026
18 checks passed
@oldergod
oldergod deleted the bquenaudon.2026-04-16.lateflushes branch November 23, 2026 20:10
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