Avoid race in duplex pipe for streaming calls#3572
Draft
Conversation
2160ea1 to
ba9575f
Compare
ba9575f to
c11403d
Compare
Member
Author
|
Does it look legit to you @swankjesse ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3370
From Claude
Root Cause
GrpcServerStreamingCallwas implemented by delegating toGrpcStreamingCall, which uses aPipeDuplexRequestBody, an asynchronous pipe where the request body is written by a background coroutine onDispatchers.IO. This means the request message and theHTTP/2 END_STREAMflag are sent asynchronously, after the HTTP call is already enqueued.Many gRPC server implementations wait for the client's half-close (
END_STREAM) before starting to stream responses. With the duplex pipe approach, there's a race between when the server receivesEND_STREAMand when it decides to time out the idle stream, causing the reported ~5-minute delay.Fix
RealGrpcServerStreamingCallnow has its own independent implementation usingnewRequestBody()(a non-duplex request body), identical to howRealGrpcCallworks. The complete request message +END_STREAMis written synchronously in OkHttp'swriteTo()call, ensuring the server receives the full request before any response reading begins.The old delegation-based class is preserved as
GrpcStreamingCallServerStreamingAdapter, still used by the test-double factory (GrpcServerStreamingCall { ... }in GrpcCalls.kt).Supporting changes:
readFromResponseBodyCallback()ingrpc.kt: added an overload acceptingonResponseMetadata: (Map<String, String>) -> Unitso bothRealGrpcStreamingCallandRealGrpcServerStreamingCallcan use itBlockingMessageSource: replacedgrpcCall: RealGrpcStreamingCallwithonResponseMetadata: (Map<String, String>) -> Unitfor the same reasonGrpcClient.newServerStreamingCall()— now createsRealGrpcServerStreamingCalldirectly instead of wrapping a streaming call.