grpc-api: fix client serializer-cache collision across message types - #3568
Merged
bryce-anderson merged 2 commits intoJul 14, 2026
Merged
Conversation
#### Motivation DefaultGrpcClientCallFactory cached request serializers in two static maps keyed only by the request BufferEncoder (the compression codec). The cached GrpcSerializer/GrpcStreamingSerializer embeds a message-type-specific request serializer, so the first client to send a compressed request with a given codec won that codec's cache entry for the entire JVM. Any other client that later reused the same codec (e.g. gzip) for a different request message type received the wrong-typed cached serializer and failed with a ClassCastException during request serialization, before the request was sent. Only the compressed-request path was affected; uncompressed requests short-circuit to the identity serializer. #### Modifications - Drop the static serializerMap and streamingSerializerMap. The compressed serializer is method-specific and must not be shared across methods keyed only by the compressor, so build it per call instead. The uncompressed path still returns the shared identity serializer and allocates nothing; only the (uncommon) compressed path now allocates a small short-lived serializer, whose cost is negligible next to the compression pass it wraps. - Add a collision regression test in GrpcLargeMessageTest: two clients that share the gzip request compressor for different message types (Greeter/HelloRequest and Tester/TestRequest); both compressed round-trips must succeed. #### Result Multiple gRPC clients in the same JVM can share a request compression codec for different message types without colliding. No public API or behavior change.
daschl
approved these changes
Jul 14, 2026
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.
Motivation
DefaultGrpcClientCallFactory cached request serializers in two static maps keyed
only by the request BufferEncoder (the compression codec). The cached
GrpcSerializer/GrpcStreamingSerializer embeds a message-type-specific request
serializer, so the first client to send a compressed request with a given codec
won that codec's cache entry for the entire JVM. Any other client that later
reused the same codec (e.g. gzip) for a different request message type received
the wrong-typed cached serializer and failed with a ClassCastException during
request serialization, before the request was sent. Only the compressed-request
path was affected; uncompressed requests short-circuit to the identity serializer.
Modifications
serializer is method-specific and must not be shared across methods keyed only
by the compressor, so build it per call instead. The uncompressed path still
returns the shared identity serializer and allocates nothing; only the
(uncommon) compressed path now allocates a small short-lived serializer, whose
cost is negligible next to the compression pass it wraps.
the gzip request compressor for different message types (Greeter/HelloRequest
and Tester/TestRequest); both compressed round-trips must succeed.
Result
Multiple gRPC clients in the same JVM can share a request compression codec for
different message types without colliding. No public API or behavior change.